java 局域网聊天工具_java简易的局域网聊天工具

1 packagecom.lovo.work;2

3 importjava.awt.Color;4 importjava.awt.Container;5 importjava.awt.Font;6 importjava.awt.Toolkit;7 importjava.awt.event.ActionEvent;8 importjava.awt.event.ActionListener;9 importjava.io.BufferedReader;10 importjava.io.BufferedWriter;11 importjava.io.FileInputStream;12 importjava.io.FileNotFoundException;13 importjava.io.IOException;14 importjava.io.InputStreamReader;15 importjava.io.ObjectInputStream;16 importjava.io.OutputStreamWriter;17 importjava.net.ServerSocket;18 importjava.net.Socket;19 importjava.net.UnknownHostException;20 importjava.text.SimpleDateFormat;21 importjava.util.Calendar;22 importjava.util.HashMap;23 importjava.util.Properties;24 importjava.util.Set;25

26 importjavax.swing.JButton;27 importjavax.swing.JCheckBox;28 importjavax.swing.JComboBox;29 importjavax.swing.JFrame;30 importjavax.swing.JOptionPane;31 importjavax.swing.JScrollPane;32 importjavax.swing.JTextArea;33 importjavax.swing.JTextField;34

35 public class FeiqiuFrame extendsJFrame{36

37

38

39 privateContainer contentP;40 privateJTextArea msgArea;41 privateJTextField IPTxt;42 privateJButton qingkongBtn;43 privateJTextField wenbenTxt;44 privateJButton fasongBtn;45 privateString msg;46 privateSocket fromClient;47 privateString str3;48 privateJComboBox classComb;49 privateProperties pro;50 privateJCheckBox xuanze;51

52

53

54

55

56 publicFeiqiuFrame() {57 pro=newProperties();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 voidaddContent() {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 = newJTextArea();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 = newJTextField();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 = newJButton();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 = newJButton();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(newActionListener(){159

160 @Override161 public voidactionPerformed(ActionEvent e) {162 //TODO Auto-generated method stub

163 msgArea.setText("");164 }165

166 });167 this.fasongBtn.addActionListener(newActionListener(){168

169 @Override170 public voidactionPerformed(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+"\n"+getFormatDate()+"\n"+"我说:"+str2);182

183 Socket client = null;184 try{185 client = new Socket(IP, 9527);186 BufferedWriter bw = new BufferedWriter(newOutputStreamWriter(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 staticString getFormatDate()219 {220 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");221 returnsdf.format(Calendar.getInstance().getTime());222 }223

224 publicContainer getContentP() {225 returncontentP;226 }227

228 public voidsetContentP(Container contentP) {229 this.contentP =contentP;230 }231

232 publicJTextArea getMsgArea() {233 returnmsgArea;234 }235

236 public voidsetMsgArea(JTextArea msgArea) {237 this.msgArea =msgArea;238 }239

240 publicJTextField getIPTxt() {241 returnIPTxt;242 }243

244 public voidsetIPTxt(JTextField iPTxt) {245 IPTxt =iPTxt;246 }247

248 publicJButton getQingkongBtn() {249 returnqingkongBtn;250 }251

252 public voidsetQingkongBtn(JButton qingkongBtn) {253 this.qingkongBtn =qingkongBtn;254 }255

256

257 publicJComboBox getClassComb() {258 returnclassComb;259 }260

261 public voidsetClassComb(JComboBox classComb) {262 this.classComb =classComb;263 }264

265 publicJTextField getWenbenTxt() {266 returnwenbenTxt;267 }268

269 public voidsetWenbenTxt(JTextField wenbenTxt) {270 this.wenbenTxt =wenbenTxt;271 }272

273 publicJButton getFasongBtn() {274 returnfasongBtn;275 }276

277 public voidsetFasongBtn(JButton fasongBtn) {278 this.fasongBtn =fasongBtn;279 }280

281 publicString getMsg() {282 returnmsg;283 }284

285 public voidsetMsg(String msg) {286 this.msg =msg;287 }288

289 publicSocket getFromClient() {290 returnfromClient;291 }292

293 public voidsetFromClient(Socket fromClient) {294 this.fromClient =fromClient;295 }296

297 publicString getStr3() {298 returnstr3;299 }300

301 public voidsetStr3(String str3) {302 this.str3 =str3;303 }304

305

306 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值