Java聊天室 IO流 Socket流 GUI界面 客户端DeleteCompoent界面详解
效果图
- 查询按钮点击 发送查询对象 io流读取聊天记录文件 界面显示内容
- 删除按钮点击 发送删除对象 io流读取聊天记录文件 清空聊天记录文件
- 显示提示语
checkButtonx.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
infortextArea1.setText("");
String text = infortextArea2.getText();
if (!Objects.equals(text, "")) {
String jname = MainView.ss + "MEtoYOU" + text + ".txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff, jname);
if (file.exists()) {
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(System.currentTimeMillis());
BufferedWriter bu = null;
try {
bu = new BufferedWriter(new FileWriter(file));
bu.write("");
bu.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
JOptionPane.showMessageDialog(null, "私聊记录已成功清空!");
infortextArea1.setText("您已成功删除与" + text + "用户的私聊记录记录!!!【" + "\n" + timeStr + "】");
} else {
JOptionPane.showMessageDialog(null, "您与" + text + "用户没有私聊记录记录!");
}
} else {
JOptionPane.showMessageDialog(null, "用户名为空!请输入用户名!");
}
}
});
checkButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
infortextArea1.setText("");
String jname = MainView.ss + "MEto.txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff, jname);
if (file.exists()) {
try {
BufferedReader bu = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bu.readLine()) != null) {
infortextArea1.append(line + "\n");
}
} catch (IOException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "公聊记录不存在!");
}
}
});
代码
package SanWa.UI;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Objects;
public class DeleteCompoent extends Box {
public static JTextArea infortextArea1;
public static JTextArea infortextArea2;
private JPanel c2;
private JPanel c22;
public DeleteCompoent() {
//垂直布局
super(BoxLayout.Y_AXIS);
this.setBackground(new Color(208, 208, 208));
JLabel d1Label = new JLabel("聊天记录删除");//组装视图
d1Label.setBackground(new Color(50, 50, 50));
d1Label.setForeground(Color.white);
JPanel btnPanel = new JPanel();
Color color = new Color(208, 208, 208);
d1Label.setFont(new java.awt.Font("黑体", 1, 25));
d1Label.setBounds(500, 500, 1500, 600);
btnPanel.setBackground(new Color(50, 50, 50));
btnPanel.setMaximumSize(new Dimension(2000, 40));
btnPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
btnPanel.add(d1Label);
this.add(btnPanel);
Box u1Box = Box.createHorizontalBox();
u1Box.setPreferredSize(new Dimension(850, 520));
infortextArea1 = new JTextArea();
infortextArea1.setEditable(false);
infortextArea1.setBounds(0, 0, 850, 200);
infortextArea1.setBackground(new Color(208, 208, 208));
Font fon = new Font("黑体", 1, 25);
infortextArea1.setFont(fon);
JScrollPane scrollPane21 = new JScrollPane();
scrollPane21.setBounds(0, 0, 850, 300);
scrollPane21.setViewportView(infortextArea1);
u1Box.add(scrollPane21);
Box u5Box = Box.createHorizontalBox();
JScrollPane scrollPane_5 = new JScrollPane();
JScrollPane scrollPane_6 = new JScrollPane();
JLabel pLabel = new JLabel("【私人聊天记录删除】:请在此输入要删除的用户名:");
pLabel.setFont(new Font("黑体", 1, 16));
c2 = new JPanel();
c2.setLayout(new FlowLayout(FlowLayout.LEFT));
c2.setBackground(new Color(208, 208, 208));
c2.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel = new JPanel();
Color color1 = new Color(208, 208, 208);
btn1Panel.setBackground(color1);
btn1Panel.add(pLabel);
JPanel btn11Panel = new JPanel();
Color color11 = new Color(208, 208, 208);
infortextArea2 = new JTextArea();
infortextArea2.setPreferredSize(new Dimension(200, 18));
infortextArea2.setBackground(new Color(208, 208, 208));
infortextArea2.append("");
JButton checkButton = new JButton("私聊记录查询");
checkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
infortextArea1.setText("");
String text = infortextArea2.getText();
String jname = MainView.ss + "MEtoYOU" + text + ".txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff, jname);
try {
if (file.exists()) {
BufferedReader bu = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bu.readLine()) != null) {
infortextArea1.append(line + "\n");
}
} else {
JOptionPane.showMessageDialog(null, "您与该用户没有私人聊天记录!");
}
} catch (IOException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
}
});
JButton checkButtonx = new JButton("删除");
checkButtonx.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
infortextArea1.setText("");
String text = infortextArea2.getText();
if (!Objects.equals(text, "")) {
String jname = MainView.ss + "MEtoYOU" + text + ".txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff, jname);
if (file.exists()) {
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(System.currentTimeMillis());
BufferedWriter bu = null;
try {
bu = new BufferedWriter(new FileWriter(file));
bu.write("");
bu.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
JOptionPane.showMessageDialog(null, "私聊记录已成功清空!");
infortextArea1.setText("您已成功删除与" + text + "用户的私聊记录记录!!!【" + "\n" + timeStr + "】");
} else {
JOptionPane.showMessageDialog(null, "您与" + text + "用户没有私聊记录记录!");
}
} else {
JOptionPane.showMessageDialog(null, "用户名为空!请输入用户名!");
}
}
});
btn11Panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn11Panel.setBackground(color11);
btn11Panel.add(infortextArea2);
scrollPane_5.setViewportView(infortextArea2);
c2.setPreferredSize(new Dimension(600, 30));
c2.add(btn1Panel);
c2.add(scrollPane_5);
c2.add(checkButton);
c2.add(checkButtonx);
scrollPane_6.setViewportView(c2);
u5Box.add(scrollPane_6);
this.add(u5Box);
JLabel pLabel1 = new JLabel("【公聊记录删除】:");
pLabel1.setFont(new Font("黑体", 1, 16));
c22 = new JPanel();
c22.setLayout(new FlowLayout(FlowLayout.LEFT));
c22.setBackground(new Color(208, 208, 208));
c22.setPreferredSize(new Dimension(600, 30));
JPanel btn1Panel2 = new JPanel();
btn1Panel2.setBackground(color1);
btn1Panel2.add(pLabel1);
Box u7Box = Box.createHorizontalBox();
JScrollPane scrollPane_7 = new JScrollPane();
JPanel btn11Panel2 = new JPanel();
Color color112 = new Color(208, 208, 208);
JButton checkButton2 = new JButton("公聊记录查询");
checkButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
infortextArea1.setText("");
String jname = MainView.ss + "MEto.txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff, jname);
if (file.exists()) {
try {
BufferedReader bu = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bu.readLine()) != null) {
infortextArea1.append(line + "\n");
}
} catch (IOException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "公聊记录不存在!");
}
}
});
JButton checkButton22 = new JButton("删除");
checkButton22.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
infortextArea1.setText("");
String jname = MainView.ss + "MEto.txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff, jname);
if (file.exists()) {
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(System.currentTimeMillis());
BufferedWriter bu = null;
try {
bu = new BufferedWriter(new FileWriter(file));
bu.write("");
bu.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
JOptionPane.showMessageDialog(null, "公聊记录已成功清空!");
infortextArea1.setText("您已成功删除公聊记录!!!【" + timeStr + "】");
} else {
JOptionPane.showMessageDialog(null, "公聊记录不存在!");
}
}
});
btn11Panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn11Panel2.setBackground(color11);
c22.add(btn1Panel2);
c22.add(checkButton2);
c22.add(checkButton22);
scrollPane_7.setViewportView(c22);
u7Box.add(scrollPane_7);
this.add(u7Box);
this.add(u1Box);
}
}