资源简介
本聊天室功能齐全。
(1)同时实现私聊和群聊
(2)实时监测人员的上下线
(3)基于TCP
(4)有聊天界面
代码片段和文件信息
package TcpCommunication;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatClient extends Jframe implements ActionListener {
Jframe clientframe = new Jframe(“TCP网络聊天室“); // 新建GUI框架
GridBagLayout gl; // 新建GridBagLayout布局管理器
BorderLayout bdl; // 新建BorderLayout布局管理器
GridBagConstraints gbc; // 新建GridBagConstraints容器
// 聊天界面
JPanel pnlBack pnlTalk; // 新建聊天面板
JButton btnTalk; // 新建按钮
JTextArea txtViewTalk;
JLabel lblTalk lblTo;
JComboBox listOnline; // 新建组合框
// 登录界面
JPanel pnlLogin; // 新建登录面板
JLabel lblServerIP lblName lblPassword; // 新建标签
JTextField txtTalk txtServerIP txtName; // 新建文本域
JPasswordField txtPassword; // 密码输入组件,可以设置密码的显示方式
JButton btnLogin btnReg btnExit;
JDialog dialogLogin = new JDialog(this “登录“ true); // 新建一个具有指定标题、模式和指定所有者的对话框
Socket socket = null; // 新建套接字
BufferedReader in = null; // 读取数据
PrintWriter out = null; // 写入数据
String strSend strReceive strKey strStatus;
private StringTokenizer st;
public ChatClient() {
// 初始化
gl = new GridBagLayout(); // 新建GridBagLayout布局管理器
bdl = new BorderLayout(); // 新建BorderLayout布局管理器
gbc = new GridBagConstraints(); // 新建GridBagConstraints容器
pnlBack = (JPanel) getContentPane();// 用getContentPane()返回该窗体的contentPane 对象,然后将其转换成JPanel类型
pnlBack.setLayout(bdl);
// 初始化控件
pnlLogin = new JPanel(); // 登录面板
pnlLogin.setLayout(gl); // 设置登录面板布局管理器
lblServerIP = new JLabel(“服务器IP:“);
lblName = new JLabel(“ 用户名:“);
lblPassword = new JLabel(“ 密码: “);
txtServerIP = new JTextField(12);
txtName = new JTextField(12);
txtPassword = new JPasswordField(12);
txtServerIP.setText(“127.0.0.1“); // 设置初始服务器IP
btnLogin = new JButton(“登录“);
btnReg = new JButton(“注册“);
btnExit = new JButton(“退出“);
btnTalk = new JButton(“发送“);
lblTalk = new JLabel(“输入消息:“);
lblTo = new JLabel(“ To :“);
txtTalk = new JTextField(30); // 创建一个30列的空文本域
pnlTalk = new JPanel(); // 新建对话面板
txtViewTalk = new JTextArea(18 40); // 创建一个18行40列的文本区域
listOnline = new JComboBox(); // 新建组合框(选择列表)
txtViewTalk.setForeground(Color.black); // 设置文本域前景颜色为黑色(即为字体颜色)
txtViewTalk.setEditable(false); // 设置文本区域不可编辑
// 添加监听
btnTalk.addActionListener(this);
btnLogin.addActionListener(this);
btnReg.addActionListener(this);
btnExit.addActionListener(this);
listOnline.addItem(“All“); // 向组合框添加一个条目“ALL“
// 向聊天面板中添加组件
pnlTalk.add(lblTalk);
pnlTalk.add(txtTalk);
pnlTalk.add(lblTo);
pnlTalk.add(listOnline);
pnlTalk.add(btnTalk);
pnlBack.add(“Center“ txtViewTalk);
pnlBack.add(“South“ pnlTalk);
pnlTalk.setBackground(Color.GRAY);
btnTalk.setEnabled(false); // 设置发送按钮为不可用
// 聊天界面初始化
clientframe.getContentPane().add(pnlBack);
clientframe.setSize(600 450);
clientframe.setVisible(true);
clie
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-01-08 10:28 TcpCommunicationFinal\
文件 301 2016-12-23 16:34 TcpCommunicationFinal\.classpath
文件 397 2016-12-23 16:34 TcpCommunicationFinal\.project
目录 0 2017-01-08 10:28 TcpCommunicationFinal\.settings\
文件 598 2016-12-23 16:34 TcpCommunicationFinal\.settings\org.eclipse.jdt.core.prefs
目录 0 2017-01-08 10:29 TcpCommunicationFinal\bin\
目录 0 2017-01-08 10:29 TcpCommunicationFinal\bin\TcpCommunication\
文件 2778 2017-01-08 10:29 TcpCommunicationFinal\bin\TcpCommunication\ChatClient$ClientThread.class
文件 7851 2017-01-08 10:29 TcpCommunicationFinal\bin\TcpCommunication\ChatClient.class
文件 2784 2017-01-08 10:29 TcpCommunicationFinal\bin\TcpCommunication\ChatServer.class
文件 9465 2017-01-08 10:51 TcpCommunicationFinal\bin\TcpCommunication\ServerThread.class
文件 717 2017-01-08 10:29 TcpCommunicationFinal\bin\TcpCommunication\ServerView$1.class
文件 4991 2017-01-08 10:29 TcpCommunicationFinal\bin\TcpCommunication\ServerView.class
文件 1939 2016-12-23 17:03 TcpCommunicationFinal\log.txt
目录 0 2017-01-08 10:28 TcpCommunicationFinal\src\
目录 0 2017-01-08 10:28 TcpCommunicationFinal\src\TcpCommunication\
文件 10419 2017-01-05 17:32 TcpCommunicationFinal\src\TcpCommunication\ChatClient.java
文件 2018 2016-12-30 17:30 TcpCommunicationFinal\src\TcpCommunication\ChatServer.java
文件 12216 2017-01-08 10:51 TcpCommunicationFinal\src\TcpCommunication\ServerThread.java
文件 6072 2017-01-05 22:09 TcpCommunicationFinal\src\TcpCommunication\ServerView.java
文件 106 2017-01-01 14:03 TcpCommunicationFinal\user.txt
评论
共有 条评论