Java-項目-000-多人聊天帶界面(界面利用原生swing)

Java-項目-000-多人聊天帶界面(界面利用原生swing)-2020-6-25

零、最終效果

在這裏插入圖片描述

一、Client.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
import java.util.Vector;
public class Client extends JFrame implements ActionListener {
	JLabel jL1,jL2,jL3;
	JButton jB1,jB2,jB3;
	JTextField jF1,jF2,jF3,jF4;
	JTextArea jT1;
	JList<Client> list;
	Socket socket;
	BufferedReader cin;
	PrintStream cout;
	String clientName="";
	public Client() {
		super("多用戶客戶端");
		jL1=new JLabel("服務器IP");
		jL1.setBounds(15, 5, 60, 30);
		jF1=new JTextField("127.0.0.1",15);
		jF1.setBounds(85, 5, 100, 30);
		jL2=new JLabel("端口");
		jL2.setBounds(200, 5, 40, 30);
		jF2=new JTextField("4321",15);
		jF2.setBounds(245, 5, 60, 30);
		jL3=new JLabel("客戶名稱");
		jL3.setBounds(310, 5, 60, 30);
		jF3=new JTextField("",15);
		jF3.setBounds(375,5,60,30);
		jB1=new JButton("連接服務器");
		jB1.setBounds(440, 5, 100, 30);
		jB1.addActionListener(this);
		jF4=new JTextField(" ",30);
		jF4.setBounds(15,45,310,30);
		jB2=new JButton("發送信息");
		jB2.setBounds(335, 45, 100, 30);
		jB2.addActionListener(this);
		jB3=new JButton("斷開連接");
		jB3.setBounds(440, 45, 100,30);
		jB3.addActionListener(this);
		list=new JList();
		JScrollPane jS1=new JScrollPane(list);
		jS1.setBounds(15, 85, 250, 120);
		jT1=new JTextArea();
		JScrollPane jS2=new JScrollPane(jT1);
		jS2.setBounds(275, 85, 250, 120);
		Container w1Container=this.getContentPane();
		w1Container.setLayout(null);
		w1Container.add(jL1);
		w1Container.add(jL2);
		w1Container.add(jL3);
		w1Container.add(jB1);
		w1Container.add(jB2);
		w1Container.add(jB3);
		w1Container.add(jF1);
		w1Container.add(jF2);
		w1Container.add(jF3);
		w1Container.add(jF4);
		w1Container.add(jS1);
		w1Container.add(jS2);
		this.setSize(560,250);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		Client w1=new Client();

	}
	class ReadMessageThread extends Thread{
		public void run() {
			String line="";
			while(true) {
				try {
					line=cin.readLine();
				}catch(IOException e) {
					jT1.append("輸入輸出異常\n");
				}
				StringTokenizer st=new StringTokenizer(line,":");
				String keyword=st.nextToken();
				if(keyword.equalsIgnoreCase("QUIT")) {
					try {
						socket.close();
						jT1.append("接收到服務器同意斷開信息,套接字關閉\n");
					}catch(IOException e) {
						jT1.append("套接字關閉異常\n");
					}
					this.stop();
				}else if(keyword.equalsIgnoreCase("PEOPLE")) {
					Vector imessage=new Vector();
//					while(st.hasMoreTokens()) {
//					imessage.addElement(st.countTokens());}
					while (st.hasMoreTokens())
						imessage.add(st.nextToken());
					System.out.println(imessage);
					list.setListData(imessage);
				}else {
					String message=st.nextToken("\0");
					message=message.substring(1);
					jT1.append(message+"\n");
				}
			}
		}
		}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==jB1) {
			try {
				InetAddress ip=InetAddress.getByName(jF1.getText());
				int port=Integer.parseInt(jF2.getText());
				socket=new Socket(ip,port);
				jT1.append("系統提示:與服務器開始連接……\n");
			}catch(IOException e1) {
				jT1.append("服務器端口打開錯誤\n");
			}
			if(socket!=null) {
				jT1.append("系統提示:與服務器連接成功……\n");
				clientName=jF3.getText().trim();
				try {
					cin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
					cout=new PrintStream(socket.getOutputStream());
					String str="PEOPLE:"+clientName;
					System.out.println(str);
					cout.println(str);
					ReadMessageThread readThread=new ReadMessageThread();
					readThread.start();
				}catch(IOException e3) {
					jT1.append("輸入輸出異常\n");
				}
			}
			
		}
		if(e.getSource()==jB2) {
			String str=jF4.getText();
			str="MSG:"+clientName+":"+str;
			cout.println(str);
			
		}
		if(e.getSource()==jB3) {
			String str="QUIT";
			cout.println(str);
			jT1.append("客戶請求斷開連接\n");
		}
	}

}

