Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20094
  • 博文数量: 12
  • 博客积分: 640
  • 博客等级: 上士
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-05 15:38
文章分类

全部博文(12)

文章存档

2011年(1)

2008年(11)

我的朋友
最近访客

分类: Java

2008-08-05 17:03:03

作者:   链接:http://wolfplanet.javaeye.com/blog/223269  发表时间: 2008年08月02日
下面介绍客户端较重要的两个类

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) {   // 这里是接受IP地址,然后与其连接建立私聊通道

                    final Object oo = o;

                    tt = new Thread(new Runnable() {



                        public void run() {

                            PrivateClient pr = new PrivateClient((InetAddress) oo);

                            

                        }

                        });

                    tt.start();

                } else if (o instanceof String[]) {   //这是有人来请求与自己私聊

                    String[] msg = (String[]) o;

                    if (msg[0].equals("request")) {

                        PrivateDialog pd = new PrivateDialog(msg[1], cf);

                        if (pd.getAccept()) {

                            String[] sent = {"iaccept", msg[1]};

                            cf.write(sent);

                            final String nameOfClient = msg[1];

                            tt = new Thread(new Runnable() {



                                public void run() {

                                    PrivateServer pr = new PrivateServer(nameOfClient, cf.getMyName());

                                }

                                });

                            tt.start();

                        }

                    }else if(msg[0].equals("sendMusic")){

                        URL file1 = getClass().getResource(msg[1]);

                        this.cf.audioClip=java.applet.Applet.newAudioClip(file1);

                        JOptionPane.showMessageDialog(this.cf,"some one have pick you a music,enjoy it!");

                    }

                    else if (msg[0].equals("checkmessage")) {

                        String sMess = cf.jTextPaneChatArea.getText() + "****这是你上次的聊天记录****\n" + msg[1] + "\n****聊天记录结束****\n";

                        cf.jTextPaneChatArea.setText(sMess);

                    } else {

                        String[] sent = {"ideny", msg[1]};

                        cf.write(sent);

                    }

                }

            }



        } catch (Exception e) {

            s += "Error";

            cf.jTextPaneChatArea.setText(s);

        }

    }

 这段代码也很简单,其实就是接受信息然后分类处理。

 

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")) {

            s = "iwantshow";

            this.buttonHideShow.setLabel("hide");

            t = new Thread(this.runnable);

            t.start();

            try {

                Thread.sleep(300);

                t.stop();

            } catch (Exception ex) {

            }

        } else if (e.getSource() == this.buttonCheck) {

            s = "iwantcheck";

            t = new Thread(this.runnable);

            t.start();

            try {

                Thread.sleep(300);

                t.stop();

            } catch (Exception ex) {

            }

        } else if (e.getSource() == this.buttonSave) {

            s = "iwantsave" + this.jTextPaneChatArea.getText();

            t = new Thread(this.runnable);

            t.start();

            try {

                Thread.sleep(300);

                t.stop();

            } catch (Exception ex) {

            }

        } else if (e.getSource() == this.buttonSaveLocal) {

            if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {

                final String fileName = fileChooser.getSelectedFile().getAbsolutePath();

                Thread tt = new Thread(new Runnable() {



                    public void run() {

                        try {

                            PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));

                            pw.write(jTextPaneChatArea.getText());

                            pw.close();

                            JOptionPane.showMessageDialog(ClientFrame.this, "you have successfully save your message to local file");

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }

                });

                tt.start();

                try {

                    Thread.sleep(300);

                    tt.stop();

                } catch (Exception ex) {

                }

            }



        } else if (e.getSource() == this.buttonListenMusic) {

            if (this.audioClip == null) {

                JOptionPane.showMessageDialog(this, "util now you haven't receive music");

            } else {

                if (this.firstAudio) {

                    audioThread.start();

                    this.firstAudio = false;

                } else {

                    try {

                        audioThread.stop();

                        Thread.sleep(100);

                        audioThread = new Thread(new Runnable() {



                            public void run() {

                                new AudioPlayDialog(audioClip);

                            }

                        });

                        audioThread.start();

                    } catch (Exception et) {

                        et.printStackTrace();

                    }

                }

            }

        } else if (e.getSource() == this.buttonSendMusic) {

            String name = (String) this.jListUserList.getSelectedValue();

            if (name == null) {

                JOptionPane.showMessageDialog(this, "you haven't choosen any person!");

            } else {

                ChooseMusicDialog mDialog = new ChooseMusicDialog(this);

                String music = mDialog.getMusic();

                if (music == null) {

                } else {

                    s = "sendMusic" + music + name;

                    t = new Thread(this.runnable);

                    t.start();

                    try {

                        Thread.sleep(300);

                        t.stop();

                    } catch (Exception ex) {

                    }

                }

            }

        }

    }

 

上面就是能干点事的代码,很简单的处理事件,然后发送信息给ServerThread。

 

好了,到这主要部分就说完了。只是今天有点失眠,就写了篇文章,把之前做过的一个小东西拿出来献丑了,有很多的bug。还望高手指点啊。附件是我的源码,没经过优化,有点乱。用netbeans6.0以上版本可以成功导入,以下版本会出现乱码。

 

源码

阅读(557) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~