package Chat;
import javax.swing.*;
import java.awt.*;
public class ServerChatMain extends JFrame {
public static void main(String[] args) {
new ServerChatMain();
}
private JTextArea jta;
private JScrollPane jsp;
private JPanel jp;
private JTextField jtf;
private JButton jb;
public ServerChatMain(){
jta = new JTextArea();
jta.setEditable(false);
jsp = new JScrollPane(jta);
jp = new JPanel();
jtf = new JTextField(10);
jb = new JButton("发送");
jp.add(jtf);
jp.add(jb);
this.add(jsp, BorderLayout.CENTER);
this.add(jp,BorderLayout.SOUTH);
this.setTitle("QQ聊天服务端");
this.setSize(300,300);
this.setLocation(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
由于客户端和服务端效果是一样的,只需要复制一下到客户端即可,下图为在mac电脑上的效果图。

由于客户端和服务端的界面是一样的,复制后修改一下即可。
package Chat;
import javax.swing.*;
import java.awt.*;
public class ClientChatMain extends JFrame {
public static void main(String[] args) {
new ClientChatMain();
}
private JTextArea jta;
private JScrollPane jsp;
private JPanel jp;
private JTextField jtf;
private JButton jb;
public ClientChatMain(){
jta = new JTextArea();
jta.setEditable(false);
jsp = new JScrollPane(jta);
jp = new JPanel();
jtf = new JTextField(10);
jb = new JButton("发送");
jp.add(jtf);
jp.add(jb);
this.add(jsp, BorderLayout.CENTER);
this.add(jp,BorderLayout.SOUTH);
this.setTitle("QQ聊天客户端");
this.setSize(300,300);
this.setLocation(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

到这一步,聊天的界面也就搭建完成了。
下一步进行:Tcp通信的思路和代码实现(后续补上链接)
记录时间:2020年11月22日