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

📄 client.java

📁 一个很好的JAVA聊天室系统 可以在课堂里实践并且进行聊天
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;


public class Client extends Applet
{
    Login login=null;
    TextField user=new TextField(20);
    TextField host=new TextField(20);
    TextArea log=new TextArea(10,60);
    TextField message=new TextField(40);
    Button connect=new Button("Connet");
    Button cancle1=new Button("Cancle");
    Dialog err;    
    Button cancle2=new Button();
    Socket chSocket=null;
    PrintStream os;
//    DataInputStream is;
    BufferedReader reader;
    InputStreamReader istrReader;

    public void start() { this.setVisible(true); }

    public void init()  {  initForm(); }

    String getData()
     {
      String fromServer=null;
      try
        {
          fromServer=reader.readLine();  // is.readLine();
        }
      catch(Exception e)
        {
          log.setText("Exception:"+e);
          System.out.println("Exception in getData:"+e);                     
          return null;
        }
      return fromServer;
     }

    boolean sendData(String toServer)
     {
      try
        {
          os.println(toServer);
          os.flush();
        }
      catch(Exception e) {
          log.setText("Exception:"+e);
          System.out.println("Exception in sendData:"+e);
          return false; }
      return true;
     }

    boolean connectHost(String hostName)  
     {
      try { chSocket=new Socket(hostName,1024);
            istrReader = new InputStreamReader(chSocket.getInputStream());
            reader=new BufferedReader(istrReader);
//            is=new DataInputStream(chSocket.getInputStream());
            os=new PrintStream(chSocket.getOutputStream());
          }
      catch(UnknownHostException e)
          {
            log.setText("Trying to connet to unknow Host:"+e);
            System.out.println("Trying to connet to unknow Host:"+e);
            return false;
          }
      catch(Exception e)
          {
            System.out.println(e.toString());
            return false;
          }
      return true;
     }

     void cancle1_mouseClicked(MouseEvent e) { System.exit(0); }

     void cancle2_mouseClicked(MouseEvent e)
         {
           err.dispose();
         }

    void initForm()
     {
       this.setSize(0,0);
       this.setBackground(Color.white);
          this.setLayout(new BorderLayout());
          this.setSize(300,150);
          this.setName("Client");
          Panel p1=new Panel();
	  p1.setLayout(new BorderLayout());
          this.add("center",p1);
          Panel p2=new Panel();
          p2.add("West",new Label("User Name"));
          user.setText("");
          p2.add("East",user);
          p1.add("North",p2);
          Panel p3=new Panel();
          p3.add("West",new Label("Host name"));
          host.setText("202.112.1.1");
          p3.add("East",host);
          p1.add("Center",p3);
          Panel p4=new Panel();
          p4.add(connect);
          p4.add(cancle1);
          p1.add("South",p4);
          log.setEditable(false);
          this.setLocation(200,200);
          cancle2.addMouseListener(new java.awt.event.MouseAdapter() { 
            public void mouseClicked(MouseEvent e) {
                   cancle2_mouseClicked(e); }
            });
          message.addActionListener(new java.awt.event.ActionListener()
            {
              public void actionPerformed(ActionEvent e)
                {
                  message_actionPerformed(e);
                }
            });
          connect.addMouseListener(new java.awt.event.MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
              connect_mouseClicked(e); }
            });
          cancle1.addMouseListener(new java.awt.event.MouseAdapter()
            {
              public void mouseClicked(MouseEvent e)
                {
                  cancle1_mouseClicked(e); }
            });
     }      
     void closeSocket() {
              try {
                    os.close();
                    reader.close();   // is.close();
                    chSocket.close(); }
              catch(Exception e)
                  {
                    log.setText("Exception:"+e);
                    System.out.println("Exception in closeData:"+e);
                   }
             }

     void connect_mouseClicked(MouseEvent e) 
             {
                if (!this.user.getText().equals(""))
                   if (connectHost(host.getText()))
                      {
		        this.removeAll();
                        login=new Login(this);
                        login.start();
                        this.setName("Client-user ["+user.getText()+"]");
                        this.setLayout(new BorderLayout());
                        Panel p1=new Panel();
                        p1.setLayout(new BorderLayout());
                        Panel p2=new Panel();
                        p2.add("Center",log);
                        p1.add("North",p2);
                        Panel p3=new Panel();
                        p3.add("West",new Label("Message"));
                        p3.add("Center",message);
                        p1.add("Center",p3);
                        this.add("Center",p1);
                        this.validate();
                        log.setText("");
                        message.setText("");
                        this.setVisible(true);
                        sendData(user.getText());
                      }  
                   else  
                      { showDialog("Can`t connect to Host:"+host.getText(),"OK"); }
             }

     void showDialog(String Warning,String label)
         {
                 err=new Dialog(new Frame(),"Error!",true);
                 err.setLayout(new BorderLayout());
                 Panel t1=new Panel();  
                 t1.setLayout(new BorderLayout());
                 t1.add("East",new Label("       "));
                 t1.add("West",new Label("       "));
                 t1.add("Center",cancle2);
                 cancle2.setLabel(label); 
                 err.setSize(200,100);
                 err.add("South",t1);
                 err.add("Center",new Label(Warning));
                 err.show();
         }
  
     void message_actionPerformed(ActionEvent e)
         { 
                 String msg=null;
                 msg=message.getText(); 
                 sendData(msg);
                 message.setText("");
                 if(msg.equals("\\bye"))
                   {
//                     login.stop();
                     closeSocket();  
                     System.exit(0); 
                   }        
         }

}
 
class Login extends Thread
   { 
     Client client=null;
     public Login(Client client) { this.client=client; }
     public void run()
           {
             String fromServer=null; 
             String msg=null;
             while((fromServer=client.getData())!=null)
                   client.log.append(fromServer+"\n");
           }

   }

⌨️ 快捷键说明

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