二、Server.java


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Vector;
import java.util.StringTokenizer;
public class Server extends JFrame implements ActionListener{
	JLabel jL1,jL2,jL3;
	JButton jB1;
	JTextField jF1;
	JTextArea jT1,jT2;
	ServerSocket server;
	static Vector<Client> clients=new Vector();
	public Server() {
		super("多用戶服務器端");
		jL1=new JLabel("監聽端口");
		jL1.setBounds(15, 5, 60, 30);
		jF1=new JTextField("4321",15);
		jF1.setBounds(85, 5, 100, 30);
		jB1=new JButton("開始監聽");
		jB1.setBounds(200, 5, 100, 30);
		jB1.addActionListener(this);
		jL2=new JLabel("連接信息");
		jL2.setBounds(15, 45, 60, 30);
		jT1=new JTextArea();
		JScrollPane jS1=new JScrollPane(jT1);
		jS1.setBounds(15, 75, 310, 100);
		jL3=new JLabel("系統信息");
		jL3.setBounds(15, 175, 60, 30);
		jT2=new JTextArea();
		JScrollPane jS2=new JScrollPane(jT2);
		jS2.setBounds(15, 205, 310, 100);
		Container wlContainer=this.getContentPane();
		wlContainer.setLayout(null);
		wlContainer.add(jL1);
		wlContainer.add(jL2);
		wlContainer.add(jL3);
		wlContainer.add(jB1);
		wlContainer.add(jF1);
		wlContainer.add(jS1);
		wlContainer.add(jS2);		
		this.setSize(350, 350);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		Server wl=new Server();

	}
	class ConnectSocket extends Thread{
		Socket socket;
		public void run() {
			while(true) {
				try {
					socket=server.accept();
				}catch(IOException e2) {
					jT1.append("客戶連接失敗\n");
				}
				Client c=new Client(socket);
				clients.addElement(c);
				if(checkName(c)) {
					c.start();
					notifyRoom();
				}
				else {
					disconnect(c);
				}
			}
		}
	}
	class Client extends Thread{
		String name;
		BufferedReader dis;
		PrintStream ps;
		Socket socket;
		public Client(Socket s) {
			socket=s;
			try {
				dis=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				ps=new PrintStream(socket.getOutputStream());
				String info=dis.readLine();
				StringTokenizer stinfo=new StringTokenizer(info,":");
				String head=stinfo.nextToken();
				name=stinfo.nextToken();
				jT1.append("系統信息:"+name+"已經連接\n");
			}catch(IOException e){
				jT2.append("系統信息:用戶連接出錯\n");
			}
		}
		public void send(String msg) {
			ps.println(msg);
			ps.flush();
		}
		public void run() {
			while(true) {
				String line=null;
				try {
					line=dis.readLine();
				}catch(IOException e) {
					jT2.append("系統消息:讀客戶信息出錯");
					disconnect(this);
					notifyRoom();
					return;
				}
				StringTokenizer st=new StringTokenizer(line,":");
				String keyword=st.nextToken();
				if(keyword.equalsIgnoreCase("MSG")) {
					sendClients(line);
				}else if(keyword.equalsIgnoreCase("QUIT")) {
					send("QUIT");
					disconnect(this);
					notifyRoom();
					this.stop();
				}
			}
		}
	}
	public boolean checkName(Client newClient) {
		for(int i=0;i<clients.size();i++) {
			Client c=(Client)clients.elementAt(i);
			if((c!=newClient)&&(c.name).equals(newClient.name))
				return false;
		}
		return true;
	}
	public void notifyRoom() {
		String people="PEOPLE";
		for(int i=0;i<clients.size();i++) {
			Client c=(Client)clients.get(i);
			people+=":"+c.name;
			System.out.println(c.name);
		}
		sendClients(people);
	}
	public void sendClients(String msg) {
		for(int i=0;i<clients.size();i++) {
			Client c=(Client)clients.get(i);
			c.send(msg);
		}
	}
	public void disconnect(Client c) {
		try {
			jT1.append(c.name+"斷開連接\n");
			c.send("QUIT");
			clients.removeElement(c);
			c.socket.close();
		}catch(IOException e) {
			jT2.append("客戶斷開錯誤\n");
		}
	}
	public void actionPerformed(ActionEvent e) {
		try {
			server=new ServerSocket(Integer.parseInt(jF1.getText()));
			jT2.append("系統提示:聊天服務器系統開始啓動……\n");
		}catch(IOException e1) {
			jT2.append("服務器端口打開出錯\n");
		}
		if(server!=null) {
			ConnectSocket connect=new ConnectSocket();
			connect.start();
		}
	}

}

三、test.java

public class test {

	public static void main(String[] args) {

		Client w1 = new Client();

	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章