ClientPoint.java
上传用户:qaz007_wsx
上传日期:2022-06-21
资源大小:3k
文件大小:7k
源码类别:

ICQ/即时通讯

开发平台:

Java

  1. //ClientPoint.java
  2.   import   javax.swing.*;   
  3.   import   java.awt.event.*;   
  4.   import   java.io.*;   
  5.   import   java.net.*;   
  6.   import   java.awt.*;   
  7.  public   class   ClientPoint   extends   JFrame   implements   ActionListener,KeyListener{   
  8.       static    int   SERVERPORT=8088;   //端口
  9.       static    String   SERVERADDRESS="127.0.0.1";  // IP地址 
  10.       private   BufferedReader   cin=null;   
  11.       private   PrintWriter   cout=null;   
  12.       private   Socket   client=null;   
  13.       public    String   clientname;  
  14.        //初始化
  15.       private   JPanel   jPanel1=new   JPanel(); 
  16.       private   JPanel   jPanel2=new   JPanel();
  17.       private   JPanel   jPanel3=new   JPanel();
  18.       
  19.       private   JLabel JLabelname=new JLabel("用户名");
  20.       private   JLabel JLabelInfo=new JLabel("请输入信息");
  21.       private   JLabel JLabelPassword=new JLabel("密码");
  22.       
  23.       private   JTextField   jTextSendInfo=new   JTextField(25);
  24.       private   JTextField   jTextname=new   JTextField(10); 
  25.       private   JTextField   jTextPassword=new   JTextField(10);   
  26.       private   JTextArea   jTextGetInfo=new   JTextArea();       
  27.       public    JScrollPane   areaScrollPane=new   JScrollPane(jTextGetInfo);       
  28.       private   Button   Send=new   Button("发送");   
  29.       private   Button   Login=new   Button("登录");   
  30.        // 初始化界面 
  31.       public   ClientPoint(){       
  32.           
  33.           super("聊天室^_^");           
  34.           
  35.           enableEvents(AWTEvent.WINDOW_EVENT_MASK);           
  36.           setSize(480,450);  //设置整个聊天界面
  37.            
  38.           jPanel2.add(JLabelname ); 
  39.           jPanel2.add(jTextname);
  40.           jPanel2.add(JLabelPassword ); 
  41.           jPanel2.add(jTextPassword);
  42.           //按钮Login的设置
  43.           jPanel2.add(Login);
  44.           jPanel2.setBackground(new   Color(75,   234,   166));
  45.           jPanel2.setLayout(new FlowLayout());
  46.           this.getContentPane().add("North",jPanel2);
  47.           //背景和字体的设置
  48.           jTextGetInfo.setBackground(new   Color(122,150,223));   
  49.           jTextGetInfo.setFont(new   java.awt.Font("Dialog",   0,   15));   
  50.           jTextGetInfo.setForeground(new   Color(176,   42,   0));
  51.           //jPanel3.add(jTextGetInfo);
  52.           this.getContentPane().add("Center",jTextGetInfo);  
  53.           this.setResizable(false);
  54.            
  55.           jPanel1.add(JLabelInfo); 
  56.           jPanel1.add(jTextSendInfo); 
  57.           //按钮Send的设置
  58.           jPanel1.add(Send);   
  59.           jPanel1.setBackground(new   Color(75,   234,   166));
  60.           jPanel1.setLayout(new FlowLayout());
  61.           this.getContentPane().add("South",jPanel1);
  62.      
  63. areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);   
  64.           areaScrollPane.setAutoscrolls(true);   
  65.           jTextSendInfo.setText("");   
  66.           jPanel1.add("North",areaScrollPane);   
  67.           jTextGetInfo.setEditable(false);   
  68.           //开始监听
  69.           Send.addActionListener(this);   
  70.           jTextSendInfo.addKeyListener(this);   
  71.           Login.addActionListener(new   ActionListener()   
  72.                       {public   void   actionPerformed(ActionEvent   e){   
  73.                       if(!jTextname.getText().equals("")){   
  74.                               clientname=jTextname.getText();   
  75.                               connect();                             
  76.                               Login.setEnabled(false);   
  77.                               Send.setEnabled(true);   
  78.                               jTextname.setText("");   
  79.                       }   
  80.                       else   JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);   
  81.                       }});   
  82.           //this.setContentPane(jPanel1);   
  83.       }  
  84.     // 登录进入聊天室
  85.       public   void   connect(){   
  86.           try{   
  87.               client=new   Socket(SERVERADDRESS,SERVERPORT);                 
  88.                 cout=new   PrintWriter(client.getOutputStream());                       
  89.                     cin=new   BufferedReader(   
  90.                       new   InputStreamReader(client.getInputStream()));   
  91.                       cout.println("欢迎["+clientname+"]进入聊天室");   
  92.                       cout.flush();   
  93.                         Receiver   r=new   Receiver();   
  94.                       r.start();           
  95.       }catch(Exception   e){   
  96.         e.printStackTrace();   
  97.       }   
  98.   }   
  99.   protected   void   processWindowEvent(WindowEvent   e){   
  100.             if(e.getID()==WindowEvent.WINDOW_CLOSING){   
  101.                     quit();   
  102.             }   
  103.             super.processWindowEvent(e);   
  104.           }   
  105.       //主函数  
  106.           public   static   void   main(String[]   args){   
  107.               ClientPoint   cp=new   ClientPoint();   
  108.               cp.show();   
  109.                 
  110.           }   
  111.     //与服务器连接 
  112. class   Receiver   extends   Thread{   
  113.   public   void   run(){   
  114.   String   msg=null;   
  115.   JScrollBar   sb;   
  116.   try{   
  117.   msg=cin.readLine();   
  118.   while(true){   
  119.   jTextGetInfo.append(msg+"n");   
  120.   sb=areaScrollPane.getVerticalScrollBar();   
  121.   sb.setValue(sb.getMaximum()+50);   
  122.   jTextSendInfo.setEnabled(true);   
  123.   msg=cin.readLine();   
  124.   }   
  125.   }catch(Exception   e){   
  126.   Send.setEnabled(false);   
  127.   }   
  128.   }   
  129.   }  
  130. //离开聊天室   
  131.   void   quit(){   
  132.   try{   
  133.   cout.println("["+clientname+"]离开聊天室");   
  134.   cout.flush();   
  135.   cout.println("exit");   
  136.   cout.flush();   
  137.   System.out.print("lianjieduankai");   
  138.   cin.close();   
  139.   cout.close();   
  140.   client.close();   
  141.   }catch(Exception   e){}   
  142.   finally{System.exit(0);   
  143.   }   
  144.   }   
  145.   //处理和监听事件   
  146.   public   void   actionPerformed(ActionEvent   e){   
  147.                       if(!jTextSendInfo.getText().equals("")){   
  148.                       cout.println("["+clientname+"]:"+jTextSendInfo.getText());   
  149.                       cout.flush();                       
  150.                       jTextSendInfo.setText("");   
  151.                     jTextSendInfo.setFocusable(true);                       
  152.                           }   
  153.                           else   JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);   
  154.                           }   
  155.     
  156.   public   void   keyPressed(KeyEvent   e){}   
  157.   public   void   keyTyped(KeyEvent   e){}   
  158.   public   void   keyReleased(KeyEvent   e)   
  159.   {   
  160.   if   (e.getKeyCode()==KeyEvent.VK_ENTER)   
  161.   {   
  162.   if(!jTextSendInfo.getText().equals(""))   
  163.   {   
  164.   if(cout==null)   
  165.   {   
  166.   clientname=jTextSendInfo.getText();   
  167.   connect();                             
  168.   Login.setEnabled(false);   
  169.   Send.setEnabled(true);   
  170.   jTextSendInfo.setText("");   
  171.   }   
  172.   else   
  173.   {   
  174.   cout.println("["+clientname+"]:"+jTextSendInfo.getText());   
  175.   cout.flush();                       
  176.   jTextSendInfo.setText("");   
  177.   jTextSendInfo.setFocusable(true);     
  178.   }   
  179.   }   
  180.   else   
  181.   {   
  182.   if   (cout==null)   
  183.   {   
  184.   JOptionPane.showMessageDialog(null,"警告:用户名不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);   
  185.   }   
  186.   else   JOptionPane.showMessageDialog(null,"警告:消息不能为空!!","消息对话框",JOptionPane.WARNING_MESSAGE);   
  187.       }   
  188.     }   
  189.   }   
  190. }