⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientframe.java~27~

📁 Java 聊天程序 有客户端和服务端。
💻 JAVA~27~
📖 第 1 页 / 共 2 页
字号:
                }
                else if (keyword.equalsIgnoreCase("QUIT")) {
                    //如果关键字是QUIT,则判断是服务器的关闭信息,
                    //用户需要用此信息来切断此次连接
                    String who = str.nextToken();
                    if (who.equalsIgnoreCase("SERVER")) {
                        //判断是否为服务器
                        chat2.append("服务器关闭!\n");
                        jTextField2.setText("");
                        list1.removeAll();
                        try {
                            listen = null;
                            socket.close();
                            socket = null;
                            stop();
                        }
                        catch (IOException e) {
                            chat2.append("Error: " + e+ "\n");
                        }
                    }
                    else {
                        chat2.append(who + "已经离开!\n");
}
                }
                else if (keyword.equalsIgnoreCase("WELCOME")) {
                    //WELCOME表示有用户登录
                    chat2.append("" + name +",登录成功!\n");
                    //播放上线声音
                    try {
                        InputStream in = new FileInputStream("online.au");
                        AudioStream as = new AudioStream(in);
                        AudioPlayer.player.start(as);
                    }
                    catch (FileNotFoundException ex1) {
                    }
                    catch (IOException ex1) {
                    }

                }
                else if (keyword.equalsIgnoreCase("FAULTNAME")) {
                    chat2.append("" + name +
                                 ",登录失败!\n");
                    if (socket != null) {
                        try {
                            socket.close();
                            socket = null;
                            name = null;
                            this.stop();
                        }
                        catch (IOException e) {
                            chat2.append("Error:" + e);
                        }
                    }
                }
            }
        } //结束run方法
    } //结束内部的Listen类

    //用户点击“发送”按钮的处理方法
    void jButton1_actionPerformed(ActionEvent e) {
        if (socket != null) {
            StringBuffer message = new
                StringBuffer("MSG:");
            //定义一个字符缓冲区存储发送信息
            //其中用MSG为关键字,让服务器识别为聊天信息
            //发送消息的形式为:
            //MSG:BROAD:(发送方):(具体的消息)
            //或者
            //MSG:(接收方):(发送方):(具体的消息)
            if (jRadioButton1.isSelected()) {
                //查看选择的发送方式为“广播发送”还是“选择发送”
                message.append("BROAD:");
                chat2.append("我对大家说:" + chat1.getText() + "\n");
            }
            else {
                message.append(list1.getSelectedItem()
                               + ":");
                chat2.append("我对" + list1.getSelectedItem() + "说:" +
                             chat1.getText() + "\n");
            }
            message.append(name + ":");
            message.append(chat1.getText());
            send(message.toString());

            //发送聊天信息
            chat1.setText("");
        } //结束if
    } //结束“发送”的处理方法

    //用户点击“退出”按钮的处理方法
    void jButton2_actionPerformed(ActionEvent e) {
        String ms = chat2.getText();

        Object[] options = {
            "确定", "保存记录退出", "取消"};
        int agr = JOptionPane.showOptionDialog(null,
                                               "你确定退出聊天室?",
                                               "警告",
                                               JOptionPane.YES_OPTION,
                                               JOptionPane.WARNING_MESSAGE, null,
                                               options, options[0]);
        if (agr == 0) {
            disconnect();
            System.exit(0);
        }
        else if (agr == 1) {
            File jilu = new File("messages.dat");

            if (jilu.exists()) {
                try {
                    FileOutputStream fout = new
                        FileOutputStream(
                            "messages.dat", true);
                    byte[] mo = ms.getBytes();

                    try {
                        fout.write(mo);
                        fout.close();
                    }
                    catch (IOException ex1) {
                        ex1.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "记录保存成功!");
                }
                catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
            }
            else {
                try {
                    jilu.createNewFile();
                    FileOutputStream fout = new
                        FileOutputStream(
                            "messages.dat", true);
                    byte[] mo = ms.getBytes();
                    fout.write(mo);
                    fout.close();

                }
                catch (IOException ex2) {
                    ex2.printStackTrace();
                }
                JOptionPane.showMessageDialog(null, "记录保存成功!");
            }
            disconnect();
            System.exit(0);
        }

    }

    public void jLabel3_ancestorAdded(AncestorEvent event) {
        /* class talk
             extends Thread {
             public void run() {
                 String a="0";
                 while (notEnd) {
                     jLabel3.setText("与 " + a + " 聊天中");
                     a=list1.getSelectedItem();
                     try {
                         Thread.sleep(1500);
                     }
                     catch (InterruptedException ex) {
                         ex.printStackTrace();
                     }
                 }
             }
         }

         new Thread(new talk()).start();
         */
    }

    public void Zt_actionPerformed(ActionEvent e) {
        if (Zt.getSelectedItem().equals("宋体")) {
            chat1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            chat2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
        }
        else if (Zt.getSelectedItem().equals("微软雅黑")) {
            chat1.setFont(new java.awt.Font("微软雅黑", Font.PLAIN, 12));
            chat2.setFont(new java.awt.Font("微软雅黑", Font.PLAIN, 12));
        }
        else if (Zt.getSelectedItem().equals("黑体")) {
            chat1.setFont(new java.awt.Font("黑体", Font.PLAIN, 12));
            chat2.setFont(new java.awt.Font("黑体", Font.PLAIN, 12));
        }
        else if (Zt.getSelectedItem().equals("隶书")) {
            chat1.setFont(new java.awt.Font("隶书", Font.PLAIN, 12));
            chat2.setFont(new java.awt.Font("隶书", Font.PLAIN, 12));
        }
    }

    public void Ys_actionPerformed(ActionEvent e) {
        if (Ys.getSelectedItem().equals("黑色")) {
            chat1.setForeground(Color.black);
            chat2.setForeground(Color.black);
        }
        else if (Ys.getSelectedItem().equals("红色")) {
            chat1.setForeground(Color.RED);
            chat2.setForeground(Color.RED);
        }
        else if (Ys.getSelectedItem().equals("黄色")) {
            chat1.setForeground(Color.YELLOW);
            chat2.setForeground(Color.YELLOW);
        }
        else if (Ys.getSelectedItem().equals("蓝色")) {
            chat1.setForeground(Color.BLUE);
            chat2.setForeground(Color.BLUE);
        }
    }

    public void Pf_actionPerformed(ActionEvent e) {
        if (Pf.getSelectedItem().equals("白色")) {

            jPanel3.setBackground(Color.WHITE);
            jPanel4.setBackground(Color.WHITE);
            jPanel5.setBackground(Color.WHITE);
            jPanel6.setBackground(Color.WHITE);
            jPanel7.setBackground(Color.WHITE);
            jPanel8.setBackground(Color.WHITE);
            jPanel10.setBackground(Color.WHITE);
            jPanel11.setBackground(Color.WHITE);

        }
        else if (Pf.getSelectedItem().equals("蓝色")) {
            jPanel3.setBackground(Color.BLUE);
            jPanel4.setBackground(Color.BLUE);
            jPanel5.setBackground(Color.BLUE);
            jPanel6.setBackground(Color.BLUE);
            jPanel7.setBackground(Color.BLUE);
            jPanel8.setBackground(Color.BLUE);
            jPanel10.setBackground(Color.BLUE);
            jPanel11.setBackground(Color.BLUE);
        }
        else if (Pf.getSelectedItem().equals("天蓝")) {
            jPanel3.setBackground(new Color(110, 130, 255));
            jPanel4.setBackground(new Color(110, 130, 255));
            jPanel5.setBackground(new Color(110, 130, 255));
            jPanel6.setBackground(new Color(110, 130, 255));
            jPanel7.setBackground(new Color(110, 130, 255));
            jPanel8.setBackground(new Color(110, 130, 255));
            jPanel10.setBackground(new Color(110, 130, 255));
            jPanel11.setBackground(new Color(110, 130, 255));

        }
        else if (Pf.getSelectedItem().equals("默认")) {
            jPanel3.setBackground(new Color(238, 238, 238));
            jPanel4.setBackground(new Color(238, 238, 238));
            jPanel5.setBackground(new Color(238, 238, 238));
            jPanel6.setBackground(new Color(238, 238, 238));
            jPanel7.setBackground(new Color(238, 238, 238));
            jPanel8.setBackground(new Color(238, 238, 238));
            jPanel10.setBackground(new Color(238, 238, 238));
            jPanel11.setBackground(new Color(238, 238, 238));

        }
    }

    public void jButton3_actionPerformed(ActionEvent e) {
        jiluFrame ss = new jiluFrame();
        Center.CenterWindow(ss);
        ss.setVisible(true);
    }

    public void list1_actionPerformed(ActionEvent e) {

    }

    public void list1_mouseClicked(MouseEvent e) {
        if (e.getModifiers() == MouseEvent.BUTTON1_MASK &&
            e.getClickCount() == 1) {
            jLabel3.setText("与" + list1.getSelectedItem() + "聊天中");
        }

    }
}

class ClientFrame_list1_actionAdapter
    implements ActionListener {
    private ClientFrame adaptee;
    ClientFrame_list1_actionAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.list1_actionPerformed(e);
    }
}

class ClientFrame_list1_mouseAdapter
    extends MouseAdapter {
    private ClientFrame adaptee;
    ClientFrame_list1_mouseAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseClicked(MouseEvent e) {
        adaptee.list1_mouseClicked(e);
    }
}

class ClientFrame_jButton3_actionAdapter
    implements ActionListener {
    private ClientFrame adaptee;
    ClientFrame_jButton3_actionAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton3_actionPerformed(e);
    }
}

class ClientFrame_Pf_actionAdapter
    implements ActionListener {
    private ClientFrame adaptee;
    ClientFrame_Pf_actionAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Pf_actionPerformed(e);
    }
}

class ClientFrame_Ys_actionAdapter
    implements ActionListener {
    private ClientFrame adaptee;
    ClientFrame_Ys_actionAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Ys_actionPerformed(e);
    }
}

class ClientFrame_Zt_actionAdapter
    implements ActionListener {
    private ClientFrame adaptee;
    ClientFrame_Zt_actionAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.Zt_actionPerformed(e);
    }
}

class ClientFrame_jLabel3_ancestorAdapter
    implements AncestorListener {
    private ClientFrame adaptee;
    ClientFrame_jLabel3_ancestorAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void ancestorAdded(AncestorEvent event) {
        adaptee.jLabel3_ancestorAdded(event);
    }

    public void ancestorRemoved(AncestorEvent event) {
    }

    public void ancestorMoved(AncestorEvent event) {
    }
}

class ClientFrame_jButton1_actionAdapter
    implements java.awt.event.ActionListener {
    ClientFrame adaptee;

    ClientFrame_jButton1_actionAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}

class ClientFrame_jButton2_actionAdapter
    implements java.awt.event.ActionListener {
    ClientFrame adaptee;

    ClientFrame_jButton2_actionAdapter(ClientFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -