📄 chatevent.java
字号:
package client.chat.chatpane;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.StyledDocument;
import utils.InfoPacket;
import utils.LogUtils;
import utils.StaticUtils;
/**
* 聊天界面的所有按钮的监听事件类
* @author 洪景泉
*
*/
public class ChatEvent implements ActionListener{
private ChatFrame chatFrame=null;
private InputStream is=null;
private OutputStream os=null;
private ChatDialog cd=null;
private SimpleDateFormat sdf=null;
private File selectFile=null;
/**
*LeftPane的构造函数
* @param chatFrame 主窗体对象
* @param is 输入流
* @param os 输出流
* @param cd 聊天记录对话框的对象
*/
public ChatEvent(ChatFrame chatFrame,InputStream is,OutputStream os,ChatDialog cd) {
this.chatFrame = chatFrame;
this.is=is;
this.os=os;
this.cd=cd;
}
/**
* TopPane的构造函数
* @param chatFrame 主窗体对象
* @param is 输入流
* @param os 输出流
*/
public ChatEvent(ChatFrame chatFrame,InputStream is,OutputStream os) {
this.chatFrame = chatFrame;
this.is=is;
this.os=os;
}
public void actionPerformed(ActionEvent e) {
JButton btn=(JButton)e.getSource();
String acommant=btn.getActionCommand();
if(acommant.equals("send")){// 发送
String chatInfo = chatFrame.getLPane().getSendText().getText();
// 发送
String userID=(String)StaticUtils.currentUser.get("userID");
String currentuser=(String)StaticUtils.currentUser.get("userName");
String to=(String)StaticUtils.currentUser.get("to");
InfoPacket infoPacket = new InfoPacket(InfoPacket.CHAT,userID+":"+currentuser,to,chatInfo);
try {
os.write(infoPacket.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
}
// 显示
JTextPane showText = chatFrame.getLPane().getShowText();
StyledDocument showDoc = showText.getStyledDocument();
showText.setEditable(true);
try {
sdf = new SimpleDateFormat ("HH:mm:ss");
showDoc.insertString(showDoc.getLength(),currentuser+" "+sdf.format(new Date())+"\n",null);
showDoc.insertString(showDoc.getLength(), chatInfo, null);
showDoc.insertString(showDoc.getLength(), "\n", null);
} catch (Exception ex) {
ex.printStackTrace();
}
// 清空输入框
chatFrame.getLPane().getSendText().setText("");
showText.setEditable(false);
// 把当前用户发送的聊天记录放在追加到聊天记录对话框里面
cd.getChatHistory().append(" "+currentuser+" "+sdf.format(new Date())+"\n");
cd.getChatHistory().append(" "+chatInfo+"\n");
}else if(acommant.equals("close")){//关闭
//把当前的聊天内容存入文本文件里面
String str=chatFrame.getLPane().getShowText().getText();
LogUtils.write("./chatHistory/",str);
System.exit(0);
}else if(acommant.equals("chatLog")){//聊天记录
sdf = new SimpleDateFormat ("yyyyMMdd");
// 读取今天的聊天记录
File logFile = new File("./chatHistory/" + sdf.format(new Date()) + ".log");
if(logFile.exists()){
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(logFile);
br = new BufferedReader(fr);
String str = null;
while ((str = br.readLine()) != null) {
cd.getChatHistory().append(" "+str+"\n");
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
} finally {
try {
fr.close();
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
cd.setSize(chatFrame.getWidth(), 200);
Point cPoint=chatFrame.getLocationOnScreen();
Point dPoint=new Point(cPoint.x,cPoint.y+chatFrame.getHeight());
cd.setLocation(dPoint);
cd.setVisible(!cd.isVisible());
}else if(acommant.equals("reSetInfo")){//重置密码
//System.out.println("重置密码");
ModiPane mp=new ModiPane("修改用户信息",chatFrame,is,os);
mp.setSize(420,320);
mp.setModal(true);
mp.setLocationRelativeTo(null);
mp.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
mp.setVisible(true);
}
if(acommant.equals("fileBtn")){//发送文件
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("我的文档"));
selectFile = null;
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()|| f.isFile()) {
return true;
} else {
return false;
}
}
public String getDescription() {
return "所有文件(*.*)";
}
});
if(fileChooser.showOpenDialog(chatFrame) == JFileChooser.APPROVE_OPTION) {
selectFile = fileChooser.getSelectedFile();
// System.out.println("文件大小:"+selectFile.length());
// System.out.println(selectFile);
if (selectFile != null) {
// System.out.println("成功");
//发送请求包
String userID=(String)StaticUtils.currentUser.get("userID");
String currentuser=(String)StaticUtils.currentUser.get("userName");
String to=(String)StaticUtils.currentUser.get("to");
String fileName=selectFile.getName();
InfoPacket infoPacket = new InfoPacket(InfoPacket.FILE,userID+":"+currentuser,to,fileName+","+selectFile.length());
try {
os.write(infoPacket.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
}
JTextPane showText = chatFrame.getLPane().getShowText();
StyledDocument showDoc = showText.getStyledDocument();
showText.setEditable(true);
try {
sdf = new SimpleDateFormat ("HH:mm:ss");
showDoc.insertString(showDoc.getLength(),currentuser+" "+sdf.format(new Date())+"\n",null);
showDoc.insertString(showDoc.getLength(), "等待对方接收", null);
showDoc.insertString(showDoc.getLength(), "\n", null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
/**
* 获取当前选中文件的路径
* @return
*/
public File getSelectFile() {
return selectFile;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -