首页
java在线聊天室chart类的创建
日期:2021-10-25 11:42  点击:506
java在线聊天室chart类的创建
package aaa;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.*;

public class chart extends JFrame  {
	//定义全局变量或成员变量
	private JButton send=new JButton("发送");
	private JTextArea inputTextArea=new JTextArea(5,50);
	private JTextArea showTextArea=new JTextArea(10,50);
	private JComboBox userlist=new JComboBox();
	private JPanel up=new JPanel();
	private JPanel center=new JPanel();
	private JPanel down=new JPanel();
	private Socket client;
	private InetAddress ips;
	private int ports=8080;
	private String user;
	
	chart(String user){
		this.user=user;
		gui();//图形
		listion();//第一次连接服务器
		flashuser();//等服务器发信息给客户
		send();//发送聊天记录
	}
	
	//图形界面
	void gui(){
		JFrame f=new JFrame("聊天室");
		f.setSize(800,400);
		f.setLocation(300,200);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setLayout(new BorderLayout());
		f.add(up,BorderLayout.NORTH);
		f.add(down,BorderLayout.SOUTH);
		f.add(center,BorderLayout.CENTER);
		up.add(new JScrollPane(showTextArea));
		showTextArea.setEditable(false);
		center.add(inputTextArea);
		down.add(userlist);
		down.add(send);
		userlist.addItem("所有人");
		f.setVisible(true);
	}
	
	//连接服务器
	void listion(){
		try {
			ips=InetAddress.getByName("127.0.0.1");
			client=new Socket(ips,ports);
			PrintWriter writer = new PrintWriter(client.getOutputStream());
			String a=user+"@@"+"reg";
			writer.println(a);  
	        writer.flush(); 
			
		}catch(Exception e1) {
				e1.printStackTrace();
		}
	}
	
	void flashuser(){	
		new Thread() {	   
			public void run() {
				while(true) {	
					System.out.println(client);
					if(client!=null) {
					    try {
							BufferedReader r = new BufferedReader(new InputStreamReader(client.getInputStream())); 
							String line=r.readLine();
							String[] arr=line.split("@");
							//null@aaaa@userlist
							if(arr[arr.length-1].equals("userlist")) {
								for(String user:arr) {
									if(!user.equals("null")&&!user.equals("userlist")) {
									userlist.addItem(user);	
								    }
								}
							}else if(arr[arr.length-1].equals("群发聊天")) {
									showTextArea.append(arr[0]+arr[1]+"\n");
							}else {
								    showTextArea.append(arr[0]+arr[1]+arr[2]+"\n");
							}
					   }catch(Exception e) {
							e.printStackTrace();
					   }
	               }
	           }
			}
	   }.start();
	}
	
	void send() {
		//发送聊天内容
		send.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				try {
					PrintWriter writer = new PrintWriter(client.getOutputStream());
					String a=user+"@"+inputTextArea.getText()+"@"+userlist.getSelectedItem();
					writer.println(a);  
					writer.flush(); 
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
	}

	public static void main(String[] args) {
		new chart("zhangsan");

	}

}

上一篇· java弹窗JDialog对话框    下一篇·java在线聊天室服务端Server类的创建
最新文章
热门文章
关于网站  |  联系我们  |  网站地图  | 
2021-10-25 11:42
Powered by edu63.com
首页 刷新 顶部