java多线程聊天室
第一次写博客,有不妥之处请指正
使用swing实现的java多线程聊天室
我的代码结构
- Chat
- ClientUI.java
- ConnectUI.java
- HelpUI.java
- PortConfig.java
- ServerUI.java
- User.java
- UserConfig.java
放一下我写的代码吧
借鉴了前辈的内容,自己又改了改,初学,大二,MVC模式还不太会,代码写在一起,请见谅。
目前有私聊功能还未完成,我写了一些但是效果并不理想。。没有出想要的结果,在客户端和服务端注释那里,身边实在没有人能沟通,这里就没有写下去,交了作业就没看了,按理说应该会出现私聊的效果,若有大佬看到写的内容,请您指点一下
下面是代码运行的界面展示
User类
package com.Haomu.chat;
public class User {
private String name;
private String ip;
public User() {
super();
}
public User(String name, String ip) {
super();
this.name = name;
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
服务端ServerUI.java
package com.Haomu.chat;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.CardLayout;
import java.awt.Font;
import javax.swing.UIManager;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import java.awt.Window.Type;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import net.miginfocom.swing.MigLayout;
import javax.swing.JToggleButton;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.CompoundBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.EtchedBorder;
import javax.swing.JEditorPane;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.border.MatteBorder;
public class ServerUI extends JFrame{
private JPanel contentPane;
private JTextField txt;
private JButton btnUser;
private JButton btnConnect;
private JButton btnStop;
private JButton btnExit;
private JButton btnSend;
private JButton btnHelp;
private JTextArea textArea;
private boolean isStart = false;
private List<ClientThread> client = null;
private ServerSocket serverSocket;
private ServerThread serverThread;
private DefaultListModel listModel;
private JList userList;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerUI frame = new ServerUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ServerUI() {
setResizable(false);
setBackground(Color.PINK);
setTitle("聊天室服务端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 600);
contentPane = new JPanel();
contentPane.setBackground(Color.PINK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel menu_panel = new JPanel();
menu_panel.setBackground(Color.PINK);
menu_panel.setBorder(new CompoundBorder());
contentPane.add(menu_panel, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setBackground(Color.PINK);
panel.setBorder(new CompoundBorder());
menu_panel.add(panel);
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
btnUser = new JButton("端口设置");
btnUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PortConfig pc = new PortConfig();
pc.setVisible(true);
}
});
btnUser.setBackground(Color.PINK);
btnUser.setVerticalAlignment(SwingConstants.BOTTOM);
btnUser.setFont(new Font("微软雅黑", Font.BOLD, 16));
panel.add(btnUser);
btnConnect = new JButton("启动服务");
btnConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
});
btnConnect.setBackground(Color.PINK);
btnConnect.setFont(new Font("微软雅黑", Font.BOLD, 16));
panel.add(btnConnect);
JLabel label = new JLabel(" ");
menu_panel.add(label);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.PINK);
panel_1.setBorder(new CompoundBorder());
menu_panel.add(panel_1);
panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
btnStop = new JButton("停止服务");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
}
});
btnStop.setBackground(Color.PINK);
btnStop.setFont(new Font("微软雅黑", Font.BOLD, 16));
panel_1.add(btnStop);
JLabel label_1 = new JLabel(" ");
panel_1.add(label_1);
btnExit = new JButton("退出");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel_1.add(btnExit);
btnExit.setBackground(Color.PINK);
btnExit.setFont(new Font("微软雅黑", Font.BOLD, 16));
JPanel message_panel = new JPanel();
message_panel.setBackground(Color.PINK);
contentPane.add(message_panel, BorderLayout.SOUTH);
message_panel.setLayout(new GridLayout(2, 1, 0, 3));
JPanel panel_3 = new JPanel();
panel_3.setBackground(Color.PINK);
FlowLayout flowLayout = (FlowLayout) panel_3.getLayout();
panel_3.setBorder(new CompoundBorder());
message_panel.add(panel_3);
JLabel send = new JLabel("发送信息:");
panel_3.add(send);
send.setFont(new Font("微软雅黑", Font.BOLD, 16));
txt = new JTextField();
txt.setEditable(false);
txt.setFont(new Font("微软雅黑", Font.BOLD, 14));
txt.setBackground(new Color(255, 255, 255));
txt.setHorizontalAlignment(SwingConstants.CENTER);
panel_3.add(txt);
txt.setColumns(20);
//给文本框增加回车发送功能
txt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
send();
}
});
JLabel label_2 = new JLabel(" ");
panel_3.add(label_2);
//单击发送
btnSend = new JButton("发送");
btnSend.setEnabled(false);
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
send();
}
});
btnSend.setBackground(Color.PINK);
btnSend.setFont(new Font("微软雅黑", Font.BOLD, 16));
panel_3.add(btnSend);
JPanel panel_4 = new JPanel();
panel_4.setBorder(new CompoundBorder());
panel_4.setBackground(Color.PINK);
message_panel.add(panel_4);
panel_4.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 1));
btnHelp = new JButton("帮助");
btnHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
HelpUI h = new HelpUI();
h.setVisible(true);
}
});
panel_4.add(btnHelp);
btnHelp.setFont(new Font("微软雅黑", Font.BOLD, 16));
btnHelp.setBackground(Color.PINK);
JPanel user_panel = new JPanel();
user_panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
contentPane.add(user_panel, BorderLayout.CENTER);
user_panel.setLayout(new BorderLayout(0, 0));
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("微软雅黑", Font.BOLD, 14));
JScrollPane scrollPane = new JScrollPane();
textArea.setBounds(23, 217, 650, 266);
scrollPane.setViewportView(textArea);
user_panel.add(scrollPane);
JPanel panel_5 = new JPanel();
panel_5.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
scrollPane.setRowHeaderView(panel_5);
panel_5.setLayout(new BorderLayout(0, 0));
JLabel label_3 = new JLabel("在线用户");
label_3.setBackground(new Color(255, 255, 255));
label_3.setHorizontalAlignment(SwingConstants.CENTER);
label_3.setFont(new Font("微软雅黑", Font.BOLD, 18));
panel_5.add(label_3, BorderLayout.NORTH);
listModel = new DefaultListModel();
userList = new JList(listModel);
userList.setForeground(new Color(0, 0, 0));
userList.setFont(new Font("微软雅黑", Font.BOLD, 12));
panel_5.add(userList, BorderLayout.CENTER);
}
/**
*
* @Description 发送信息
* @author Haomu
* @date 2019年12月1日 下午12:58:59
*/
public synchronized void send() {
if (!isStart) {
JOptionPane.showMessageDialog(