ClientFrame.java
资源名称:javachat.rar [点击查看]
上传用户:hbgsjx
上传日期:2022-06-25
资源大小:3k
文件大小:5k
源码类别:
ICQ/即时通讯
开发平台:
Java
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.border.*;
- public class ClientFrame extends JFrame
- {
- private JButton jbdenglu;
- private JButton jbxiaxian;
- private Socket socket;
- private JComboBox combobox;
- private JTextArea jtextarea;
- private JScrollPane jp;
- private JTextField message;
- ObjectOutputStream output;
- ObjectInputStream input;
- private ClientThread clientthread;
- public ClientFrame()
- {
- super("Client Frame");
- jbdenglu=new JButton("登陆");
- jbxiaxian=new JButton("下线");
- jbxiaxian.setEnabled(false);
- jbdenglu.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- denglu();
- }
- });
- jbxiaxian.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- xiaxian();
- }
- });
- combobox=new JComboBox();
- combobox.insertItemAt("all", 0);
- combobox.setSelectedIndex(0);
- jtextarea=new JTextArea(20,20);
- jtextarea.setEditable(false);
- jp=new JScrollPane(jtextarea);
- jtextarea.setText("请输入您的用户名");
- message=new JTextField(40);
- message.setEditable(true);
- message.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- sendmessage();
- }
- });
- this.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- xiaxian();
- System.exit(0);
- }
- });
- Container c=getContentPane();
- c.setLayout(null);
- jbdenglu.setBounds(30,30,90,30);
- jbxiaxian.setBounds(150,30,90,30);
- jp.setBounds(30,110,300,300);
- combobox.setBounds(30,425,90,30);
- message.setBounds(30,470,300,30);
- c.add(jbdenglu,null);
- c.add(jbxiaxian,null);
- c.add(jp,null);
- c.add(combobox,null);
- c.add(message,null);
- setSize(370,570);
- setVisible(true);
- }
- public void denglu()
- {
- try
- {
- socket=new Socket("222.206.113.21",8000);
- output=new ObjectOutputStream(socket.getOutputStream());
- output.flush();
- input=new ObjectInputStream(socket.getInputStream());
- String msg=message.getText();
- output.writeObject(msg);
- output.flush();
- clientthread=new ClientThread(socket,input,combobox,jtextarea);
- clientthread.start();
- jbdenglu.setEnabled(false);
- jbxiaxian.setEnabled(true);
- message.setEditable(true);
- message.setText("");
- jtextarea.setText("您已成功上线"+"n");
- }catch(Exception e){}
- }
- public void xiaxian()
- {
- jbdenglu.setEnabled(true);
- jbxiaxian.setEnabled(false);
- jtextarea.setText("请输入您的用户名");
- try
- {
- output.writeObject("closed");
- output.flush();
- output.writeObject("closed");
- output.flush();
- input.close();
- output.close();
- socket.close();
- }catch(Exception e){}
- }
- public void sendmessage()
- {
- try
- {
- String tosombody=combobox.getSelectedItem().toString();
- String msg=message.getText();
- message.setText("");
- output.writeObject(tosombody);
- output.flush();
- output.writeObject(msg);
- output.flush();
- }catch(Exception e){}
- }
- public class ClientThread extends Thread
- {
- JComboBox combobox;
- JTextArea jtextarea;
- Socket socket;
- ObjectInputStream input;
- public ClientThread(Socket socket,ObjectInputStream input,JComboBox combobox,JTextArea jtextarea)
- {
- this.socket=socket;
- this.input=input;
- this.combobox=combobox;
- this.jtextarea=jtextarea;
- }
- public void run()
- {
- try
- {
- while(true)
- {
- String st=(String)input.readObject();
- if(st.equalsIgnoreCase("list"))
- {
- String userlist=(String)input.readObject();
- String names[]=userlist.split("n");
- combobox.removeAllItems();
- int i=0;combobox.addItem("all");
- while(i<names.length)
- {
- combobox.addItem(names[i]);i++;
- }
- combobox.setSelectedIndex(0);
- }
- else if(st.equalsIgnoreCase("message"))
- {
- jtextarea.append((String)input.readObject());
- }
- }
- }catch(Exception e){}
- }
- }
- public static void main(String args[])
- {
- new ClientFrame();
- }
- }