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

本文详细介绍了使用Java实现聊天室的GUI界面设计,包括私聊功能的实现,通过Socket流进行消息传递,以及如何在界面中展示聊天记录并保存至本地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

效果图







  • 发送按钮点击 发送私聊对象 界面显示内容 并保存至聊天记录
 setButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setMessage = infortextArea.getText();
                String sender = infortextArea2.getText();
                if (!Objects.equals(sender, "")) {
                    if (!Objects.equals(setMessage, "")) {
                        User us = new User(MainView.ss, sender, setMessage, MsgTypeConfig.PRIVATE_MSG, System.currentTimeMillis());
                        String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(us.getTime());
                        Font fon = new Font("黑体", 1, 25);
                        infortextArea1.setFont(fon);
                        infortextArea1.append(MainView.ss + ":" + "【" + timeStr + "】" + "\n");
                        infortextArea1.append(setMessage + "\n");
                        try {
                            send.writeObject(us);
                            String jname = MainView.ss + "MEtoYOU" + sender + ".txt";
                            File ff = new File("ChatRoom\\JiLu");
                            File file = new File(ff, jname);
                            file.createNewFile();
                            BufferedWriter bu = new BufferedWriter(new FileWriter(file, true));
                            bu.write(MainView.ss + ":" + "【" + timeStr + "】" + "\n");
                            bu.write(setMessage + "\n");
                            bu.flush();

                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                        infortextArea.setText("");
                    } else {
                        JOptionPane.showMessageDialog(null, "不能发送空的消息!请输入消息!");
                    }

                } else {
                    JOptionPane.showMessageDialog(null, "接收者为空!请输入接收者!");
                }


            }

        });

代码

package SanWa.UI;

import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import SanWa.Util.PathUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
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 PrivateCompoent extends Box {
    public static JTextArea infortextArea;
    public static JTextArea infortextArea1;
    public static JTextArea infortextArea2;
    public static ObjectOutputStream send = MainView.out;
    String setMessage;
    private JPanel c2;

    public PrivateCompoent() {

        //垂直布局
        super(BoxLayout.Y_AXIS);
        BackGroundPanel bgPanel = null;
        BackGroundPanel bgPanel1 = null;
        BackGroundPanel bgPanel2 = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
            bgPanel1 = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
            bgPanel2 = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //  this.setBackground(new Color(58,115,195));
        JLabel d1Label = new JLabel("                    私聊频道");//组装视图
        JPanel btnPanel = new JPanel();
        Color color = new Color(50,50,50);
        d1Label.setFont(new java.awt.Font("楷体", 1, 30));
        d1Label.setForeground(Color.white);
        d1Label.setPreferredSize(new Dimension(770, 39));
        d1Label.setBounds(500, 500, 1500, 600);
        btnPanel.setBackground(color);
        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, 325));
        JLabel uLabel = new JLabel(" ");
        infortextArea1 = new JTextArea();
        infortextArea1.setEditable(false);
        infortextArea1.setBounds(0, 0, 850, 200);
        infortextArea1.setBackground(new Color(208,208,208));
        JScrollPane scrollPane21 = new JScrollPane();
        scrollPane21.setBounds(0, 0, 850, 300);
        scrollPane21.setViewportView(infortextArea1);
        u1Box.add(scrollPane21);
        this.add(u1Box);


        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(850, 30));
        JPanel btn1Panel = new JPanel();
        Color color1 = new Color(208,208,208);
        btn1Panel.setBackground(color1);
        btn1Panel.add(pLabel);
        JPanel btn11Panel = new JPanel();
        Box u5Box = Box.createHorizontalBox();
        JScrollPane scrollPane_5 = new JScrollPane();
        Color color11 = new Color(208,208,208);
        infortextArea2 = new JTextArea();
        infortextArea2.setFont(new java.awt.Font("楷体", 1, 16));
        infortextArea2.setPreferredSize(new Dimension(240, 25));
        infortextArea2.setBackground(new Color(208,208,208));
        infortextArea2.setText("");
        JButton checkButton = new JButton("查询用户在线状态");
        btn11Panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        btn11Panel.setBackground(color11);
        btn11Panel.add(infortextArea2);
scrollPane_5.setViewportView(infortextArea2);


        c2.add(btn1Panel);
        c2.add(scrollPane_5);
        //c2.add(checkButton);
        this.add(c2);


        Box uBox = Box.createHorizontalBox();
        uBox.setPreferredSize(new Dimension(850, 180));
        infortextArea = new JTextArea();
        infortextArea.setFont(new java.awt.Font("楷体", 1, 23));
        infortextArea.setBounds(0, 0, 850, 200);
        infortextArea.setBackground(new Color(208,208,208));
        infortextArea.setText("");
        JScrollPane scrollPane_1 = new JScrollPane();

        scrollPane_1.setViewportView(infortextArea);
        uBox.add(scrollPane_1);

        this.add(uBox);

        JPanel btnPanel1 = new JPanel();
        Color color12 = new Color(208,208,208);
        btnPanel1.setBackground(color12);
        btnPanel1.setMaximumSize(new Dimension(1500, 40));
        btnPanel1.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton setButton = new JButton("发送");
        setButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setMessage = infortextArea.getText();
                String sender = infortextArea2.getText();
                if (!Objects.equals(sender, "")) {
                    if (!Objects.equals(setMessage, "")) {
                        User us = new User(MainView.ss, sender, setMessage, MsgTypeConfig.PRIVATE_MSG, System.currentTimeMillis());
                        String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(us.getTime());
                        Font fon = new Font("黑体", 1, 25);
                        infortextArea1.setFont(fon);
                        infortextArea1.append(MainView.ss + ":" + "【" + timeStr + "】" + "\n");
                        infortextArea1.append(setMessage + "\n");
                        try {
                            send.writeObject(us);
                            String jname = MainView.ss + "MEtoYOU" + sender + ".txt";
                            File ff = new File("ChatRoom\\JiLu");
                            File file = new File(ff, jname);
                            file.createNewFile();
                            BufferedWriter bu = new BufferedWriter(new FileWriter(file, true));
                            bu.write(MainView.ss + ":" + "【" + timeStr + "】" + "\n");
                            bu.write(setMessage + "\n");
                            bu.flush();

                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                        infortextArea.setText("");
                    } else {
                        JOptionPane.showMessageDialog(null, "不能发送空的消息!请输入消息!");
                    }

                } else {
                    JOptionPane.showMessageDialog(null, "接收者为空!请输入接收者!");
                }


            }

        });
        JButton getButton1 = new JButton("取消");
        btnPanel1.add(getButton1);
        btnPanel1.add(setButton);
        this.add(btnPanel1);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值