JavaSocket实现多人聊天室文档格式.docx
《JavaSocket实现多人聊天室文档格式.docx》由会员分享,可在线阅读,更多相关《JavaSocket实现多人聊天室文档格式.docx(34页珍藏版)》请在冰豆网上搜索。
[html]
viewplaincopy
1.import
java.awt.BorderLayout;
2.import
java.awt.Color;
3.import
java.awt.GridLayout;
4.import
java.awt.Toolkit;
5.import
java.awt.event.ActionEvent;
6.import
java.awt.event.ActionListener;
7.import
java.awt.event.WindowAdapter;
8.import
java.awt.event.WindowEvent;
9.import
java.io.BufferedReader;
10.import
java.io.IOException;
11.import
java.io.InputStreamReader;
12.import
java.io.PrintWriter;
13.import
.BindException;
14.import
.ServerSocket;
15.import
.Socket;
16.import
java.util.ArrayList;
17.import
java.util.StringTokenizer;
18.
19.import
javax.swing.DefaultListModel;
20.import
javax.swing.JButton;
21.import
javax.swing.JFrame;
22.import
javax.swing.JLabel;
23.import
javax.swing.JList;
24.import
javax.swing.JOptionPane;
25.import
javax.swing.JPanel;
26.import
javax.swing.JScrollPane;
27.import
javax.swing.JSplitPane;
28.import
javax.swing.JTextArea;
29.import
javax.swing.JTextField;
30.import
javax.swing.border.TitledBorder;
31.
32.public
class
Server
{
33.
34.
private
JFrame
frame;
35.
JTextArea
contentArea;
36.
JTextField
txt_message;
37.
txt_max;
38.
txt_port;
39.
JButton
btn_start;
40.
btn_stop;
41.
btn_send;
42.
JPanel
northPanel;
43.
southPanel;
44.
JScrollPane
rightPanel;
45.
leftPanel;
46.
JSplitPane
centerSplit;
47.
JList
userList;
48.
DefaultListModel
listModel;
49.
50.
ServerSocket
serverSocket;
51.
ServerThread
serverThread;
52.
ArrayList<
ClientThread>
clients;
53.
54.
boolean
isStart
=
false;
55.
56.
//
主方法,程序执行入口
57.
public
static
void
main(String[]
args)
58.
new
Server();
59.
}
60.
61.
执行消息发送
62.
send()
63.
if
(!
isStart)
64.
JOptionPane.showMessageDialog(frame,
"
服务器还未启动,不能发送消息!
错误"
65.
JOptionPane.ERROR_MESSAGE);
66.
return;
67.
68.
(clients.size()
==
0)
69.
没有用户在线,不能发送消息!
70.
71.
72.
73.
String
message
txt_message.getText().trim();
74.
(message
null
||
message.equals("
))
75.
消息不能为空!
76.
77.
78.
79.
sendServerMessage(message);
群发服务器消息
80.
contentArea.append("
服务器说:
+
txt_message.getText()
\r\n"
);
81.
txt_message.setText(null);
82.
83.
84.
构造放法
85.
Server()
86.
frame
JFrame("
服务器"
87.
更改JFrame的图标:
88.
//frame.setIconImage(Toolkit.getDefaultToolkit().createImage(Client.class.getResource("
qq.png"
)));
89.
frame.setIconImage(Toolkit.getDefaultToolkit().createImage(Server.class.getResource("
90.
contentArea
JTextArea();
91.
contentArea.setEditable(false);
92.
contentArea.setForeground(Color.blue);
93.
txt_message
JTextField();
94.
txt_max
JTextField("
30"
95.
txt_port
6666"
96.
btn_start
JButton("
启动"
97.
btn_stop
停止"
98.
btn_send
发送"
99.
btn_stop.setEnabled(false);
100.
listModel
DefaultListModel();
101.
userList
JList(listModel);
102.
103.
southPanel
JPanel(new
BorderLayout());
104.
southPanel.setBorder(new
TitledBorder("
写消息"
));
105.
southPanel.add(txt_message,
Center"
106.
southPanel.add(btn_send,
East"
107.
leftPanel
JScrollPane(userList);
108.
leftPanel.setBorder(new
在线用户"
109.
110.
rightPanel
JScrollPane(contentArea);
111.
rightPanel.setBorder(new
消