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

📄 clientchat.java

📁 客户端端聊天工具实现
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.util.*;
import java.awt.datatransfer.*;
import java.util.Date;
public class ClientChat extends JFrame implements ActionListener,Runnable
{
    public JFrame aFrame;
    public JTextArea aTextArea, bTextArea;
    public JLabel aLabel;
    public JTextField aTextField;
    public JButton aButton;
    public JPanel aPanel1,aPanel2,aPanel3;
    public JScrollPane aScrollPane,bScrollPane;

    Socket socket;
    InputStream Is;
    PrintStream PS;
    OutputStream Os;
    BufferedReader DIS; 
    String s;
String ClientName="";

    public ClientChat(String name)
    {
       super("ClientChat");
       
      
  this.ClientName=name;
setSize(400,300);
       show();
       this.ClientName=JOptionPane.showInputDialog(null,"Name");
      this.setTitle(this.ClientName);
       getContentPane().setLayout(new BorderLayout());
       aTextArea =new JTextArea();
       aTextArea.setBounds(20,20,300,300);
       bTextArea = new JTextArea();
       bTextArea.setBounds(0,0,200,200);
       aLabel =new JLabel("我的第一个聊天程序!!!!");
       aTextField = new JTextField(10);
       aButton =new JButton("发送");
aButton.addActionListener(this);
       aPanel1 =new JPanel();
       aPanel2 =new JPanel();
       aPanel3 = new JPanel();
       aPanel1.setLayout(new BorderLayout());
       aPanel2.setLayout(new BorderLayout());
       aPanel3.setLayout(new BorderLayout());
       aScrollPane =new JScrollPane(aTextArea);
       bScrollPane =new JScrollPane(bTextArea);
      
       getContentPane().add(aPanel1,BorderLayout.CENTER);
       getContentPane().add(aPanel2,BorderLayout.SOUTH);
       
      aPanel1.add(aScrollPane,BorderLayout.CENTER);
      aPanel1.add(aLabel,BorderLayout.SOUTH);
      aPanel3.add(aTextField,BorderLayout.WEST);
      aPanel3.add(aButton,BorderLayout.EAST);
      aPanel2.add(bScrollPane,BorderLayout.CENTER);
      aPanel2.add(aPanel3,BorderLayout.SOUTH);
       
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
startNet();
bTextArea.requestFocus();
    }
    public void startNet()
    {
      try
      {
         
        
          socket =new Socket(InetAddress.getLocalHost(),2020);
aTextField.setText(socket.getInetAddress().getHostAddress());
          Is= socket.getInputStream();
          Os= socket.getOutputStream();
          DIS=new BufferedReader(new InputStreamReader(Is));
          PS= new PrintStream(Os);
         Thread thread1=new Thread(this);
         thread1.start();
      }catch(Exception e){}
       }
     public void run()
     {

       try{
           int flag=0;
           while(flag==0)
           {
             s=DIS.readLine();
             aTextArea.append("server reply:"+"\n"+s+"\n");
             
             if(s.trim().equals("bye"))
             {
               flag=1;
               PS.println("bye");
               DIS.close();
               PS.close();
               Is.close();
               Os.close();
               socket.close();
               startNet();
             }
           }
          }catch(Exception e){}
       }

    public void actionPerformed(ActionEvent e)
    {
       

       if(e.getActionCommand()=="发送")
        try{
           PS.println(bTextArea.getText());

            }
            catch(Exception ex){
            System.out.println("Error:"+ex);
            }
        aTextArea.append("client said:"+"\n"+bTextArea.getText()+"\n");
        
        bTextArea.setText("");
     }
        
         
    public static void main(String[] args)
    {
       ClientChat c=new ClientChat("aa");
       
       c.startNet();
    }
}

⌨️ 快捷键说明

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