chatroom.java

来自「一个简单聊天室实现,不错的哦」· Java 代码 · 共 60 行

JAVA
60
字号
import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;import java.applet.*;public class ChatRoom extends Applet implements Runnable,ActionListener{ Button button;TextField text1;TextArea text2;  Socket socket;  DataInputStream in;  DataOutputStream out;  Thread thread;  public void init() {setBackground(new Color(113,163,139));  setLayout(new BorderLayout());  button=new Button("send");text1=new TextField(12);  text2=new TextArea();  Panel p=new Panel();p.add(text1);p.add(button);  add("Center",text2);add("South",p);  button.addActionListener(this); } public void start() { try   {socket = new Socket(this.getCodeBase().getHost(), 4331);   in = new DataInputStream(socket.getInputStream());   out = new DataOutputStream(socket.getOutputStream());   }    catch (IOException e){}  if (thread == null)  {thread = new Thread(this);  thread.setPriority(Thread.MIN_PRIORITY);  thread.start();  } } public void run()  {String s1=null;    while(true)     { try{s1=in.readUTF();// 通过使用in读取服务器放入"线路"里的信息          }       catch (IOException e) {}       if(s1.equals("bye"))        {try{socket.close();break;}         catch (IOException e) {}           }       text2.append(s1+"\n");     }   }  public void actionPerformed(ActionEvent e)  {if (e.getSource()==button)     { String s=text1.getText();       if(s!=null)		              { try{out.writeUTF(s);}          catch(IOException e1){}         }                      else        { try{out.writeUTF("请说话");}          catch(IOException e1){}         }      }  }}

⌨️ 快捷键说明

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