- 论坛徽章:
- 0
|
关键字: 网络涂鸦板
接上篇网络聊天室涂鸦板之服务器端
源码包下载(如无法下载可到原文处下载,或者给我留言)
下面介绍客户端较重要的两个类
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 |
|