刚写的聊天系统,拿来和大家探讨

来源:互联网 发布:搜狗输入法淘宝弹窗 编辑:程序博客网 时间:2024/05/01 02:06

功能不是很多,只是实现了所有在线用户的通话。点对点的通话已经基本想好怎么写了,但是还没有实现

下边是主要代码,希望大家一起探讨。

Code:
  1. /**  
  2.  * 用来设置字体属性  
  3.  *   
  4.  * @author 龚伟涛  
  5.  */  
  6. class MyAttribute extends SimpleAttributeSet {   
  7.   
  8.     private static final long serialVersionUID = -6771680451132779539L;   
  9.     String config;   
  10.   
  11.     public MyAttribute(String config) {   
  12.         this.config = config;   
  13.         setAttribute();   
  14.     }   
  15.   
  16.     /*  
  17.      * 设置属性  
  18.      */  
  19.     void setAttribute() {   
  20.         String[] configs = config.split(" ");   
  21.         if (!configs[0].equals("默认"))// 字体种族   
  22.             StyleConstants.FontConstants.setFontFamily(this, configs[0]);   
  23.         if ("斜体".equals(configs[1]))// 字体类型   
  24.             StyleConstants.FontConstants.setItalic(thistrue);   
  25.         else if ("粗体".equals(configs[1]))   
  26.             StyleConstants.FontConstants.setBold(thistrue);   
  27.         else if ("粗斜体".equals(configs[1])) {   
  28.             StyleConstants.FontConstants.setBold(thistrue);   
  29.             StyleConstants.FontConstants.setItalic(thistrue);   
  30.         }   
  31.         if (configs[2].equals("默认"))// 字体大小   
  32.             StyleConstants.FontConstants.setFontSize(this12);   
  33.         else  
  34.             StyleConstants.FontConstants.setFontSize(this, Integer   
  35.                     .parseInt(configs[2]));   
  36.         if (configs[3].equals("默认"))// 字体颜色   
  37.             StyleConstants.FontConstants.setForeground(this, Color.BLACK);   
  38.         else {   
  39.             if ("1".equals(configs[3]))   
  40.                 StyleConstants.FontConstants.setForeground(this, Color.RED);   
  41.             else if ("2".equals(configs[3]))   
  42.                 StyleConstants.FontConstants.setForeground(this, Color.BLUE);   
  43.             else if ("3".equals(configs[3]))   
  44.                 StyleConstants.FontConstants.setForeground(this, Color.YELLOW);   
  45.             else {   
  46.                 String[] s = configs[3].split(",");   
  47.                 StyleConstants.FontConstants.setForeground(thisnew Color(   
  48.                         Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer   
  49.                                 .parseInt(s[2])));   
  50.             }   
  51.         }   
  52.     }   
  53. }  
Code:
  1. txtNews.addKeyListener(new KeyAdapter() {//按回车提交信息   
  2.             @Override  
  3.             public void keyPressed(KeyEvent e) {   
  4.                 if (e.getKeyChar() == KeyEvent.VK_ENTER)   
  5.                     btnConfirm.doClick();   
  6.             }   
  7.         });   
  8.   
  9.   
  10.     @Override  
  11.     public void actionPerformed(ActionEvent e) {   
  12.         if (e.getSource() == btnConfirm) {   
  13.             String getNews = txtNews.getText().trim();   
  14.             if (getNews != null) {   
  15.                 config = configFont + " " + configStyle + " " + configSize   
  16.                         + " " + configColor;   
  17.                 try {   
  18.                     if (out != null)   
  19.                         out.writeUTF(getNews + "+" + config);   
  20.                 } catch (IOException e1) {   
  21.                 }   
  22.                 txtNews.setText("");   
  23.             }   
  24.         } else if (e.getSource() == btnClear) {   
  25.             txpNews.setText("");   
  26.         } else if (e.getSource() == cbxFont) {   
  27.             configFont = cbxFont.getSelectedItem().toString().trim();   
  28.         } else if (e.getSource() == cbxSize) {   
  29.             configSize = cbxSize.getSelectedItem().toString().trim();   
  30.         } else if (e.getSource() == cbxStyle) {   
  31.             configStyle = cbxStyle.getSelectedItem().toString().trim();   
  32.         } else if (e.getSource() == cbxColor) {   
  33.             String s = cbxColor.getSelectedItem().toString().trim();   
  34.             if (("红色".equals(s))) {   
  35.                 configColor = "1";   
  36.             } else if (("蓝色".equals(s))) {   
  37.                 configColor = "2";   
  38.             } else if (("黄色".equals(s))) {   
  39.                 configColor = "3";   
  40.             } else if (("自定义".equals(s))) {   
  41.                 Color c = JColorChooser.showDialog(this"自定义字体颜色"null);   
  42.                 configColor = c.getRed() + "," + c.getGreen() + ","  
  43.                         + c.getBlue();   
  44.             } else {   
  45.                 configColor = "默认";   
  46.             }   
  47.         } else if (e.getSource() == btnStart) {   
  48.             if (((JButton) e.getSource()).getText().equals("连接")) {   
  49.                 IP = JOptionPane.showInputDialog("请设置IP地址", IP);// 设置IP   
  50.                 String regex = "((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d)).((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d)).((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d)).((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d))";   
  51.                 if (!verEx(IP, regex)) {   
  52.                     JOptionPane.showMessageDialog(this"IP设置不符合规范");   
  53.                     return;   
  54.                 }   
  55.                 String s = JOptionPane.showInputDialog("请设置IP地址", port);// 设置端口   
  56.                 if (s.length() > 5) {   
  57.                     s = s.substring(06);   
  58.                 }   
  59.                 if (verEx(s, "//d*")) {   
  60.                     port = Integer.parseInt(s.trim());   
  61.                     if (port > 65535 || port < 1) {   
  62.                         JOptionPane.showMessageDialog(this,"端口号超出范围,请输入0~65535之间的整数。");   
  63.                         return;   
  64.                     }   
  65.                 } else {   
  66.                     JOptionPane.showMessageDialog(this,"端口号应该为整数,请输入0~65535之间的整数。");   
  67.                     return;   
  68.                 }   
  69.                 start = true;   
  70.             } else {   
  71.                 exit();   
  72.             }   
  73.         } else if (e.getSource() == btnStop) {   
  74.             exit();   
  75.         }   
  76.     }   
  77.   
  78.     /*  
  79.      * 其他支持方法  
  80.      */  
  81.     protected void exit() {// 退出系统   
  82.         try {   
  83.             socket.close();   
  84.         } catch (Exception e) {   
  85.         } finally {   
  86.             System.exit(0);   
  87.         }   
  88.     }   
  89.   
  90.     protected boolean verEx(String strVer, String strExp) {// 正则判断   
  91.         Pattern p = Pattern.compile(strExp);   
  92.         Matcher m = p.matcher(strVer);   
  93.         return m.matches();   
  94.     }   
  95.   
  96.     /*  
  97.      * 重写run()方法  
  98.      */  
  99.     @Override  
  100.     public void run() {   
  101.         String news = null;   
  102.         try {   
  103.             socket = new Socket(IP, port);// 实例化套接字   
  104.             in = new DataInputStream(socket.getInputStream());// 获取输入流   
  105.             out = new DataOutputStream(socket.getOutputStream());// 获取输出流   
  106.             btnStart.setText("已登录");   
  107.             btnStart.setEnabled(false);   
  108.         } catch (IOException e) {   
  109.             btnStart.setText("退出");   
  110.             txpNews.setText("未发现服务器。");   
  111.             btnConfirm.setEnabled(false);   
  112.             return;   
  113.         }   
  114.         while (true) {   
  115.             try {   
  116.                 news = in.readUTF();   
  117.                 int m = news.lastIndexOf("+");   
  118.                 int n = news.lastIndexOf(":");   
  119.                 String s = news.substring(m + 1);   
  120.                 doc = txpNews.getStyledDocument();   
  121.                 myAtt = new MyAttribute(s);   
  122.                 doc.insertString(doc.getLength(), news.substring(0, n) + " : ",   
  123.                         null);   
  124.                 doc.insertString(doc.getLength(), news.substring(n + 1, m)   
  125.                         + "/n", myAtt);   
  126.                 txpNews.setCaretPosition(doc.getLength());   
  127.             } catch (IOException e) {   
  128.                 txpNews.setText("与服务器已经断开。");   
  129.                 break;   
  130.             } catch (BadLocationException e) {   
  131.                 e.printStackTrace();   
  132.             }   
  133.         }   
  134.     }   
  135.   
  136.     /*  
  137.      * main方法  
  138.      */  
  139.     public static void main(String[] args) {   
  140.         MyClient mc = new MyClient();   
  141.         mc.setVisible(true);   
  142.         while (mc.start == false) {// 等待客户设置服务器IP和端口   
  143.             try {   
  144.                 Thread.sleep(1000);   
  145.             } catch (InterruptedException e) {   
  146.                 e.printStackTrace();   
  147.             }   
  148.         }   
  149.         Thread thread = new Thread(mc);   
  150.         thread.start();   
  151.     }  
