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

📄 chatroom.java

📁 一个简单聊天室实现,不错的哦
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -