📄 chatrecord.java
字号:
package clientele;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class Chatrecord extends JFrame
{
private static final long serialVersionUID = 146565654168L;
/**
* 文本域:存放聊天记录
*/
JTextArea jTextArea1 = null;
/**
* 关闭按钮
*/
JButton jButton1 = null;
/**
* 打开按钮
*/
JButton button = null;
/**
* 窗体
*/
JFrame jFrame = null;// 窗体
/**
* 添加按钮等
*/
JPanel jPanel = null;
/**
* 聊天发送者号码
*/
int souceid = 0;// 聊天发送者
/**
* 聊天接收者号码
*/
int destid = 0;// 聊天目标
/**
* 添加文本区
*/
JScrollPane jScrollPane1 = null;
/**
* 构造函数
*/
public Chatrecord(int souceid, int destid)
{
this.souceid = souceid;
this.destid = destid;
}
/**
* 此方法初始化
*/
public JFrame getJFrame()
{
if (jFrame == null)
{
jFrame = new JFrame();
jFrame.setBackground(new Color(153, 153, 255));
jFrame.setSize(460, 190);
jFrame.setContentPane(getJPanel());
Toolkit toolkit = jFrame.getToolkit();
Dimension screen = toolkit.getScreenSize();
jFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(
getClass().getResource("/picture/client.jpg")));
jFrame.setBounds(screen.width / 2 - 460 / 2,
screen.height / 2 - 350 / 2, 379, 350);// //让窗体在屏幕正中央显示
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.getRootPane().setDefaultButton(button);
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
jFrame.setVisible(false);// 隐藏窗体
}
});
}
return jFrame;
}
/**
* 此方法初始化jPanel
*/
public JPanel getJPanel()
{
if (jPanel == null)
{
jPanel = new JPanel();
jPanel.setBackground(new Color(153, 153, 255));
jPanel.setLayout(null);
}
if (jTextArea1 == null)
{
jTextArea1 = new JTextArea();
jTextArea1.setTabSize(8);
}
if (jButton1 == null)
{
jButton1 = new JButton();
jButton1.setFont(new Font("", Font.PLAIN, 14));
}
if ( button == null)
{
button = new JButton();
button.setFont(new Font("", Font.PLAIN, 14));
}
if (jScrollPane1 == null)
{
jScrollPane1 = new JScrollPane();
}
jTextArea1.setBackground(Color.white);
jTextArea1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
jTextArea1.setForeground(Color.black);
jTextArea1.setBorder(BorderFactory.createLineBorder(Color.green));
jTextArea1.setLineWrap(true);
jTextArea1.setBounds(6, 9, 100, 200);
jTextArea1.setEditable(false);
jScrollPane1.setBounds(new Rectangle(6, 9, 360, 260));
jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.getViewport().add(jTextArea1);// 聊天记录
jButton1.setBounds(new Rectangle(59, 280, 62, 24));
jButton1.setForeground(SystemColor.menuText);
jButton1.setText("关闭");
button.setBounds(new Rectangle(224, 280, 62, 24));
button.setForeground(SystemColor.menuText);
button.setText("打开");
/**
* 打开按钮事件监听
*/
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button)
jTextArea1.setText("");
try
{
String name = ".//" + String.valueOf(souceid) + ".txt";
BufferedReader fileread=new BufferedReader(new FileReader(name));
String temp="";
do{
temp=fileread.readLine();
if(temp!=null)
jTextArea1.append(temp+"\n");
}
while(temp!=null);
fileread.close();
}
catch (Exception ex)
{
System.out.println (ex.getMessage());
ex.printStackTrace();
}
}
});
/**
* 关闭按钮行为事件监听
*/
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jButton1)
jFrame.dispose();// 隐藏窗体
}
});
jPanel.setEnabled(true);
jPanel.add(jButton1);
jPanel.add(button);
jPanel.add(jScrollPane1);
return jPanel;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -