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

📄 client.java

📁 JAVA网络聊天室.运用网络编程、文件操作、数据流处理以及多线程等基础技术编写一个网络聊天室.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                             show.setForeground(Color.BLUE);
                             break;
                    case 4: show.setBackground(Color.BLACK);
                             show.setForeground(Color.GREEN);
                             break;
                    case 5: show.setBackground(Color.cyan);
                             show.setForeground(Color.MAGENTA);
                             break;
                    case 6: show.setBackground(Color.LIGHT_GRAY);
                             show.setForeground(Color.BLUE);
                    default: cor = 0;
                }
                cor++;
            }
        }
        void cnet(String UsrName) 
        {
            connect(UsrName); //调用connect()连接服务器进行通话
        }
    }
    
    /****************************************菜单设置监听*********************************************/
    class JMenuSet implements ActionListener
    {
        public void actionPerformed(ActionEvent E)
        {
            if("常用短语".equals(E.getActionCommand()))
            {
                JOptionPane.showMessageDialog(null, "暂无常用短语可用!", "常用短语", JOptionPane.ERROR_MESSAGE);
            }
            else if ("复制记录".equals(E.getActionCommand()))
            {
                show.requestFocus();
                show.selectAll();
                show.copy();
            }
            else if ("记录另存为".equals(E.getActionCommand()))
            {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.showSaveDialog(null);
                String dir = fileChooser.getCurrentDirectory().toString();
                String tempFileName = fileChooser.getSelectedFile().getName();
                try
                {
                    BufferedWriter writeOut = new BufferedWriter(new FileWriter(dir+"/"+tempFileName));
                    String[] tempcontent = show.getText().split("\\n");
                    for(int i = 0 ;i<tempcontent.length;i++){
                    writeOut.write(tempcontent[i]) ;
                    System.out.println(tempcontent[i])  ;
                    writeOut.newLine() ;
                    }
                    writeOut.close();
                }
                catch(Exception ex)
                {
                    System.out.println("文件存取发生异常");
                }
            }
            else if("设置显示".equals(E.getActionCommand()))
            {
                MyFont myFont = new MyFont();
            }
            else if("关于..".equals(E.getActionCommand()))
            {
                new AboutSoft();
            }
            else if("退出".equals(E.getActionCommand()))
            {
                System.exit(0);
            }
        }
    }
    
    class AboutSoft extends JFrame
    {
        JFrame aboutFrame ;
        JTextArea aboutShow;
        AboutSoft()
        {
            aboutFrame = new JFrame("关于本软件");
            aboutShow = new JTextArea("", 10, 10);
            aboutShow.setBounds( 0 , 0 , 320 , 300 ) ;
            aboutFrame.setSize(320, 250);
            aboutFrame.setLayout(null);
            aboutShow.setBackground(Color.BLACK);
            aboutShow.setForeground(Color.CYAN);
            aboutShow.setText("\n\tGUET \n\n\tComputer and Control College -Software Engineering\n\n\t课程设计: 网络聊天室系统\n\n\t制    作:  NightSnow -xxxxxxxxxx\n\n\t指导教师:  LPS");
            aboutShow.setVisible(true);
            aboutFrame.add(aboutShow);

            aboutFrame.setVisible(true);
            aboutFrame.addWindowListener(new WindowAdapter() 
            { 
                public void windowClosing(WindowEvent E)
                {
                    aboutFrame.setVisible(false);
                } 
            }); 
            aboutShow.setEditable(false);
        }
    }
    
    /****************************************设置文本字体*********************************************/
    class MyFont extends JDialog implements ItemListener
    {
        private JComboBox jc1,jc2;
        String str="";
        GraphicsEnvironment E = GraphicsEnvironment.getLocalGraphicsEnvironment(); //获取系统字体
        String []name=E.getAvailableFontFamilyNames();                                     
        String []size={"2","4","6","8","10","12","14","16","18","20","22","24","26","28","30","32"};
        MyFont()
        {
            super.setLayout(null);
            super.setBounds(400,400,250,100);
            super.setTitle("设置文本字体");
            jc1 = new JComboBox(name);
            jc1.setBounds(5,5,90,20);
            jc1.addItemListener(this);
            jc2 = new JComboBox(size);
            jc2.setBounds(100,5,90,20);
            jc2.addItemListener(this);
            add(jc1);
            add(jc2);
            super.setVisible(true);
            super.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
        }
        public void itemStateChanged(ItemEvent E) 
        {
            if(E.getStateChange() == ItemEvent.SELECTED)
            {
                int a=Integer.parseInt(size[jc2.getSelectedIndex()]);
                show.setFont(new Font(jc1.getSelectedItem().toString(),Font.BOLD,a));   //*********  设置文本字体
            }
        }
    }
    /*************************************回车输出事件************************************************/
    class inputAction implements ActionListener
    {
        public void actionPerformed(ActionEvent E)
        {
            String str = input.getText().trim();
            input.setText("");
            SendMes(str);
        }
    }
    /***********************************向服务端发送信息**********************************************/
    public void SendMes(String str)
    {
        try
        {
            dos.writeUTF(str);
            dos.flush();
        }
        catch (IOException E)
        {
            E.printStackTrace();
        }
    }
    /************************************接收服务器发送的信息******************************************/
    private class RecvClient implements Runnable
    {
        public void run()
        {
            try
            {
                while (bConnect)
                {
                    String str = dis.readUTF();
                    show.setText(show.getText() + str + '\n');
                }
            }
            catch (IOException E)
            {
                E.printStackTrace();
            }
        }
    }
    /**********************************CONNECTION BEGIN************************************************/
    public void connect(String UsrName)
    {
        try
        {
            s = new Socket(tIp.getText(),Integer.parseInt(tPort.getText()));
            dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(s.getInputStream());
            bConnect = true;
            new Thread(new RecvClient()).start();
            System.out.println("成功登陆");
            JOptionPane.showMessageDialog(null, "欢迎进入夜雪聊天室!\n愿你在这片自由的天地玩得开心!", "成功登陆!", JOptionPane.CLOSED_OPTION);

            this.UsrName = UsrName;
            setTitle("夜雪聊天室-[" + UsrName + "]");

        }
        catch (UnknownHostException UHE)
        {
            UHE.printStackTrace();
        }
        catch (IOException E)
        {
            E.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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