import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
/*
* 聊天客户端的主框架类
*/
public class ChatClient extends JFrame implements ActionListener {
String ip = "127.0.0.1";// 连接到服务端的ip地址
int port = 8888;// 连接到服务端的端口号
String userName = "Alice";// 用户名
int type = 0;// 0表示未连接,1表示已连接
Image icon;// 程序图标
JComboBox combobox;// 选择发送消息的接受者
JTextArea messageShow;// 客户端的信息显示
JScrollPane messageScrollPane;// 信息显示的滚动条
JLabel sendToLabel, messageLabel;
JTextField clientMessage;// 客户端消息的发送
JButton clientMessageButton;// 发送消息
JTextField showStatus;// 显示用户连接状态
Socket socket;
ObjectOutputStream output;// 网络套接字输出流
ObjectInputStream input;// 网络套接字输入流
ClientReceive recvThread;
// 建立工具栏
JToolBar toolBar = new JToolBar();
// 建立工具栏中的按钮组件
JButton loginButton;// 用户登录
JButton logoffButton;// 用户注销
JButton userButton;// 用户信息的设置
JButton exitButton;// 退出按钮
// 框架的大小
Dimension faceSize = new Dimension(400, 600);
JPanel downPanel;
GridBagLayout girdBag;
GridBagConstraints girdBagCon;
public ChatClient() {
init();// 初始化程序
// 添加框架的关闭事件处理
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
// 设置框架的大小
this.setSize(faceSize);
// 设置运行时窗口的位置
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int) (screenSize.width - faceSize.getWidth()) / 2,
(int) (screenSize.height - faceSize.getHeight()) / 2);
this.setResizable(false);
this.setTitle("Chat Client"); // 设置标题
// 程序图标
icon = getImage("icon.gif");
this.setIconImage(icon); // 设置程序图标
show();
}
/**
* 程序初始化函数
*/
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
// 初始化按钮
loginButton = new JButton("Login");
logoffButton = new JButton("Logout");
userButton = new JButton("UserSetting");
exitButton = new JButton("Exit");
// 将按钮添加到工具栏
toolBar.add(userButton);
toolBar.addSeparator();// 添加分隔栏
toolBar.add(loginButton);
toolBar.add(logoffButton);
toolBar.addSeparator();// 添加分隔栏
toolBar.add(exitButton);
contentPane.add(toolBar, BorderLayout.NORTH);
// 初始时
loginButton.setEnabled(true);
logoffButton.setEnabled(false);
// 添加按钮的事件侦听
loginButton.addActionListener(this);
logoffButton.addActionListener(this);
userButton.addActionListener(this);
exitButton.addActionListener(this);
combobox = new JComboBox();
combobox.insertItemAt("All", 0);
combobox.setSelectedIndex(0);
messageShow = new JTextArea();
messageShow.setEditable(false);
// 添加滚动条
messageScrollPane = new JScrollPane(messageShow,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
messageScrollPane.setPreferredSize(new Dimension(400, 400));
messageScrollPane.revalidate();
clientMessage = new JTextField(23);
clientMessage.setEnabled(false);
clientMessageButton = new JButton();
clientMessageButton.setText("Send");
// 添加系统消息的事件侦听
clientMessage.addActionListener(this);
clientMessageButton.addActionListener(this);
sendToLabel = new JLabel("SendTo:");
messageLabel = new JLabel("Message:");
downPanel = new JPanel();
girdBag = new GridBagLayout();
downPanel.setLayout(girdBag);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 0;
girdBagCon.gridy = 0;
girdBagCon.gridwidth = 5;
girdBagCon.gridheight = 2;
girdBagCon.ipadx = 5;
girdBagCon.ipady = 5;
JLabel none = new JLabel(" ");
girdBag.setConstraints(none, girdBagCon);
downPanel.add(none);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 0;
girdBagCon.gridy = 2;
girdBagCon.insets = new Insets(1, 0, 0, 0);
girdBag.setConstraints(sendToLabel, girdBagCon);
downPanel.add(sendToLabel);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 1;
girdBagCon.gridy = 2;
girdBagCon.anchor = GridBagConstraints.LINE_START;
girdBag.setConstraints(combobox, girdBagCon);
downPanel.add(combobox);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 4;
girdBagCon.gridy = 2;
girdBagCon.insets = new Insets(1, 0, 0, 0);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 0;
girdBagCon.gridy = 3;
girdBag.setConstraints(messageLabel, girdBagCon);
downPanel.add(messageLabel);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 1;
girdBagCon.gridy = 3;
girdBagCon.gridwidth = 3;
girdBagCon.gridheight = 1;
girdBag.setConstraints(clientMessage, girdBagCon);
downPanel.add(clientMessage);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 4;
girdBagCon.gridy = 3;
girdBag.setConstraints(clientMessageButton, girdBagCon);
downPanel.add(clientMessageButton);
showStatus = new JTextField(35);
showStatus.setEditable(false);
girdBagCon = new GridBagConstraints();
girdBagCon.gridx = 0;
girdBagCon.gridy = 5;
girdBagCon.gridwidth = 5;
girdBag.setConstraints(showStatus, girdBagCon);
downPanel.add(showStatus);
contentPane.add(messageScrollPane, BorderLayout.CENTER);
contentPane.add(downPanel, BorderLayout.SOUTH);
// 关闭程序时的操作
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (type == 1) {
DisConnect();
}
System.exit(0);
}
});
}
/**
* 事件处理
*/
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == userButton) { // 用户信息设置
// 调出用户信息设置对话框
UserConf userConf = new UserConf(this, userName);
userConf.show();
userName = userConf.userInputName;
} else if (obj == loginButton) { // 登录
Connect();
} else if (obj == logoffButton) { // 注销
DisConnect();
showStatus.setText("");
} else if (obj == clientMessage || obj == clientMessageButton) { // 发送消息
SendMessage();
clientMessage.setText("");
} else if (obj == exitButton) { // 退出
int j = JOptionPane.showConfirmDialog(this, "You really want to quit?", "Exit",
JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);
if (j == JOptionPane.YES_OPTION) {
if (type == 1) {
DisConnect();
}
System.exit(0);
}
}
}
public void Connect() {
try {
socket = new Socket(ip, port);
} catch (Exception e) {
JOptionPane.showConfirmDialog(this, "Can not connect to the server!!",
"Warning", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE);
return;
}
try {
output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
input = new ObjectInputStream(socket.getInputStream());
output.writeObject(userName);
output.flush();
recvThread = new ClientReceive(socket, output, input, combobox,
messageShow, showStatus);
recvThread.start();
loginButton.setEnabled(false);
userButton.setEnabled(false);
logoffButton.setEnabled(true);
clientMessage.setEnabled(true);
messageShow.append("Connect to server " + ip + ":" + port + " success...\n");
type = 1;// 标志位设为已连接
} catch (Exception e) {
System.out.println(e);
return;
}
}
public void DisConnect() {
loginButton.setEnabled(true);
userButton.setEnabled(true);
logoffButton.setEnabled(false);
clientMessage.setEnabled(false);
if (socket.isClosed()) {
return;
}
try {
output.writeObject("用户下线");
output.flush();
input.close();
output.close();
socket.close();
messageShow.append("Disconnect to the server!!!\n");
type
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
ChattingRoom.rar (24个子文件)
ChattingRoom
.project 388B
bin
Node.class 522B
icon.gif 575B
ChatServer.class 8KB
ChatClient.class 10KB
ClientReceive.class 2KB
ServerListen.class 2KB
ChatClient$1.class 728B
UserConf.class 3KB
UserConf$2.class 755B
UserConf$3.class 813B
ChatServer$1.class 668B
UserLinkList.class 1KB
UserConf$1.class 1KB
ServerReceive.class 4KB
src
icon.gif 575B
UserConf.java 3KB
ClientReceive.java 2KB
UserLinkList.java 1KB
Node.java 207B
ServerReceive.java 3KB
ChatServer.java 6KB
ChatClient.java 9KB
.classpath 232B
共 24 条
- 1
ddzzjj1988
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- ### 1、项目介绍 本项目Scrapy进行数据爬取,并使用Django框架+PyEcharts实现可视化大屏 效果如下:
- # 微信小程序-健康菜谱 基于微信小程序的一个查找检索菜谱的应用 ### 效果 !动态图(./res/gif/demo
- zabbix-get命令包资源
- 毕业设计,基于PyQt5实现的可视化界面的Python车牌自动识别系统源码
- 26-朴素贝叶斯分类.rar
- 没有安Matlab 也可以 生成FIR抽头系数工具.py
- python烟花代码.rar
- 实验目的: 1.构建基于verilog语言的组合逻辑电路和时序逻辑电路; 2.掌握verilog语言的电路设计技巧 3.完成如
- 扩展卡尔曼滤波matlab仿真
- 3_base.apk.1
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0