Java聊天室 IO流 Socket流 GUI界面 客户端ChaXunCompoent界面详解

Java聊天室 IO流 Socket流 GUI界面 客户端ChaXunCompoent界面详解

效果图




  • 查询按钮点击 发送查询对象 io流读取聊天记录文件 界面显示内容
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, "公聊记录不存在!");
                }

            }
        });
                checkButton.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);
                    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, "您与" + text + "用户没有私聊记录记录!");
                        }

                    } 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.util.Objects;

public class ChaXunCompoent extends Box {
    public static JTextArea infortextArea1;
    public static JTextArea infortextArea2;
    public static ObjectOutputStream send = MainView.out;
    private JPanel c2;
    private JPanel c22;

    public ChaXunCompoent() {

        //垂直布局
        super(BoxLayout.Y_AXIS);
        this.setBackground(new Color(208, 208, 208));
        JLabel d1Label = new JLabel("聊天记录查询");//组装视图
        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, 40));
        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();
                if (!Objects.equals(text, "")) {

                    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, "您与" + text + "用户没有私聊记录记录!");
                        }

                    } catch (IOException fileNotFoundException) {
                        fileNotFoundException.printStackTrace();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "用户名为空!请输入用户名!");
                }
            }
        });
        btn11Panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        btn11Panel.setBackground(color11);
        btn11Panel.add(infortextArea2);
        scrollPane_6.setViewportView(infortextArea2);
        c2.setPreferredSize(new Dimension(600, 30));
        c2.add(btn1Panel);
        c2.add(scrollPane_6);
        c2.add(checkButton);
        scrollPane_5.setViewportView(c2);
        u5Box.add(scrollPane_5);
        this.add(u5Box);

        Box u7Box = Box.createHorizontalBox();
        JScrollPane scrollPane_7 = new JScrollPane();
        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);

        JPanel btn11Panel2 = new JPanel();
        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, "公聊记录不存在!");
                }

            }
        });
        btn11Panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        btn11Panel2.setBackground(color11);


        c22.add(btn1Panel2);
        c22.add(checkButton2);
        scrollPane_7.setViewportView(c22);
        u7Box.add(scrollPane_7);
        this.add(u7Box);
        this.add(u1Box);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值