[Java] 多用户在线聊天客户端 →→→→→进入此内容的聊天室

来自 , 2019-09-16, 写在 Java, 查看 124 次.
URL http://www.code666.cn/view/02a32ad2
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.net.*;
  4. import java.awt.*;
  5. import javax.swing.*;
  6. import java.io.*;
  7. import java.awt.event.*;
  8.  
  9. public class ChatClient{
  10.         private String name;
  11.         private Socket s;
  12.         private DataInputStream dis;
  13.         private DataOutputStream dos;
  14.         private JFrame jf;
  15.         private JTextArea jta;
  16.         private JTextField jtf;
  17.         private boolean runnable = true;
  18.        
  19.         public static void main(String args[]){
  20.                 ChatClient cc = new ChatClient();
  21.                 cc.createUI();
  22.                 cc.inputName();
  23.                 cc.connect();
  24.                 cc.createThread();
  25.                
  26.         }
  27.         public void createUI(){
  28.                 jf = new JFrame("客户端");
  29.                 jta = new JTextArea();
  30.                 jta.setEditable(false);
  31.                 jtf = new JTextField();
  32.                 Button send = new Button("发送");
  33.                 Panel p = new Panel();
  34.                 p.setLayout(new BorderLayout());
  35.                 p.add(jtf,"Center");
  36.                 p.add(send,"East");
  37.                 jf.add(jta,"Center");
  38.                 jf.add(p,"South");
  39.                
  40.                 MyClientListener listener = new MyClientListener(this);
  41.                 send.addActionListener(listener);
  42.                 jtf.addActionListener(listener);
  43.                 jf.addWindowListener(new WindowAdapter(){
  44.                         public void windowClosing(WindowEvent e){
  45.                                 ChatClient.this.shutDown();
  46.                         }
  47.                         });
  48.                 jf.setSize(400,400);
  49.                 jf.setLocation(600,0);
  50.                 jf.setVisible(true);
  51.                 jtf.requestFocus();
  52.         }
  53.         public void inputName(){
  54.                 String name = JOptionPane.showInputDialog("Input Your name:");
  55.                 this.setName(name);
  56.                 jf.setTitle(name);
  57.         }
  58.         public void connect(){
  59.                 try{
  60.                         s = new Socket("127.0.0.1",9999);
  61.                         dos = new DataOutputStream(s.getOutputStream());
  62.                         dis = new DataInputStream(s.getInputStream());
  63.                         dos.writeUTF(name);
  64.                 }catch(Exception e){
  65.                         e.printStackTrace();
  66.                 }
  67.         }
  68.         public void createThread(){
  69.                 MyClientReader reader = new MyClientReader(this);
  70.                 reader.start();
  71.         }
  72.         public void stop(){
  73.                 runnable = false;
  74.         }
  75.         public void shutDown(){
  76.                 try{
  77.                         dos.writeUTF("Bye");
  78.                         jta.append("Exit in 5 seconds");
  79.                         this.stop();
  80.                         Thread.sleep(5000);
  81.                         dis.close();
  82.                         dos.close();
  83.                         s.close();
  84.                 }catch(Exception e){
  85.                         e.printStackTrace();
  86.                 }
  87.                 System.exit(0);
  88.         }
  89.        
  90.         public boolean getRunnable(){
  91.                 return runnable;
  92.         }
  93.         public void setName(String name){
  94.                 this.name = name;
  95.         }
  96.         public DataInputStream getDataInputStream(){
  97.                 return dis;
  98.         }
  99.         public DataOutputStream getDataOutputStream(){
  100.                 return dos;
  101.         }
  102.         public JTextArea getTextArea(){
  103.                 return jta;
  104.         }
  105.         public JTextField getTextField(){
  106.                 return jtf;
  107.         }
  108.        
  109. }
  110.  
  111. class MyClientListener implements ActionListener{
  112.         private ChatClient client;
  113.         public MyClientListener(ChatClient client){
  114.                 this.client = client;
  115.         }
  116.         public void actionPerformed(ActionEvent e){
  117.                 JTextField jtf = client.getTextField();
  118.                 String info = jtf.getText();
  119.                 try{
  120.                         client.getDataOutputStream().writeUTF(info);
  121.                 }catch(Exception e1){
  122.                         e1.printStackTrace();
  123.                 }
  124.                 if(info.equals("bye")){
  125.                         client.shutDown();
  126.                 }
  127.                 jtf.setText("");
  128.                 jtf.requestFocus();
  129.         }
  130. }
  131.  
  132. class MyClientReader extends Thread{
  133.         private ChatClient client;
  134.         public MyClientReader(ChatClient client){
  135.                 this.client = client;
  136.         }
  137.        
  138.         public void run(){
  139.                 String info;
  140.                 DataInputStream dis = client.getDataInputStream();
  141.                 JTextArea jta = client.getTextArea();
  142.                 try{
  143.                         while(client.getRunnable()){
  144.                                 info = dis.readUTF();
  145.                                 jta.append(info+"\n");
  146.                         }
  147.                 }catch(Exception e){
  148.                         e.printStackTrace();
  149.                 }
  150.         }
  151. }

回复 "多用户在线聊天客户端"

这儿你可以回复上面这条便签

captcha