package com.liuqi.chart.client.ui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import com.liuqi.chart.client.ClientSenderImpl;
import com.liuqi.chart.client.tran.SendFileWorker;
import com.liuqi.chart.client.util.Cache;
import com.liuqi.chart.common.bean.TextMessage;
import com.liuqi.chart.common.bean.User;
import com.liuqi.common.ui.LFrame;
/**
* 聊天面板
* @author 67
*
*/
public class ChartDialog extends LFrame implements ActionListener{
private User user;//与哪个用户在聊天
private JTextPane historyArea = new JTextPane();
private JTextArea inputArea = new JTextArea();
private ChartToolBar tb = new ChartToolBar(this);
private ChartRightPanel rightPanel = new ChartRightPanel(this);
private JButton sendButton = new JButton("发送");
private JButton closeButton = new JButton("关闭");
private SimpleAttributeSet myNameSet = new SimpleAttributeSet();//显示的我的名称的字体
private SimpleAttributeSet myContentSet = new SimpleAttributeSet();//显示的我的内容的字体
private SimpleAttributeSet opeNameSet = new SimpleAttributeSet();//显示的对方的名称的字体
private SimpleAttributeSet opeContentSet = new SimpleAttributeSet();//显示的对方的内容的字体
private SendFileWorker worker;
public ChartDialog(User user) {
worker = new SendFileWorker(this);
worker.execute();
this.user = user;
this.setTitle("与" + this.user.getName() + "聊天中...");
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
initAttributeSets();
initView();
sendButton.addActionListener(this);
closeButton.addActionListener(this);
}
/**
* 初始化各个显示字体
*/
private void initAttributeSets(){
StyleConstants.setForeground(myNameSet, Color.green);
StyleConstants.setForeground(myContentSet, Color.black);
StyleConstants.setForeground(opeNameSet, Color.blue);
StyleConstants.setForeground(opeContentSet, Color.black);
}
/**
* 初始化界面
*/
private void initView(){
this.setSize(600,500);
historyArea.setEditable(false);
historyArea.setBackground(new Color(240,240,240));
inputArea.setLineWrap(true);
this.setLayout(new MigLayout("insets 2","[grow][]","[grow][][]"));
this.add(new JScrollPane(historyArea),"growx,growy");
this.add(rightPanel,"wrap,w 140!,span 1 4,growy");
this.add(tb,"growx,wrap");
this.add(new JScrollPane(inputArea),"growx,h 80!,wrap");
this.add(closeButton,"split 2,h 24!,align right");
this.add(sendButton,"h 80!,h 24!");
this.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.gray),
BorderFactory.createBevelBorder(0)));
this.setBackground((ImageIcon)null);
this.setTitleColor(Color.black);
((JPanel)this.getContentPane()).setOpaque(true);
Color color = new Color(201,234,250);
color = Color.white;
this.getContentPane().setBackground(color);
this.setTitleBackground(color);
this.getTitleBar().setOpaque(true);
this.setLocationRelativeTo(null);
}
public User getUser() {
return user;
}
/**
* 取得右部面板
* @return
*/
public ChartRightPanel getRightPanel(){
return rightPanel;
}
/**
* 往聊天信息面板中写入接收到的信息
* @param message
*/
public void appendMessage(User user,String message){
Document document = historyArea.getDocument();
try {
document.insertString(document.getLength(), user.getName() + ":\n", opeNameSet);
document.insertString(document.getLength(), message + "\n", opeContentSet);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* 聊天面板中加入自己发送的内容
* @param message
*/
public void appendMessage(String message){
Document document = historyArea.getDocument();
try {
document.insertString(document.getLength(), "我:\n", myNameSet);
document.insertString(document.getLength(), message + "\n", myContentSet);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(sendButton)){
TextMessage message = new TextMessage();
message.setMessage(inputArea.getText().trim());
inputArea.setText("");
appendMessage(message.getMessage());
try {
ClientSenderImpl.getInstance().send(message, user);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getSource().equals(closeButton)){
this.setVisible(false);
}
}
public static void main(String[] args){
new ChartDialog(new User()).setVisible(true);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
用Java实现的一个简单聊天工具,有服务器也有客户端,能发送文字消息和文件,但是用户上线并未存入数据库,而只是简单的缓存在服务器的一个数组中,所以,只要服务器一关闭,所有用户数据都会消失,界面是用第三方包,很简单,有点丑陋。GB2312编码方式,基于JDK1.7编译,如果直接导入eclipse出问题,请自行百度解决,3Q
资源推荐
资源详情
资源评论
收起资源包目录
Java实现简单的聊天工具 (208个子文件)
.amateras 868B
ChartDialog.class 7KB
SendFileWorker.class 6KB
LFrame.class 5KB
Server.class 5KB
FileTransfferDialog.class 5KB
LTitleBar.class 5KB
ClientServer.class 4KB
LineChartTool.class 4KB
BarChartTool.class 4KB
TransfferImpl$TransfferWorker.class 4KB
TransfferImpl$TransfferWorker.class 4KB
Client.class 4KB
LPanelTable.class 4KB
LTable.class 3KB
ReceiveFileWorker.class 3KB
TranOneFilePanel.class 3KB
TransfferImpl.class 3KB
TransfferImpl.class 3KB
ClientSenderImpl.class 3KB
TranFilePanel.class 3KB
ChartToolBar.class 3KB
MainFrame.class 3KB
PieChartTool.class 3KB
CenterPanel.class 3KB
MessageProcessor.class 2KB
ChartRightPanel.class 2KB
LToolBarButtonUI.class 2KB
LToolBarButton.class 2KB
TimeTool.class 2KB
ChartDialogCache.class 2KB
LImageButton.class 2KB
UserListListener.class 2KB
TranFileCache.class 2KB
MessageCache.class 2KB
MoveListener.class 2KB
ChartTool.class 2KB
TranObject.class 2KB
TranObject.class 2KB
LPanelCellRenderer.class 2KB
User.class 2KB
User.class 2KB
UserCache.class 2KB
TopPanel.class 2KB
DateVerifyTool.class 2KB
ChartRightPanel$InfoPanel.class 1KB
PaintTool.class 1KB
SetIpDialog.class 1KB
ChartRightPanel$ImageLabel.class 1KB
LImageButtonUI.class 1KB
LDialog.class 1KB
TranObjectType.class 1KB
TranObjectType.class 1KB
LTableCellRenderer.class 1KB
Client$1.class 1KB
Test.class 1KB
NumberVerifyTool.class 1KB
Cache.class 1KB
Message.class 828B
Message.class 828B
LLogger.class 812B
Constants.class 799B
LLogger.class 798B
LTitleBar$TitleBarButton.class 733B
LColorLabel.class 719B
TextMessage.class 612B
TextMessage.class 612B
Transffer.class 611B
Transffer.class 611B
LTablePanel.class 548B
Constants.class 523B
Constants.class 523B
ClientSender.class 455B
BottomPanel.class 354B
.classpath 497B
.classpath 301B
jfreechart-1.0.13.jar 1.36MB
jcommon-1.0.16.jar 302KB
miglayout-3.7.4-swing.jar 75KB
ChartDialog.java 5KB
TransfferImpl.java 5KB
TransfferImpl.java 5KB
LFrame.java 5KB
LTitleBar.java 4KB
SendFileWorker.java 4KB
Server.java 4KB
Client.java 4KB
ClientServer.java 3KB
FileTransfferDialog.java 3KB
LineChartTool.java 3KB
BarChartTool.java 3KB
ChartRightPanel.java 3KB
TimeTool.java 3KB
LTable.java 2KB
LPanelTable.java 2KB
TranOneFilePanel.java 2KB
ClientSenderImpl.java 2KB
ReceiveFileWorker.java 2KB
ChartToolBar.java 2KB
TranFilePanel.java 2KB
共 208 条
- 1
- 2
- 3
Broncho.Lee
- 粉丝: 3750
- 资源: 105
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 功能强大的Java库,旨在简化与Milvus向量数据库的交互,为开发者提供类似MyBatis-Plus注解和方法调用的直观API
- ITK中基于B样条BSpline的可变形配准样例的输入输出图像
- 05-09 周四 vLLM的部署和实践
- WEB访问日志分析与入侵检测可视化系统源码(95分以上课设).zip
- UE4-UE5的官方预览模型
- 电子通信设计资料自动关断继电器驱动电路
- 基于Python使用递归和动态规划解决背包问题.zip
- 从0开始建立dSPACE RT Simulink工程:CAN通信(Bus Manager方法)DBC
- 轮胎X光缺陷检测数据集VOC+YOLO格式1903张4类别.zip
- 基于Matlab和遗传算法解决组合优化背包问题.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页