黑马程序员技术交流社区

标题: 网络聊天室 模拟程序 [打印本页]

作者: 杨华东    时间: 2012-10-13 00:34
标题: 网络聊天室 模拟程序
本帖最后由 杨华东 于 2012-10-13 00:40 编辑
  1. import java.awt.BorderLayout;
  2. import java.awt.Button;
  3. import java.awt.Color;
  4. import java.awt.Dialog;
  5. import java.awt.FlowLayout;
  6. import java.awt.Frame;
  7. import java.awt.Label;
  8. import java.awt.Panel;
  9. import java.awt.TextArea;
  10. import java.awt.TextField;
  11. import java.awt.Toolkit;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.KeyAdapter;
  15. import java.awt.event.KeyEvent;
  16. import java.awt.event.WindowAdapter;
  17. import java.awt.event.WindowEvent;
  18. import java.io.BufferedReader;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.FileWriter;
  23. import java.io.IOException;
  24. import java.net.DatagramPacket;
  25. import java.net.DatagramSocket;
  26. import java.net.InetAddress;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;
  29. import java.util.TimeZone;

  30. class GuiChat {
  31. private TextField textField;
  32. private TextArea sendTextArea;
  33. private TextArea receiveTextArea;
  34. private Frame frame;
  35. private Button b;
  36. private Dialog dia;
  37. private Label lb;
  38. public GuiChat() {
  39. frame = createFrame();
  40. frame.setBackground(Color.BLUE);
  41. Panel sendPanel = createSendPanel();
  42. Panel textPanel = createTextPanel();
  43. frame.add(textPanel); // 将textPanel放在frame的CENTER
  44. frame.add(sendPanel, BorderLayout.SOUTH); // 将sendPanel放在frame的SOUTH
  45. frame.setVisible(true);
  46. }
  47. private Panel createTextPanel() {
  48. Panel textPanel = new Panel();
  49. textPanel.setLayout(new BorderLayout()); // Panel默认是FlowLayout, 改为BorderLayout
  50. receiveTextArea = new TextArea();receiveTextArea.setBackground(Color.PINK);
  51. sendTextArea = new TextArea(7, 100); // 设置TextArea的行数为7行, 列数会因为边界布局忽略
  52. sendTextArea.setBackground(Color.PINK);
  53. receiveTextArea.setEditable(false); // 将receiveTextArea设置为不可编辑, 背景颜色会默认变为灰色
  54. //receiveTextArea.setBackground(Color.WHITE); // 设置receiveTextArea背景颜色为白色
  55. textPanel.add(receiveTextArea); // 放在Panel的CENTER
  56. textPanel.add(sendTextArea, BorderLayout.SOUTH); // 放在Panel的SOUTH

  57. sendTextArea.addKeyListener(new KeyAdapter() {
  58. public void keyPressed(KeyEvent e) {
  59. if (e.getKeyCode() == KeyEvent.VK_ENTER && e.isControlDown()) { // 判断按下的是CTRL+ENTER
  60. send();
  61. e.consume(); // 取消这次输入, 让回车失效
  62. }
  63. }
  64. });
  65. return textPanel;
  66. }
  67. private Panel createSendPanel() {
  68. Panel sendPanel = new Panel();
  69. textField = new TextField(25);
  70. textField.setBackground(Color.PINK);
  71. Button sendButton = new Button("发送");sendButton.setBackground(Color.YELLOW);
  72. Button clearButton = new Button("清屏");clearButton.setBackground(Color.YELLOW);
  73. Button logButton = new Button("聊天记录");logButton.setBackground(Color.YELLOW);
  74. Button clearjilu = new Button("清除记录");clearjilu.setBackground(Color.YELLOW);
  75. sendPanel.add(textField);
  76. sendPanel.add(sendButton);
  77. sendPanel.add(clearButton);
  78. sendPanel.add(logButton);
  79. sendPanel.add(clearjilu);
  80. clearjilu.addActionListener(new ActionListener(){
  81. public void actionPerformed(ActionEvent e) {
  82. File f = new File("c:\\Program Files\\");
  83. File[] names = f.listFiles();
  84. receiveTextArea.setText("");
  85. for(File name:names){
  86. if(name.getName()=="jilu")
  87. name.delete();
  88. }
  89. try {
  90. FileWriter fw = new FileWriter("C:\\Program Files\\jilu");
  91. fw.write("");
  92. } catch (IOException e1) {

  93. e1.printStackTrace();
  94. }
  95. }
  96. });
  97. sendButton.addActionListener(new ActionListener() {
  98. public void actionPerformed(ActionEvent e) {
  99. send();
  100. }
  101. });
  102. clearButton.addActionListener(new ActionListener() {
  103. public void actionPerformed(ActionEvent e) {
  104. receiveTextArea.setText("");
  105. }
  106. });
  107. logButton.addActionListener(new ActionListener() {
  108. public void actionPerformed(ActionEvent e) {
  109. loadLog();
  110. }
  111. });
  112. return sendPanel;
  113. }
  114. private void loadLog() {
  115. BufferedReader reader = null;
  116. try {
  117. reader = new BufferedReader(new FileReader("C:\\Program Files\\jilu.txt"));//
  118. StringBuilder sb = new StringBuilder();
  119. int ch;
  120. while ((ch = reader.read()) != -1)
  121. sb.append((char) ch);
  122. receiveTextArea.setText(sb.toString());
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. } finally {
  126. if (reader != null)
  127. try {
  128. reader.close();
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. }
  134. private void send() {
  135. String ip = textField.getText(); // 获取textField中的地址
  136. ip = "".equals(ip) ? "255.255.255.255" : ip; // 如果ip地址为空, 就改为255.255.255.255
  137. String content = sendTextArea.getText(); // 获取sendTextArea中的内容
  138. String head = getTime() + " 我 对 " + ("255.255.255.255".equals(ip) ? "所有人" : ip) + " 说:\r\n"; // 获取时间, 组装一个字符串显示
  139. receiveTextArea.append(head + content + "\r\n\r\n"); // 向receiveTextArea中追加内容

  140. sendMsg(ip, content); // 将content发送到ip

  141. saveLog(head + content + "\r\n\r\n");

  142. sendTextArea.setText(""); // 清除sendTextArea中的内容
  143. }
  144. private synchronized void saveLog(String s) {
  145. BufferedWriter writer = null;
  146. try {
  147. writer = new BufferedWriter(new FileWriter("C:\\Program Files\\jilu", true)); // true表示向文件追加
  148. writer.write(s);
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. } finally {
  152. if (writer != null)
  153. try {
  154. writer.close();
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. }
  158. }
  159. }
  160. private String getTime() {
  161. TimeZone.setDefault(TimeZone.getDefault());
  162. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  163. return format.format(new Date());
  164. }
  165. private void sendMsg(String ip, String content) {
  166. DatagramSocket socket = null;
  167. try {
  168. socket = new DatagramSocket();
  169. byte[] b = content.getBytes();
  170. if(b.length==0){
  171. Dialog dia=createDialog();
  172. dia.setVisible(true);
  173. }
  174. DatagramPacket packet = new DatagramPacket(b, b.length, InetAddress.getByName(ip), 8888);
  175. socket.send(packet);
  176. } catch (IOException e) {
  177. e.printStackTrace();
  178. } finally {
  179. if (socket != null)
  180. socket.close();
  181. }
  182. }
  183. private Dialog createDialog() {
  184. dia = new Dialog(frame,"消息提示",true);
  185. dia.setBackground(Color.GREEN);
  186. b = new Button("确定");
  187. b.setBackground(Color.BLUE);
  188. lb = new Label(" 不 能 输 入 空 消 息 ");
  189. dia.add(lb);
  190. dia.setBounds(300, 250, 300, 200);
  191. dia.setLayout(new FlowLayout());
  192. dia.add(b);
  193. b.addActionListener(new ActionListener(){
  194. public void actionPerformed(ActionEvent e) {
  195. dia.dispose();
  196. }
  197. });
  198. dia.addWindowListener(new WindowAdapter(){
  199. public void windowClosing(WindowEvent e){
  200. dia.dispose();
  201. }
  202. });
  203. return dia;
  204. }
  205. private Frame createFrame() {
  206. Frame frame = new Frame("华东---------模拟聊天室");
  207. frame.setSize(500, 600);
  208. frame.setLocation(300, 100);
  209. frame.setIconImage(Toolkit.getDefaultToolkit().createImage("G:\\DCIM\\.thumbnails\\1335314618830.jpg"));
  210. frame.addWindowListener(new WindowAdapter() {
  211. public void windowClosing(WindowEvent e) {
  212. e.getWindow().dispose();
  213. }
  214. });
  215. return frame;
  216. }
  217. public static void main(String[] args) {
  218. new GuiChat();
  219. }

  220. }
复制代码
个人小作品  望高手 能给我 改进一哈  那里不行的求指教。。。

最重要的是大家一起分享  静下心来。。。。。。有啥说啥
作者: 王亚运    时间: 2012-10-13 09:31
只能膜拜大牛们~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2