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

📄 inputnametextfield.java

📁 用java开发的网络聊天室
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package client;/** * * @author Administrator */import java.awt.*;import java.net.*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;public class InputNameTextField extends Panel implements ActionListener,Runnable{  TextField nameFile=null;                         String name=null;  Checkbox male=null,female=null;                  CheckboxGroup group=null;  Button  进入聊天室=null,退出聊天室=null;  Socket socket=null;                              DataInputStream in=null;                         DataOutputStream out=null;                       Thread thread=null;                              boolean 能否聊天=false;  Hashtable listTable;                                public InputNameTextField(Hashtable listTable)  {    this.listTable=listTable;    nameFile=new TextField(10);    group=new CheckboxGroup();    male=new Checkbox("男",true,group);    female=new Checkbox("女",false,group);    进入聊天室=new Button("进入");    退出聊天室=new Button("退出");    进入聊天室.addActionListener(this);    退出聊天室.addActionListener(this);    thread=new Thread(this);    add(new Label("昵称:"));    add(nameFile);     add(male);     add(female);    add(进入聊天室);    add(退出聊天室);    退出聊天室.setEnabled(false);  } public void set能否聊天(boolean b)  {   能否聊天=b;  } public boolean get能否聊天()  {    return 能否聊天;  } public String getName()  {   return name;  } public void setName(String s)  {    name=s;  } public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)  {          this.socket=socket;       this.in=in;       this.out=out;      try{           thread.start();         }       catch(Exception e)         {          nameFile.setText(""+e);         }  } public Socket getSocket()  {    return socket;  }  public void actionPerformed(ActionEvent e)  {    if(e.getSource()==进入聊天室)        {          退出聊天室.setEnabled(true);        if(能否聊天==true)         {           nameFile.setText("您正在聊天:"+name);         }       else         {           this.setName(nameFile.getText());            String sex=group.getSelectedCheckbox().getLabel();           if(socket!=null&&name!=null)             {               try{                    out.writeUTF("姓名:"+name+"性别:"+sex);                         }               catch(IOException ee)                  {                    nameFile.setText("没有连通服务器"+ee);                  }             }         }     }   if(e.getSource()==退出聊天室)      {       try          {             out.writeUTF("用户离开:");          }       catch(IOException ee)          {          }     }       } public void run()  {    String message=null;     while(true)       {          if(in!=null)           {              try                 {                   message=in.readUTF();                 }              catch(IOException e)                 {                    nameFile.setText("和服务器断开"+e);                 }           }         if(message.startsWith("可以聊天:"))           {             能否聊天=true;             break;           }          else if(message.startsWith("聊天者:"))           {             String people=message.substring(message.indexOf(":")+1);             listTable.put(people,people);                          }         else if(message.startsWith("不可以聊天:"))           {             能否聊天=false;             nameFile.setText("该昵称已被占用");           }       }  }}

⌨️ 快捷键说明

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