// \发送\按钮 public void sendMessage() {
Chat chatobj = new Chat(); chatobj.chatUser = strLoginName;
chatobj.chatMessage = txtMessage.getText();
chatobj.chatToUser = String.valueOf(cmbUser.getSelectedItem()); chatobj.whisper = chPrivateChat.isSelected() ? true : false; chatobj.emote = emote.getSelectedItem().toString(); // 向服务器发送信息 try { }
Socket toServer = new Socket(strServerIp, 1001);
ObjectOutputStream outObj = new ObjectOutputStream(toServer
.getOutputStream());
outObj.writeObject(chatobj);
txtMessage.setText(\); // 清空文本框 outObj.close(); toServer.close();
} catch (Exception e) {
} // sendMessage()结束
4.3.3.3 保存聊天记录
当用户需要保存聊天记录时可以直接点击“保存”按钮,为了防止多用户聊天记录文件冲突问题,聊天记录将会以“用户名_message.txt”为文件名,将信息保存在当前应用程序目录下。如图
保持模块程序:
- 25 -
// \保存\按钮 public void saveMessage() { try { FileOutputStream fileoutput = new FileOutputStream( this.strLoginName + \, true); String temp = taUserMessage.getText(); // System.out.println(temp); fileoutput.write(temp.getBytes()); fileoutput.close(); JOptionPane.showMessageDialog(null, \聊天记录保存在\ + this.strLoginName } + \); } catch (Exception e) { System.out.println(e); } 4.3.3.4 显示系统时间 当用户需要查看当前时间时,可以直接点击“时钟”按钮,将会启动一个漂亮的电子时钟,
时钟模块程序: public void actionPerformed(ActionEvent e) { timer.restart(); } public void paint( Graphics g ) { Insets insets = getInsets(); int L0 = (insets.left)/2, T0 = (insets.top)/2; - 26 -
int hh,mm,ss; String st; h=getSize().height; //绘制圆形 //获取时间 Calendar now=Calendar.getInstance(); hh=now.get(Calendar.HOUR_OF_DAY);//小时 mm=now.get(Calendar.MINUTE);//分钟 ss=now.get(Calendar.SECOND);// 秒 g.setColor(Color.pink); g.fillRect(L0,T0,60,28);//填充的矩形 g.setColor(Color.blue); if (hh < 10) st=\+hh; else st=\+hh; if (mm < 10) st=st+\+mm; else st=st+\+mm; if (ss < 10) st=st+\+ss; else st=st+\+ss; g.drawString(st,L0,T0+25); //计算时间和图形的关系 sdo=90-ss*6; mdo=90-mm*6; hdo=90-hh*30-mm/2; //擦除秒针 ??????????? //绘制秒针 g.setColor(Color.yellow); x=(int)((r-8)*Math.cos(RAD*sdo)+x0); y=(int)((r-8)*Math.sin(RAD*sdo)+y0)-2*T0; g.drawLine(x0,y0,x,(h-y)); old_X=x; old_Y=y; //绘制分针 line(g,mdo,(int)(r*0.7),Color.green); //绘制时针 line(g,hdo,(int)(r*0.5),Color.red); ???????????????????? } // end paint - 27 -
图4-22 屏蔽聊天信息
}
return fileString;
FileReader fileReader = new FileReader(files);
BufferedReader read = new BufferedReader(fileReader); while (true) { }
read.close();
// System.out.println(fileString); // TODO 自动生成 catch 块 e.printStackTrace(); // TODO 自动生成 catch 块 e.printStackTrace();
String line = read.readLine(); if (line == null) {
break;}
fileString += (line);
// fileString += (line + \
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
4.3.3.6 退出聊天系统
当前用户点击“退出”按钮或者关闭窗口时,系统将会把用户退出的信息打包成对象发送给服务器,并退出客户端程序。 // \退出\按钮 public void exit() { - 28 -
} Exit exit = new Exit(); exit.exitname = strLoginName; // 发送退出信息 try { } exit(); Socket toServer = new Socket(strServerIp, 1001); // 向服务器发送信息 ObjectOutputStream outObj = new ObjectOutputStream(toServer .getOutputStream()); outObj.writeObject(exit); outObj.close(); toServer.close(); frmChat.dispose(); // this.destroy(); } catch (Exception e) { private void exitChatRoom() {
4.3.3.7出现异常信息
在用户聊天时,当服务器关闭或者与服务器无法连接时,客户端将会给出“不能连接到服务器!”的信息。并在用户查看后退出客户端。
参考文献
[1] 孟凡荣.数据库原理与应用.中国矿业大学,2009(8)28-30
[2] 张虹.软件工程与软件开发工具.清华大学出版社.2009(1)55-75 [3] Bruce Eckel.Thinking in Java.机械工业出版社.2009(6)42-64
[4] 罗军舟等. TCP/IP协议及网络编程技术.清华大学出版社.2008(10) [5] 孙一林,彭波.Java网络编程实例.清华大学出版社.2003(4) [6] 李树青.Java通用范例开发金典.电子工业出版社.2009
[7] 王鹏.Java Swing 图形界面开发与案例详解.清华大学出版社.2008 [8] 庞永庆,翟鹏.Java完全自学宝典.清华大学出版社.2008 [9] 刘永华,于春花.Java网络编程.清华大学出版社.2008
[10] Boost Documentation,http://www.boost.org 或 http://boost.sourceforge.net [11] Boost.Asio Documentation,http://asio.sourceforge.net
- 29 -