Code:
  1. /**  
  2.  * 服务器线程类  
  3.  */  
  4. class ServerThread extends Thread {   
  5.     Server server;   
  6.     Socket client;   
  7.     DataOutputStream out = null;   
  8.     DataInputStream in = null;   
  9.     Hashtable<String, ServerThread> clientList = null;   
  10.     String name = null;   
  11.   
  12.     ServerThread(Socket client, Hashtable<String, ServerThread> clientList,   
  13.             Server frame, int count) {   
  14.         server = frame;   
  15.         this.clientList = clientList;   
  16.         this.client = client;   
  17.         name = "游客" + count;   
  18.         try {   
  19.             in = new DataInputStream(this.client.getInputStream());   
  20.             out = new DataOutputStream(this.client.getOutputStream());   
  21.         } catch (IOException e) {   
  22.         }   
  23.     }   
  24.   
  25.     public void run() {   
  26.         while (true) {   
  27.             String str;   
  28.             try {   
  29.                 str = in.readUTF();// 读取到该线程发送的消息,这里可以实现某些词句的屏蔽,可以使用replace方法   
  30.                 this.out.writeUTF(name + ":" + str);   
  31.                 // Set<Entry<String, ServerThread>> s = clientList.entrySet();   
  32.                 // Iterator<Entry<String, ServerThread>> client1 = s.iterator();   
  33.                 // while (client1.hasNext()) {//将该线程发送的消息转发给其他在线的人   
  34.                 // ServerThread nextThread = client1.next().getValue();   
  35.                 // if (nextThread.name != this.name)   
  36.                 // nextThread.out.writeUTF(name + ":" + str);   
  37.                 // }   
  38.                 Enumeration<ServerThread> client = clientList.elements();   
  39.                 while (client.hasMoreElements()) {// 将该线程发送的消息转发给其他在线的人   
  40.                     ServerThread nextThread = client.nextElement();   
  41.                     if (nextThread.name != this.name)   
  42.                         nextThread.out.writeUTF(name + ":" + str);   
  43.                 }   
  44.             } catch (IOException e) {   
  45.                 server.txtConnNews.append("IP:    "  
  46.                         + client.getInetAddress().toString().substring(1)   
  47.                         + "    已断开服务器。    " + GetDate.getDate() + "/n");   
  48.                 clientList.remove(name);   
  49.                 try {// 关闭流   
  50.                     if (null != out)   
  51.                         out.close();   
  52.                 } catch (IOException e1) {   
  53.                 } finally {   
  54.                     try {   
  55.                         if (null != in)   
  56.                             in.close();   
  57.                     } catch (IOException e1) {   
  58.                         e1.printStackTrace();   
  59.                     } finally {   
  60.                         try {   
  61.                             if (null != this.client)   
  62.                                 this.client.close();   
  63.                         } catch (IOException e1) {   
  64.                             e1.printStackTrace();   
  65.                         }   
  66.                     }   
  67.                 }   
  68.                 break;   
  69.             }   
  70.         }   
  71.     }   
  72. }  

 

原创粉丝点击