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

📄 client_10.26.txt

📁 chatroom....JAVA实现
💻 TXT
字号:
//////////////////////////////////////////////////
//Client.java
//071026 11:42   hejia
//////////////////////////////////////////////////

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
//import java.util.*;
import javax.swing.*; 

public class Client
{  public static void main(String args[])
   {  new ComputerClient();
   }
}
class ComputerClient extends Frame implements Runnable,ActionListener,TextListener
{  Button connection,send;
   TextArea text1;
   TextField inputText,idText;
   
   Socket socket=null;
   DataInputStream in=null;
   DataOutputStream out=null;
   MyDialog dialog;
   
   Thread thread; 
   ComputerClient()
   { 
      socket=new Socket();
      setLayout(new FlowLayout());
      Box box=Box.createVerticalBox();
      //GridLayout grid =new new GridLayout(2,1)
      connection=new Button("开始聊天");
      send=new Button("发言");
      send.setEnabled(false);
      text1=new TextArea(10,30);
      inputText=new TextField(12);
      idText=new TextField(12);
      dialog=new MyDialog(this,"ID必须输入!",true);
      //dialog.setVisible(true);
      
      add(text1);
      box.add(connection);
      box.add(new Label("ID是必须的"));
      box.add(idText);
      box.add(new Label("注意用语文明"));
      box.add(inputText);
      box.add(send);
 
      
      connection.addActionListener(this);
      send.addActionListener(this);
      text1.addTextListener(this);
      
      thread=new Thread(this); 
      add(box);
      setBounds(80,80,440,230);
      setVisible(true);
      setResizable(false);
      validate();
      addWindowListener(new WindowAdapter()
                   {  public void windowClosing(WindowEvent e)
                            {  System.exit(0);
                            }
                   });
   }
   
   public void actionPerformed(ActionEvent e)
   { if(e.getSource()==connection)
      {  try  //请求和服务器建立套接字连接:
         { if(socket.isConnected())
              {} 
           else
              { InetAddress  address=InetAddress.getByName("127.0.0.1");
                InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
                socket.connect(socketAddress); 
                in =new DataInputStream(socket.getInputStream());
                out = new DataOutputStream(socket.getOutputStream());
                send.setEnabled(true);
                thread.start();
               }
         } 
         catch (IOException ee){}
      }
     if(e.getSource()==send)
      {  
        String s=inputText.getText();
        String ss=idText.getText();
        //text1.setText(ss);
        
         if(ss.length()<1){ //用ss==null判断是不行的,因为SS对象是存在的,要判断的应是SS中的内容
			dialog.setVisible(true); //对话框激活状态时,堵塞下面的语句
         }
         else 
                  
         if(s!=null)
         { 
           try { 
                  out.writeUTF(s);
                }
              catch(IOException e1){} 
           }               
      }
   }
   
   public void textValueChanged(TextEvent e){	
   	
   	 String s=text1.getText();

   }
   	
   	
   public void run()
   {  String s=null;
      while(true)
       {    try{  s=in.readUTF();
                 text1.setText(s);
               }
           catch(IOException e) 
               {  text1.setText("与服务器已断开");
                      break;
               }   
       }
  }
}


class MyDialog extends Dialog implements ActionListener  //建立对话框类
{ 

  // static final int YES=1,NO=0;
  // int message=-1; Button yes,no;
   MyDialog(Frame f,String s,boolean b) //构造方法
   {  super(f,s,b);
   
  //  yes=new Button("Yes"); yes.addActionListener(this);
  //  no=new Button("No");   no.addActionListener(this);
  //  setLayout(new FlowLayout());
  //  add(yes); add(no);
      add(new Label("ID必须输入!"));
      setBounds(160,160,100,100);
      addWindowListener(new WindowAdapter()
                      {   public void windowClosing(WindowEvent e)
                           { setVisible(false);
                           }
                      }
                   );
   }
   
   public void actionPerformed(ActionEvent e)
   {  
            
   }
   
   /* public void actionPerformed(ActionEvent e)
   {  if(e.getSource()==yes) 
      { message=YES;setVisible(false);
      }
     else if(e.getSource()==no)
      { message=NO;setVisible(false);
      }
   }
   
   public int getMessage()
   {  return message;
   }
   */
}

//对话中的信息单元;
class Info implements Serializable
//实现Serializable接口
{  String name=null;
   //double height; 
   String words;
   Info(String name,String words)
   { 
      this.name=name;
      this.word=word;
   }
   
   public void setWord (String word)
   {  this.word=word;
   }
}

⌨️ 快捷键说明

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