教育路上

全国站>Java>网络编程>java网络聊天室客户端
发布需求
填写需求信息关闭
我们会根据您的需求匹配

姓      名:

意向城市:

留      言:

手机号码:

验  证  码:  换一张

确认提交

java网络聊天室客户端

摘要:实现一个可视化的网络聊天室客户端程序 java 网络 聊天室 客户端 任务目标 掌握常用窗体控件的使用,掌握网络 API,掌握聊天室客户端程序的实现思路。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。

2022-06-13 13:29网络推荐

发布时间:
2022-06-13 13:29
信息来源:
网络推荐
浏览次数:
455
java网络聊天室客户端

网络聊天室客户端
任务介绍
实现一个可视化的网络聊天室客户端程序
任务目标
掌握常用窗体控件的使用,掌握网络 API,掌握聊天室客户端程序的实现思路
实现思路
设计一个聊天室界面,通过输入用户名并单击连接按钮进入聊天室,通过该客

户端,可以向所有用户发送信息,也可以向指定用户发送点对点信息。

实现代码:
ChatRoomClientUI.java

package aw;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;

public class ChatRoomClientUI extends JFrame {
	private JTextField jtfIP, jtfPort, jtfChat, jtfReciver, jtfName;
	private JLabel lblIP, lblPort, lblName;
	private JButton btnConnect, btnSend;
	private JTextArea jtaChat;
	private JComboBox jcbChatState;
	private JList userList;
	private ChatRoomClientUI ui;
	private ChatRoomClient client;

	public ChatRoomClientUI() {
//顶部
		ui = this;
		JPanel topPanel = new JPanel();
		lblIP = new JLabel("IP:");
		lblPort = new JLabel("端口");
		jtfIP = new JTextField("127.0.0.1");
		jtfPort = new JTextField("6666");
		lblName = new JLabel("昵称");
		jtfName = new JTextField(8);
		btnConnect = new JButton("连接");
		topPanel.add(lblIP);
		topPanel.add(jtfIP);
		topPanel.add(lblPort);
		topPanel.add(jtfPort);
		topPanel.add(lblName);
		topPanel.add(jtfName);
		topPanel.add(btnConnect);
		add(topPanel, BorderLayout.NORTH);
//主体
		jtaChat = new JTextArea();
		JScrollPane mainPanel = new JScrollPane(jtaChat);
//
		jtaChat.setPreferredSize(new Dimension(600, 480));
		mainPanel.setPreferredSize(new Dimension(600, 480));
		add(mainPanel);
//底部
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		String[] str = { "所有人", "私聊" };
		jcbChatState = new JComboBox(str);
		jtfChat = new JTextField(50);
		jtfReciver = new JTextField(8);
		btnSend = new JButton("发送");
		bottomPanel.add(jcbChatState);
		bottomPanel.add(jtfReciver);
		bottomPanel.add(jtfChat);
		bottomPanel.add(btnSend);
		add(bottomPanel, BorderLayout.SOUTH);
//右边 在线列表
		JScrollPane rightPanel = new JScrollPane();
		rightPanel.setPreferredSize(new Dimension(200, 480));
		DefaultListModel dlm = new DefaultListModel();
		userList = new JList(dlm);
		userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		rightPanel.setViewportView(userList);
		add(rightPanel, BorderLayout.EAST);
		pack();
		setTitle("简单聊天室客户端");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		btnConnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
				client = new ChatRoomClient(ui);
				client.connect(jtfIP.getText(), Integer.parseInt(jtfPort.getText()));
				Thread tr = new Thread(client);
				tr.start();
				client.sendWelcome(jtfName.getText());
			}
		});
		btnSend.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stub
				String state = (String) jcbChatState.getSelectedItem();
				if ("所有人".equals(state)) {
					jtaChat.append(jtfChat.getText() + "\r\n");
					String msg = "COMMEN@" + jtfName.getText() + "@" + jtfChat.getText();
					client.sendMsg(msg);
				} else {
					String reciverName = jtfReciver.getText();
					if (isReciverExist(reciverName)) {
						if (reciverName.equals(jtfName.getText()))
							jtaChat.append("你对自己说:" + jtfChat.getText());
						else {
							String msg = "PRIVACY@" + jtfName.getText() + "@" + reciverName + "@" + jtfChat.getText();
							client.sendMsg(msg);
							jtaChat.append(msg + "\r\n");
						}
					}
				}
			}
		});
	}

	public void updateUI(String str) {
		jtaChat.append(str + "\r\n");
	}

	public void addUser(String[] users) {
		DefaultListModel model = (DefaultListModel) userList.getModel();
		for (String user : users)
			model.addElement(user);
	}

	public void addUser(String user) {
		DefaultListModel model = (DefaultListModel) userList.getModel();
		if (!user.equals(jtfName.getText()))
			model.add(0, user);
	}

	private boolean isReciverExist(String reciverName) {
		DefaultListModel<String> model = (DefaultListModel<String>) userList.getModel();
		for (int i = 0; i < model.size(); i++)
			if (model.get(i).equals(reciverName))
				return true;
		return false;
	}
}



ChatRoomClient.java

package aw;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.Set;
import javax.swing.SwingUtilities;

public class ChatRoomClient implements Runnable {
	ChatRoomClientUI ui;
	Socket socket;
	PrintWriter pw;

	public ChatRoomClient(ChatRoomClientUI ui) {
// TODO Auto-generated constructor stub
		this.ui = ui;
	}

	@Override
	public void run() {
// TODO Auto-generated method stub
		BufferedInputStream bis;
		try {
			bis = new BufferedInputStream(socket.getInputStream());
			Scanner scanner = new Scanner(bis);
			while (true) {
				String rawString = scanner.nextLine();
				String[] str = rawString.split("@");
				switch (str[0]) {
				case "WELCOME":
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
// TODO Auto-generated method stub
							ui.updateUI("欢迎你" + str[1] + "\r\n");
							ui.addUser(str[1]);
						}
					});
					break;
				case "LIST":
					String[] users = new String[str.length - 1];
					for (int i = 1; i < str.length; i++)
						users[i - 1] = str[i];
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
// TODO Auto-generated method stub
							ui.addUser(users);
						}
					});
					break;
				case "COMMEN":
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
// TODO Auto-generated method stub
							ui.updateUI(str[1]);
						}
					});
					break;
				case "PRIVACY":
					String msg = str[1] + "悄悄对你说:" + str[2];
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
// TODO Auto-generated method stub
							ui.updateUI(msg);
						}
					});
				default:
					break;
				}
			}
		} catch (IOException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void connect(String ip, int port) {
		try {
			socket = new Socket(ip, port);
		} catch (UnknownHostException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sendWelcome(String name) {
		try {
			pw = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
			pw.println("WELCOME@" + name);
			pw.flush();
		} catch (IOException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sendMsg(String msg) {
// TODO Auto-generated method stub
		try {
			pw = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
			pw.println(msg);
			pw.flush();
		} catch (IOException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 Example02.java

package aw;

public class Example02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ChatRoomClientUI frame=new ChatRoomClientUI();
}
}

上一篇:
java聊天室服务器端程序
下一篇:
java实现文件上传服务器端和客户端
标签:
网友评论
发布评论

访客的评论 2022/12/01 17:18

文中描述的是准确的吗,如何报名!

相关推荐
我也来发表评价关闭
我对该内容的评价:
0
评价500
验证码: 看不清 换一张
提交 (匿名发布,无须担心别人知道您的身份)