📄 chatdialog.java
字号:
package client.chat.chatpane;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
/**
* 聊天记录对话框
* @author 洪景泉
*
*/
public class ChatDialog extends JDialog {
private static final long serialVersionUID = -5065109993695575062L;
private JTextArea chatHistory=null;
private ChatFrame chatFrame=null;
private JScrollPane jsPane=null;
private JButton historyBtn=null;
private JPanel jp=null;
/**
* 构造函数
* @param chatFrame 主窗体的对象
*/
public ChatDialog(ChatFrame chatFrame) {
historyBtn=new JButton("打开历史记录");
jp=new JPanel();
jp.setLayout(new BorderLayout());
this.chatFrame=chatFrame;
chatHistory=new JTextArea();
chatHistory.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jp.add(historyBtn,BorderLayout.NORTH);
jp.add(chatHistory,BorderLayout.CENTER);
jsPane=new JScrollPane(jp);
this.getContentPane().add(jsPane);
this.setUndecorated(true);
// 改变聊天记录对话框大小的监听事件
chatFrame.addComponentListener(new ComponentListener() {
public void componentHidden(ComponentEvent e) {
}
public void componentMoved(ComponentEvent e) {
ChatDialog.this.setSize(ChatDialog.this.chatFrame.getWidth(), 200);
Point p =ChatDialog.this.chatFrame.getLocationOnScreen();
Point p1 = new Point(p.x, p.y +ChatDialog.this.chatFrame.getHeight());
ChatDialog.this.setLocation(p1);
ChatDialog.this.validate();
}
public void componentResized(ComponentEvent e) {
ChatDialog.this.setSize(ChatDialog.this.chatFrame.getWidth(), 200);
Point p =ChatDialog.this.chatFrame.getLocationOnScreen();
Point p1 = new Point(p.x, p.y + ChatDialog.this.chatFrame.getHeight());
ChatDialog.this.setLocation(p1);
ChatDialog.this.validate();
}
public void componentShown(ComponentEvent e) {
}
});
//打开历史聊天记录对话框的监听事件
historyBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("./chatHistory"));
File[] files = null;
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {//此过滤器是否接受给定的文件
// TODO Auto-generated method stub
String strFile = f.getName();
if (strFile.endsWith(".log")) {
return true;
}
return false;
}
public String getDescription() {
// TODO Auto-generated method stub
return "*.log";
}
});
fileChooser.setMultiSelectionEnabled(true);
if (fileChooser.showOpenDialog(ChatDialog.this.chatFrame) == JFileChooser.APPROVE_OPTION) {
files = fileChooser.getSelectedFiles();
} else {
return;
}
if (files.length > 0) {
ChatDialog.this.chatFrame.getLPane().getCd().getChatHistory().setText("");
}
for (int i = 0; i < files.length; i++) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(files[i]);
br = new BufferedReader(fr);
String str = null;
while ((str = br.readLine()) != null) {
ChatDialog.this.chatFrame.getLPane().getCd().getChatHistory().append(
str + "\n");
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} finally {
try {
fr.close();
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
);
}
public JTextArea getChatHistory() {
return chatHistory;
}
public void setChatHistory(JTextArea chatHistory) {
this.chatHistory = chatHistory;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -