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

📄 cha01cli.java

📁 网络聊天室
💻 JAVA
字号:
/* * cha01cli.java * * Created on 2005年6月17日, 下午12:14 */import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import java.util.Hashtable;public class cha01cli extends Frame implements ActionListener, Runnable{    Socket  socket=null;    DataInputStream in=null;     DataOutputStream out=null;     Hashtable listTable;                       Thread thread;            InputNameTextField 用户提交昵称界面=null;    ChatArea 用户聊天界面=null;            Label 提示条;    Panel P_north_n,north, center;        Label L_服务器,L_端口;    TextField TF_服务器,TF_端口;    Button  B_连接=null;        /** Creates a new instance of cha01cli */    public cha01cli() {                setTitle("聊天室客户端");            setBackground(new Color(255, 255, 200));                listTable=new Hashtable();                 int width=getSize().width;                                   int height=getSize().height;        //System.out.println(Integer.toString(height));        //System.out.println(Integer.toString(width));        width=660;        height=410;                //System.out.println(valueOf(height));           用户提交昵称界面=new  InputNameTextField(listTable);        int h=用户提交昵称界面.getSize().height;        用户聊天界面=new ChatArea("",listTable,width,height-(h+5));        用户聊天界面.setVisible(true);        //用户聊天界面.setVisible(false);        提示条=new Label("准备                                                   ",Label.LEFT);                L_服务器=new Label("服务器:");        L_端口=new Label("端口:");        TF_服务器=new TextField(16);        TF_端口=new TextField(5);        TF_端口.setText("8888");        TF_端口.setEditable(false);            B_连接=new Button("连接");        B_连接.addActionListener(this);                提示条.setForeground(Color.blue);        P_north_n=new Panel(new FlowLayout(FlowLayout.LEFT));        north=new Panel(new BorderLayout());        center=new Panel();        P_north_n.add(L_服务器);         P_north_n.add(TF_服务器);        P_north_n.add(L_端口);         P_north_n.add(TF_端口);        P_north_n.add(B_连接);        P_north_n.add(提示条);        north.add(P_north_n,BorderLayout.NORTH);        north.add(用户提交昵称界面,BorderLayout.CENTER);        center.add(用户聊天界面);        add(north,BorderLayout.NORTH);        add(center,BorderLayout.CENTER);                addWindowListener(new java.awt.event.WindowAdapter() {            public void windowClosing(java.awt.event.WindowEvent evt) {            if (socket!=null){               try{                   socket.close();                   thread=null;                }                catch(IOException e)                 {                  }            }                        exitForm(evt);            }        });        setVisible(true);        setBounds(60,40,660,460);        validate();    }    private void exitForm(java.awt.event.WindowEvent evt) {                    System.exit(0);    }      public static void main(String[] args) {        new cha01cli();    }        public void run() {        while(thread!=null)        {           if(用户提交昵称界面.get能否聊天()==true)             {                用户聊天界面.setVisible(true);                用户聊天界面.setName(用户提交昵称界面.getName());                用户聊天界面.setSocketConnection(socket,in,out);                提示条.setText("祝聊天愉快!");                //center.setVisible(true);                center.validate();                break;             }           try               {                 Thread.sleep(100);               }           catch(Exception e)               {               }        }}        public void actionPerformed(java.awt.event.ActionEvent e) {        //System.out.println("Item--c/s");           if(e.getSource()==B_连接)           {              if(socket!=null)//&&in!=null&&out!=null)            {  try                  {                    socket.close();                    in.close();                     out.close();                    //用户聊天界面.setVisible(false);                  }              catch(Exception ee)                  {                  }            }         String S_ip=TF_服务器.getText().trim();                  try            {             提示条.setText("正在连接到服务器,请稍等...");             socket = new Socket(S_ip, 8888);             in=new DataInputStream(socket.getInputStream());             out=new DataOutputStream(socket.getOutputStream());            }         catch (IOException ee)            {               提示条.setText("连接失败,请检查服务器是否打开");            }         if(socket!=null)            {             InetAddress address=socket.getInetAddress();             提示条.setText("连接:"+address+"成功");             用户提交昵称界面.setSocketConnection(socket,in,out);             north.validate();            }          if(thread==null)            {              thread=new Thread(this);              thread.start();            }        }    }}

⌨️ 快捷键说明

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