package app;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Vector;
import javax.swing.*;
import util.Conf;
import vo.Customer;
import vo.Messege;
public class ChatFrame extends JFrame implements ActionListener,Runnable{
private Socket socket=null;
private ObjectInputStream ois=null;
private ObjectOutputStream oos=null;
private boolean canRun=true;
private String account;
private JLabel lbUser=new JLabel("在线人员名单:");
private List lstUser=new List();
private JLabel lbMsg=new JLabel("聊天记录:");
private JTextArea taMsg=new JTextArea();
private JScrollPane spMsg=new JScrollPane(taMsg);
private JTextField tfMsg=new JTextField();
private JButton btSend=new JButton("发送");
private JPanel plUser=new JPanel(new BorderLayout());
private JPanel plMsg=new JPanel(new BorderLayout());
private JPanel plUser_Msg=new JPanel(new GridLayout(1,2));
private JPanel plSend=new JPanel(new BorderLayout());
public ChatFrame(ObjectInputStream ois,ObjectOutputStream oos,Messege receiveMessage,String account){
this.ois=ois;
this.oos=oos;
this.account=account;
this.initFrame();
this.initUserList(receiveMessage);
new Thread(this).start();
}
public void initFrame()
{
this.setTitle("当前在线:"+account);
this.setBackground(Color.magenta);
plUser.add(lbUser,BorderLayout.NORTH);
plUser.add(lstUser,BorderLayout.CENTER);
plUser_Msg.add(plUser);
lstUser.setBackground(Color.pink);
plMsg.add(lbMsg,BorderLayout.NORTH);
plMsg.add(spMsg,BorderLayout.CENTER);
plUser_Msg.add(plUser);
lstUser.setBackground(Color.pink);
plSend.add(tfMsg,BorderLayout.CENTER);
plSend.add(btSend,BorderLayout.EAST);
tfMsg.setBackground(Color.yellow);
this.add(plUser_Msg,BorderLayout.CENTER);
this.add(plSend,BorderLayout.SOUTH);
btSend.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setVisible(true);
}
public void initUserList(Messege messege){
lstUser.removeAll();
lstUser.add(Conf.ALL);
lstUser.select(0);
Vector<Customer> userListVector=(Vector<Customer>)messege.getContent();
for(Customer cus:userListVector){
lstUser.add(cus.getAccount()+","+cus.getName());
}
}
public void run()
{
try{
while (canRun){
Messege msg=(Messege)ois.readObject();
if(msg.getType().equals(Conf.MESSAGE)){
taMsg.append(msg.getContent()+"\n");
}
else if(msg.getType().equals(Conf.LOGOUT)){
Customer cus=(Customer)msg.getContent();
lstUser.remove(cus.getAccount()+","+cus.getName());
}
}
}catch (Exception ex){
ex.printStackTrace();
canRun=false;
javax.swing.JOptionPane.showMessageDialog(this,"对不起,你被迫下线");
System.exit(-1);
}
}
public void actionPerformed(ActionEvent e){
try{
Messege msg=new Messege();
msg.setType(Conf.MESSAGE);
msg.setContent(account+"说:"+tfMsg.getText());
msg.setFrom(account);
String toInfo=lstUser.getSelectedItem();
msg.setTo(toInfo.split(",")[0]);
oos.writeObject(msg);
tfMsg.setText("");
}catch (Exception ex){
JOptionPane.showMessageDialog(this,"消息发送异常");
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
deta5-softwareTest-30360-1756660427254.zip

共17个文件
java:8个
gitignore:4个
review:2个

需积分: 5 0 下载量 114 浏览量
2025-09-02
12:19:06
上传
评论
收藏 9KB ZIP 举报
温馨提示
tdrdeta5_softwareTest_30360_1756660427254.zip
资源推荐
资源详情
资源评论






























收起资源包目录
























共 17 条
- 1
资源评论


A20250FSAF
- 粉丝: 60
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 水墨画:牵牛花(中班美工).doc
- 高中生物选修三导学案12基因工程的基本操作程序.doc
- 计算机专业自荐信.docx
- 关于强电线路对通信线路的及其防护.doc
- KTV管理公司和KTV管理团队的区别.docx
- 第七章-停车场管理系统设备安装工程-定额.doc
- 广东省深圳中学2020届高三2月份网络教学质量监测理综化学试题Word版含解析-(1).doc
- 第8章-外部线路工程.ppt
- 个人家庭理财流水账Excel表格.xls
- 阀门、管件试验作业指导书.docx
- c语言while循环获奖课件.pptx
- 助教负责人培训(1).doc
- [四川]农迁房安全监理实施细则ser.doc
- PLC技术在电器工程自动化控制中的运用论文.doc
- 电气工程施工图预算编制详解(含图例及计算).ppt
- 基础计算机培训课件.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
