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

📄 qqclient.java

📁 另外一个利用java实现的聊天程序源码。
💻 JAVA
字号:

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

public class QQClient{
	private JFrame fm;
	private JTextArea jta;
	private JComboBox jcb;
	private JTextField jtf;
	private JButton jb;
	private String name;
	
	public Socket s=null;
	
	public ObjectInputStream in=null;
	public ObjectOutputStream out=null;
	
	public QQClient(String n){
		this.name=n;
		this.fm=new JFrame("Chat Room  "+n);
		fm.getContentPane().setLayout(null);
		this.jta=new JTextArea(20,25);
		this.jta.setEditable(false);
		this.jcb=new JComboBox();
		jcb.setBounds(5, 5, 60, 27);
		this.jcb.addItem("All");
		
		JPanel p=new JPanel();
		p.setLayout(null);
		p.add(jcb);
		
		this.jtf=new JTextField(30);
		jtf.setBounds(71, 5, 311, 27);
		this.jtf.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				submit();
			}
		});
		fm.setSize(399,316);
		fm.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){ 
				exit();
				fm.dispose();
			}
		});
		final JScrollPane scrollPane = new JScrollPane(jta);
		scrollPane.setBounds(0, 0, 392, 236);
		fm.getContentPane().add(scrollPane);
		
		//JPanel p1=new JPanel();
	    p.setBounds(0, 236, 392, 47);
		p.add(jtf);
		
		fm.getContentPane().add(p);
		
		fm.setVisible(true);
	}
	
	public void connect() throws Exception{
		this.s=new Socket("127.0.0.1",7555);
		out=new ObjectOutputStream(this.s.getOutputStream());
		in=new ObjectInputStream(this.s.getInputStream());
		ServerInfo si=new ServerInfo(this.name+" entered!",this.name,ServerInfo.ADD);
		out.writeObject(si);
		out.flush(); 
	}
	
	public void output() {
		try{
			while(true){
				if (s.isClosed()){
					break;
				}
				else{
					Object o=in.readObject();
					if (o==null) continue;
					ServerInfo si=(ServerInfo)o;
					int type=si.getType();
					String user=si.getUser();
					String str=si.getContent();
					if (type==ServerInfo.ADD){
						this.jcb.addItem(user);
					}
					else if(type==ServerInfo.DEL){
						this.jcb.removeItem(user);
					}
					jta.append(str+"\n");
				}
				
			}
		}
		catch(Exception e){}
	}
	
	public void submit(){
		try{
			String str=this.jtf.getText();
			String user=(String)jcb.getSelectedItem();
			if (user==null) return;
			ServerInfo si=new ServerInfo(str,user,ServerInfo.INFO);
			out.writeObject(si);
			out.flush();
			this.jtf.setText("");
		}
		catch(Exception e){e.printStackTrace();}
	}
	
	public void exit() {
		try{
			ServerInfo si=new ServerInfo(this.name+" exit!",this.name,ServerInfo.DEL);
			out.writeObject(si);
			out.flush();
		}
		catch(Exception e){e.printStackTrace();}	
		if (s!=null){try{s.close();}catch(Exception e){}}
		if (in!=null){try{in.close();}catch(Exception e){}}
		if (out!=null){try{out.close();}catch(Exception e){}}
	}
	
	
	public static void main(String[] args) throws Exception{
		if (args.length==0) {
			System.out.println("Use java QQClient NAME");
			System.exit(0);
		}
		
		QQClient cc=new QQClient(args[0]);
		cc.connect();
		cc.output();
		
	}
}
	
	
	
	
	
	

⌨️ 快捷键说明

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