用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

多用户在线聊天客户端

2013-10-22 作者: 免费源代码下载整理举报

[java]代码库

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

public class ChatClient{
	private String name;
	private Socket s;
	private DataInputStream dis;
	private DataOutputStream dos;
	private JFrame jf;
	private JTextArea jta;
	private JTextField jtf;
	private boolean runnable = true;
	
	public static void main(String args[]){
		ChatClient cc = new ChatClient();
		cc.createUI();
		cc.inputName();
		cc.connect();
		cc.createThread();
		
	}
	public void createUI(){
		jf = new JFrame("客户端");
		jta = new JTextArea();
		jta.setEditable(false);
		jtf = new JTextField();
		Button send = new Button("发送");
		Panel p = new Panel();
		p.setLayout(new BorderLayout());
		p.add(jtf,"Center");
		p.add(send,"East");
		jf.add(jta,"Center");
		jf.add(p,"South");
		
		MyClientListener listener = new MyClientListener(this);
		send.addActionListener(listener);
		jtf.addActionListener(listener);
		jf.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				ChatClient.this.shutDown();
			}
			});
		jf.setSize(400,400);
		jf.setLocation(600,0);
		jf.setVisible(true);
		jtf.requestFocus();
	}
	public void inputName(){
		String name = JOptionPane.showInputDialog("Input Your name:");
		this.setName(name);
		jf.setTitle(name);
	}
	public void connect(){
		try{
			s = new Socket("127.0.0.1",9999);
			dos = new DataOutputStream(s.getOutputStream());
			dis = new DataInputStream(s.getInputStream());
			dos.writeUTF(name);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void createThread(){
		MyClientReader reader = new MyClientReader(this);
		reader.start();
	}
	public void stop(){
		runnable = false;
	}
	public void shutDown(){
		try{
			dos.writeUTF("Bye");
			jta.append("Exit in 5 seconds");
			this.stop();
			Thread.sleep(5000);
			dis.close();
			dos.close();
			s.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		System.exit(0);
	}
	
	public boolean getRunnable(){
		return runnable;
	}
	public void setName(String name){
		this.name = name;
	}
	public DataInputStream getDataInputStream(){
		return dis;
	}
	public DataOutputStream getDataOutputStream(){
		return dos;
	}
	public JTextArea getTextArea(){
		return jta;
	}
	public JTextField getTextField(){
		return jtf;
	}
	
}

class MyClientListener implements ActionListener{
	private ChatClient client;
	public MyClientListener(ChatClient client){
		this.client = client;
	}
	public void actionPerformed(ActionEvent e){
		JTextField jtf = client.getTextField();
		String info = jtf.getText();
		try{
			client.getDataOutputStream().writeUTF(info);
		}catch(Exception e1){
			e1.printStackTrace();
		}
		if(info.equals("bye")){
			client.shutDown();
		}
		jtf.setText("");
		jtf.requestFocus();
	}
}

class MyClientReader extends Thread{
	private ChatClient client;
	public MyClientReader(ChatClient client){
		this.client = client;
	}
	
	public void run(){
		String info;
		DataInputStream dis = client.getDataInputStream();
		JTextArea jta = client.getTextArea();
		try{
			while(client.getRunnable()){
				info = dis.readUTF();
				jta.append(info+"\n");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...