免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1218 | 回复: 0
打印 上一主题 下一主题

网络聊天室涂鸦板之客户端 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-06 19:38 |只看该作者 |倒序浏览

                                                关键字: 网络涂鸦板
  
  
  
   
接上篇网络聊天室涂鸦板之服务器端
源码包下载(如无法下载可到原文处下载,或者给我留言)
下面介绍客户端较重要的两个类
ClientThread.java
先看一下它的构造函数
               
               
                public ClientThread(ObjectInputStream ois,ClientFrame cf){
        this.ois=ois;
        this.cf=cf;
    }
其中的ois参数是负责接收服务器端发来的信息的,而cf是客户端另一个重要类。很简单。下面是run方法
# public void run(){  
#         String s = "";  
#         try {  
#             while (true) {  
#                 Object o = cf.read();  
#                 Thread tt;  
#                 if (o instanceof String) {  
#                     String msg = (String) o;  
#                     if (msg.equals("deny")) {       //私聊惨遭拒绝  
#                         JOptionPane.showMessageDialog(cf, "you have been denied");  
#                     } else if (msg.equals("successsave")) {  
#                         JOptionPane.showMessageDialog(cf, "you have successfully save your message");  
#                     } else {  
#                         s += (String) o + "\n";  
#                         cf.jTextPaneChatArea.setText(s);  
#                     }  
#                 } else if (o instanceof javax.swing.ImageIcon) {  //如果是收到的图像,则画在绘图板上  
#                     javax.swing.ImageIcon i = (javax.swing.ImageIcon) o;  
#                     Image image = i.getImage();  
#                     ToolkitImage ti = (ToolkitImage) image;  
#                     cf.drawComponent.drawPanel.setImage(ti.getBufferedImage());  
#                     cf.repaint();  
#                 } else if (o instanceof Vector) {       //在线用户列表  
#                     cf.setJList((Vector) (o));  
#                 } else if (o instanceof InetAddress) {
//后面还有很多,可以到源码包里看
   
这段代码也很简单,其实就是接受信息然后分类处理。
  
ClientFrame.java
这是客户端的界面,同时也负责很多业务,所以是最大的一个类,下面看一下部分代码
# public void actionPerformed(ActionEvent e) {  
#      if (e.getSource() == this.jButtonSent) {  
#          s = "speak to " + this.sendTo + ": " + this.jTextPaneUser.getText() + "  + FormatDate.nowDate();  
#          t = new Thread(this.runnable);  
#          t.start();  
#          try {  
#              Thread.sleep(300);  
#              t.stop();  
#          } catch (Exception ex) {  
#          }  
#      } else if (e.getSource() == this.buttonFont) {  
#          FontDialog fd = new FontDialog(this.jTextPaneChatArea.getFont(), this);  
#          fd.setVisible(true);  
#          this.jTextPaneChatArea.setFont(fd.getChoosedFont());  
#          this.jTextPaneUser.setFont(fd.getChoosedFont());  
#      } else if (e.getSource() == this.buttonPrivate) {  
#          this.sendTo = (String) (this.jListUserList.getSelectedValue());  
#          if (this.sendTo == null) {  
#              JOptionPane.showMessageDialog(this, "no person is selected");  
#          } else if (this.sendTo.equals("ALLPERSON")) {  
#              JOptionPane.showMessageDialog(this, "you cannot private chat with allperson");  
#          } else {  
#              s = "private" + this.jListUserList.getSelectedValue();  
#              t = new Thread(this.runnable);  
#              t.start();  
#              try {  
#                  Thread.sleep(300);  
#                  t.stop();  
#              } catch (Exception ex) {  
#              }  
#          }  
#      } else if (e.getActionCommand().equals("hide")) {  
#          s = "iwanthide";  
#          this.buttonHideShow.setLabel("show");  
#          t = new Thread(this.runnable);  
#          t.start();  
#          try {  
#              Thread.sleep(300);  
#              t.stop();  
#          } catch (Exception ex) {  
#          }  
#      } else if (e.getActionCommand().equals("show")) {  
上面就是能干点事的代码,很简单的处理事件,然后发送信息给ServerThread。
好了,到这主要部分就说完了。只是今天有点失眠,就写了篇文章,把之前做过的一个小东西拿出来献丑了,有很多的bug。还望高手指点啊。附件是我的源码,没经过优化,有点乱。用netbeans6.0以上版本可以成功导入,以下版本会出现乱码。

               
               
               
                 
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/75329/showart_1110996.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP