• 客户端与服务器聊天------客户端


    package week6.serverAndClient;

    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.JToolBar;

    public class Client extends Thread {
    public String message;
    private Socket socket;
    private DataOutputStream output;
    private DataInputStream input;
    private JTextArea allArea;

    public Client(JTextArea allArea) {
    this.allArea=allArea;
    try {
    socket = new Socket("localhost", 9000);
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    @Override
    public void run() {
    try {
    output = new DataOutputStream(socket.getOutputStream());
    output.writeUTF("张三说:服务器你好!");
    // 接受服务区信息
    while (true) {
    input = new DataInputStream(socket.getInputStream());
    message = input.readUTF();
    allArea.append(message);
    }
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("张三");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setBounds(100, 100, 738, 499);
    frame.setResizable(false);
    Container container = frame.getContentPane();
    container.setLayout(null);

    final JTextArea allArea = new JTextArea();// 总聊天框
    allArea.setBounds(37, 20, 647, 275);
    container.add(allArea);
    final Client client = new Client(allArea);// 创建新连接
    client.start();

    final JTextField chartField = new JTextField();// 个人信息框
    chartField.setBounds(37, 332, 534, 58);
    container.add(chartField);
    chartField.setColumns(10);

    JButton sendBtn = new JButton("发送");// 发送按钮
    sendBtn.setBounds(591, 332, 93, 23);
    container.add(sendBtn);

    sendBtn.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    String message = " "+"张三说:" + chartField.getText();
    chartField.setText(null);
    allArea.append(message);
    try {
    client.output = new DataOutputStream(client.socket.getOutputStream());
    client.output.writeUTF(message);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    });

    JButton canelBtn = new JButton("取消");// 取消按钮
    canelBtn.setBounds(592, 367, 93, 23);
    container.add(canelBtn);
    canelBtn.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    chartField.setText(null);
    }
    });

    }
    }

  • 相关阅读:
    VS2019调试 asp.net core 2.2 出现《ANCM In-Process Handler Load Failure 发布后启动错误处理》处理
    网页上显示数学公式的三种方案
    FileStream实现多线程断点续传(已封装)
    绝对定位不脱离文档流的方法
    百度地图InfoWindow弹窗圆角
    并发:线程池异步执行与创建单独的线程执行
    互斥锁和自旋锁的区别
    事务的特性和隔离级别
    线程不安全与线程安全示例
    多线程过去与现在
  • 原文地址:https://www.cnblogs.com/quanby/p/5457142.html
Copyright © 2020-2023  润新知