【java】socket编程,用GUI组件实现简单的QQ聊天功能

【java】socket编程,用GUI组件实现简单的QQ聊天功能_第1张图片
【java】socket编程,用GUI组件实现简单的QQ聊天功能_第2张图片

client

public class QQClient {
    private JTextArea message;
    private JTextArea input;
    private JPanel Client;

    public QQClient() throws IOException {
        Socket socket=new Socket("localhost",8080);
        input.addKeyListener(new InputKeyListener(message,input,socket));
        new ReceiveThread(message,"Server",socket).start();
    }

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("QQClient");
        frame.setContentPane(new QQClient().Client);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

server

public class QQServer {
    private JPanel Server;
    private JTextArea message;
    private JTextArea input;

    public QQServer() throws IOException {
        ServerSocket serverSocket=new ServerSocket(8080);
        Socket socket = serverSocket.accept();
        input.addKeyListener(new InputKeyListener(message,input,socket));
        new ReceiveThread(message,"Client",socket).start();
    }

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("QQServer");
        frame.setContentPane(new QQServer().Server);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

ReceiveThread

public class ReceiveThread extends Thread{
    private JTextArea message;
    private String fromName;
    private BufferedReader reader;

    public ReceiveThread(JTextArea message, String fromName, Socket socket) {
        this.message = message;
        this.fromName = fromName;
        try {
            this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                String text = reader.readLine();
                if(!text.isEmpty()&& !text.equals("\r")&& !text.equals("\n")){
                    message.append(fromName+":"+text+"\n");
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

InputKeyListener

public class InputKeyListener extends KeyAdapter {
    private JTextArea message;
    private JTextArea input;
    private PrintWriter writer;
    public InputKeyListener(JTextArea message, JTextArea input, Socket socket) {
        this.message = message;
        this.input = input;
        try {
            this.writer = new PrintWriter(socket.getOutputStream(), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        super.keyReleased(e);
        if(e.getExtendedKeyCode()==KeyEvent.VK_ENTER){
                String text = input.getText();
                if (!text.isEmpty()&& !text.equals("\r")&& !text.equals("")) {
                    writer.println(text);
                    message.append("我:" + text);
                }
                input.setText("");
        }

    }
}

你可能感兴趣的:(Java)