ClientFrame.java
上传用户:hbgsjx
上传日期:2022-06-25
资源大小:3k
文件大小:5k
源码类别:

ICQ/即时通讯

开发平台:

Java

  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import javax.swing.border.*;
  8. public class ClientFrame extends JFrame
  9. {
  10. private JButton jbdenglu;
  11. private JButton jbxiaxian;
  12. private Socket socket;
  13. private JComboBox combobox;
  14. private JTextArea jtextarea;
  15.         private JScrollPane jp;
  16. private JTextField message;
  17. ObjectOutputStream output;
  18. ObjectInputStream input;
  19. private ClientThread clientthread;
  20.     public ClientFrame()
  21. {
  22. super("Client Frame");
  23. jbdenglu=new JButton("登陆");
  24. jbxiaxian=new JButton("下线");
  25. jbxiaxian.setEnabled(false);
  26. jbdenglu.addActionListener(new ActionListener()
  27. {
  28. public void actionPerformed(ActionEvent e)
  29. {
  30. denglu();
  31. }
  32. });
  33. jbxiaxian.addActionListener(new ActionListener()
  34. {
  35. public void actionPerformed(ActionEvent e)
  36. {
  37. xiaxian();
  38. }
  39. });
  40. combobox=new JComboBox();
  41. combobox.insertItemAt("all", 0);
  42. combobox.setSelectedIndex(0);
  43. jtextarea=new JTextArea(20,20);
  44. jtextarea.setEditable(false);
  45.                 jp=new JScrollPane(jtextarea);
  46. jtextarea.setText("请输入您的用户名");
  47. message=new JTextField(40);
  48. message.setEditable(true);
  49. message.addActionListener(new ActionListener()
  50. {
  51. public void actionPerformed(ActionEvent e)
  52. {
  53. sendmessage();
  54. }
  55. });
  56. this.addWindowListener(new WindowAdapter()
  57. {
  58. public void windowClosing(WindowEvent e)
  59. {
  60. xiaxian();
  61. System.exit(0);
  62. }
  63. });
  64. Container c=getContentPane();
  65. c.setLayout(null);
  66. jbdenglu.setBounds(30,30,90,30);
  67. jbxiaxian.setBounds(150,30,90,30);
  68. jp.setBounds(30,110,300,300);
  69. combobox.setBounds(30,425,90,30);
  70. message.setBounds(30,470,300,30);
  71. c.add(jbdenglu,null);
  72. c.add(jbxiaxian,null);
  73. c.add(jp,null);
  74. c.add(combobox,null);
  75. c.add(message,null);
  76. setSize(370,570);
  77. setVisible(true);
  78. }
  79.     public void denglu()
  80.     {
  81.      try
  82.      {
  83.      socket=new Socket("222.206.113.21",8000);
  84.      output=new ObjectOutputStream(socket.getOutputStream());
  85.      output.flush();
  86.      input=new ObjectInputStream(socket.getInputStream());
  87.      String msg=message.getText();
  88.      output.writeObject(msg);
  89.      output.flush();
  90.      clientthread=new ClientThread(socket,input,combobox,jtextarea);
  91.      clientthread.start();
  92.      jbdenglu.setEnabled(false);
  93.      jbxiaxian.setEnabled(true);
  94.      message.setEditable(true);
  95.      message.setText("");
  96.      jtextarea.setText("您已成功上线"+"n");
  97.      }catch(Exception e){}
  98.     }
  99.     public void xiaxian()
  100.     {
  101.      jbdenglu.setEnabled(true);
  102.      jbxiaxian.setEnabled(false);
  103.      jtextarea.setText("请输入您的用户名");
  104.      try
  105.      {
  106.      output.writeObject("closed");
  107.      output.flush();
  108.      output.writeObject("closed");
  109.      output.flush();
  110.      input.close();
  111.      output.close();
  112.      socket.close();
  113.      }catch(Exception e){}
  114.     }
  115.    public void sendmessage()
  116.     {
  117.      try
  118.      {
  119.      String tosombody=combobox.getSelectedItem().toString();
  120.      String msg=message.getText();
  121.      message.setText("");
  122.          output.writeObject(tosombody);
  123.          output.flush();
  124.          output.writeObject(msg);
  125.          output.flush();
  126.      }catch(Exception e){}
  127.     
  128.     }
  129.     public class ClientThread extends Thread
  130.     {
  131.      JComboBox combobox;
  132.      JTextArea jtextarea;
  133.      Socket socket;
  134.      ObjectInputStream input;
  135.      public ClientThread(Socket socket,ObjectInputStream input,JComboBox combobox,JTextArea jtextarea)
  136.      {
  137.      this.socket=socket;
  138.      this.input=input;
  139.      this.combobox=combobox;
  140.      this.jtextarea=jtextarea;
  141.      }
  142.      public void run()
  143.      {
  144.      try
  145.      {
  146.      while(true)
  147.      {
  148.          String st=(String)input.readObject();
  149.                                     if(st.equalsIgnoreCase("list"))
  150.                                     {
  151.                                        String userlist=(String)input.readObject();
  152.                                        String names[]=userlist.split("n");
  153.                                        combobox.removeAllItems();
  154.                                        int i=0;combobox.addItem("all");
  155.                                        while(i<names.length)
  156.                                        {
  157.                                           combobox.addItem(names[i]);i++;
  158.                                        }
  159.                                        combobox.setSelectedIndex(0);
  160.                                      }
  161.                                      else if(st.equalsIgnoreCase("message"))
  162.                                       {
  163.                                         jtextarea.append((String)input.readObject());
  164.                                        }
  165.                                     
  166.      }
  167.      }catch(Exception e){}
  168.      }
  169.     }
  170.     public static void main(String args[])
  171.     {
  172.      new ClientFrame(); 
  173.     }
  174. }