Java简易的局域网聊天工具
作者:网络转载 发布时间:[ 2016/6/21 11:46:26 ] 推荐标签:测试开发技术 Java
main方法
1 package com.lovo.work;
2
3 public class Test {
4
5
6
7 public static void main(String[] args) {
8 // TODO Auto-generated method stub
9 new FeiqiuFrame();
10
11
12 }
13
14 }
主程序:文件中是姓名和IP,用键值对的方式写入
1 package com.lovo.work;
2
3 import java.awt.Color;
4 import java.awt.Container;
5 import java.awt.Font;
6 import java.awt.Toolkit;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.BufferedReader;
10 import java.io.BufferedWriter;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.ObjectInputStream;
16 import java.io.OutputStreamWriter;
17 import java.net.ServerSocket;
18 import java.net.Socket;
19 import java.net.UnknownHostException;
20 import java.text.SimpleDateFormat;
21 import java.util.Calendar;
22 import java.util.HashMap;
23 import java.util.Properties;
24 import java.util.Set;
25
26 import javax.swing.JButton;
27 import javax.swing.JCheckBox;
28 import javax.swing.JComboBox;
29 import javax.swing.JFrame;
30 import javax.swing.JOptionPane;
31 import javax.swing.JScrollPane;
32 import javax.swing.JTextArea;
33 import javax.swing.JTextField;
34
35 public class FeiqiuFrame extends JFrame{
36
37
38
39 private Container contentP;
40 private JTextArea msgArea;
41 private JTextField IPTxt;
42 private JButton qingkongBtn;
43 private JTextField wenbenTxt;
44 private JButton fasongBtn;
45 private String msg;
46 private Socket fromClient;
47 private String str3;
48 private JComboBox classComb;
49 private Properties pro;
50 private JCheckBox xuanze;
51
52
53
54
55
56 public FeiqiuFrame() {
57 pro=new Properties();
58 try {
59 pro.load(new FileInputStream("j124.properties"));//
60 } catch (FileNotFoundException e) {
61 // TODO Auto-generated catch block
62 e.printStackTrace();
63 } catch (IOException e) {
64 // TODO Auto-generated catch block
65 e.printStackTrace();
66 }
67
68 this.setSize(400, 350);
69 Toolkit tk = Toolkit.getDefaultToolkit();
70 int screenW = (int) tk.getScreenSize().getWidth();// 得到屏幕宽
71 int screenH = (int) tk.getScreenSize().getHeight();// 得到屏幕高
72 this.setLocation((screenW - 400) / 2, (screenH - 510) / 2);
73 this.setTitle("我的飞秋");
74 // 设置窗体关闭即为关闭程序
75 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
76 // 设置窗体内容面板上所有的东西
77 this.addContent();
78 // 设置窗体可见
79 this.setVisible(true);
80
81
82
83 ServerSocket server = null;
84 try {
85 server = new ServerSocket(9527);
86 while (true) {
87 // accept是阻塞方法,直到监听到客户端发送过来的消息
88 Socket fromClient = server.accept();
89 new MSGThread(fromClient,this).start();
90
91 }
92 } catch (IOException e) {
93 // TODO Auto-generated catch block
94 e.printStackTrace();
95 } finally {
96 if (server != null) {
97 try {
98 server.close();
99 } catch (IOException e) {
100 // TODO Auto-generated catch block
101 e.printStackTrace();
102 }
103 }
104 }
105
106
107 }
108
109 private void addContent() {
110 // TODO Auto-generated method stub
111 this.contentP = this.getContentPane();
112 // 设置窗体面板的背景色
113 this.contentP.setBackground(Color.WHITE);
114 // 设置容器的布局管理器为空---空布局管理
115 this.contentP.setLayout(null);
116
117 this.msgArea = new JTextArea();
118 JScrollPane sp = new JScrollPane(this.msgArea);
119 sp.setBounds(15, 20, 365, 200);
120 msgArea.setEditable(false);
121
122 // this.msgArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
123 this.contentP.add(sp);
124
125
126
127 this.wenbenTxt = new JTextField();
128 this.wenbenTxt.setFont(new Font("微软雅黑", Font.ITALIC,15));
129 this.wenbenTxt.setForeground(Color.BLUE);
130 this.wenbenTxt.setBounds(15, 230, 110, 25);
131 this.contentP.add(this.wenbenTxt);
132
133 this.fasongBtn = new JButton();
134 this.fasongBtn .setText("发送");
135 this.fasongBtn .setFont(new Font("微软雅黑", Font.PLAIN, 15));
136 this.fasongBtn .setForeground(Color.BLUE);
137 this.fasongBtn .setBounds(130, 230, 65, 25);
138
139
140 this.classComb = new JComboBox(this.pro.keySet().toArray());
141 this.classComb.setEditable(true);
142 this.classComb.setBounds(210, 230,90, 25);
143 this.contentP.add(this.classComb);
144
145
146 this.xuanze = new JCheckBox("自动回复");
147 this.xuanze.setBackground(new Color(225, 249, 255));
148 this.xuanze.setBounds(15, 270, 100, 25);
149 this.contentP.add(this.xuanze);
150
151
152 this.qingkongBtn = new JButton();
153 this.qingkongBtn.setText("清空");
154 this.qingkongBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
155 this.qingkongBtn.setForeground(Color.BLUE);
156 this.qingkongBtn.setBounds(310, 230, 65, 25);
157 this.contentP.add(this.qingkongBtn);
158 this.qingkongBtn.addActionListener(new ActionListener(){
159
160 @Override
161 public void actionPerformed(ActionEvent e) {
162 // TODO Auto-generated method stub
163 msgArea.setText("");
164 }
165
166 });
167 this.fasongBtn.addActionListener(new ActionListener(){
168
169 @Override
170 public void actionPerformed(ActionEvent e) {
171 // TODO Auto-generated method stub
172 String str=wenbenTxt.getText();
173 String ipstr=(String)FeiqiuFrame.this.getClassComb().getSelectedItem();
174 String IP=FeiqiuFrame.this.pro.getProperty(ipstr);
175
176 String str2 = str.replaceAll(" ", "");
177 if(str2!=null&&!str2.equals("")){
178 if(IP.matches("(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}")){
179 msg="XX&"+str2;
180 str3=msgArea.getText();
181 msgArea.setText(str3+"
"+getFormatDate()+"
"+"我说:"+str2);
182
183 Socket client = null;
184 try {
185 client = new Socket(IP, 9527);
186 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
187 bw.write(msg);
188 bw.flush();
189 } catch (UnknownHostException e1) {
190 // TODO Auto-generated catch block
191 e1.printStackTrace();
192 } catch (IOException e1) {
193 // TODO Auto-generated catch block
194 e1.printStackTrace();
195 } finally{
196 if(client != null){
197 try {
198 client.close();
199 } catch (IOException e1) {
200 // TODO Auto-generated catch block
201 e1.printStackTrace();
202 }
203 }
204 }
205 }else{
206 JOptionPane.showMessageDialog(fasongBtn, "IP输入不正确!");
207 }
208
209 }else{
210 JOptionPane.showMessageDialog(fasongBtn, "消息输入不能为空!");
211 }
212 }
213 });
214
215 this.contentP.add(this.fasongBtn );
216
217 }
218 public static String getFormatDate()
219 {
220 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
221 return sdf.format(Calendar.getInstance().getTime());
222 }
223
224 public Container getContentP() {
225 return contentP;
226 }
227
228 public void setContentP(Container contentP) {
229 this.contentP = contentP;
230 }
231
232 public JTextArea getMsgArea() {
233 return msgArea;
234 }
235
236 public void setMsgArea(JTextArea msgArea) {
237 this.msgArea = msgArea;
238 }
239
240 public JTextField getIPTxt() {
241 return IPTxt;
242 }
243
244 public void setIPTxt(JTextField iPTxt) {
245 IPTxt = iPTxt;
246 }
247
248 public JButton getQingkongBtn() {
249 return qingkongBtn;
250 }
251
252 public void setQingkongBtn(JButton qingkongBtn) {
253 this.qingkongBtn = qingkongBtn;
254 }
255
256
257 public JComboBox getClassComb() {
258 return classComb;
259 }
260
261 public void setClassComb(JComboBox classComb) {
262 this.classComb = classComb;
263 }
264
265 public JTextField getWenbenTxt() {
266 return wenbenTxt;
267 }
268
269 public void setWenbenTxt(JTextField wenbenTxt) {
270 this.wenbenTxt = wenbenTxt;
271 }
272
273 public JButton getFasongBtn() {
274 return fasongBtn;
275 }
276
277 public void setFasongBtn(JButton fasongBtn) {
278 this.fasongBtn = fasongBtn;
279 }
280
281 public String getMsg() {
282 return msg;
283 }
284
285 public void setMsg(String msg) {
286 this.msg = msg;
287 }
288
289 public Socket getFromClient() {
290 return fromClient;
291 }
292
293 public void setFromClient(Socket fromClient) {
294 this.fromClient = fromClient;
295 }
296
297 public String getStr3() {
298 return str3;
299 }
300
301 public void setStr3(String str3) {
302 this.str3 = str3;
303 }
304
305
306 }
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11