服务器端部分搭建思路及实现
在阅读本篇之前,建议先阅读本篇的前两个部分:
Java实现基于Socket的聊天室(一)序言:https://blog.csdn.net/Jiang216/article/details/108070585
Java实现基于Socket的聊天室(二)客户端:https://blog.csdn.net/Jiang216/article/details/108071422
1.1 构建服务器端图形化界面
首先构建服务器端部分的图形化显示界面,构建ChatServerView类,图形化控件布局部分如下:
private JButton btnOpen;
private JButton btnStop;
private JTextArea tfLogInfo;
private JScrollPane scrollPane;
private JTextField inputMessage;//消息输入框
private JButton btnSend;//消息发送按钮
JPanel jp1,jp2;
private JLabel label;
private SimpleChatService service = null;
private void initView() {
jp1 = new JPanel();
btnOpen = new JButton("打开服务器");
btnStop = new JButton("关闭服务器");
btnStop.setEnabled(false);
btnOpen.addActionListener(this);//按钮添加点击事件
btnStop.addActionListener(this);//按钮添加点击事件
jp1.add(btnOpen);
jp1.add(btnStop);
jp1.setLayout(new FlowLayout(FlowLayout.CENTER));
tfLogInfo = new JTextArea(20, 20);
tfLogInfo.setEditable(false);
scrollPane = new JScrollPane(tfLogInfo);
label = new JLabel("服务器停止工作");
jp2=new JPanel();
inputMessage=new JTextField(20);
btnSend=new JButton("发送");
btnSend.addActionListener(this);//按钮添加点击事件