Java聊天室 IO流 Socket流 GUI界面 所有的代码

Java聊天室 IO流 Socket流 GUI界面 所有的代码

客户端

package SanWa.Net;

public enum MsgTypeConfig {
    PRIVATE_MSG, ONLINE_LIST1, SHANGXIAN, WENJIAN, FANKUI,
    PUBLIC_MSG,
    ONLINE_MSG,
    ONLINE_LIST,
    EXIT_MSG;
}
package SanWa.Net;

import java.io.Serializable;
import java.net.Socket;
import java.util.HashMap;

public class User implements Serializable {
    static final long serialVersionUID = 456789L;
    //String zfMsg = username + ":" + content + ":" + msgType + ":" + System.currentTimeMillis();
    private String username;
    private String userSend;
    private String content;
    private MsgTypeConfig msgType;
    private long time;
    private HashMap<String, Socket> hm;
    private String fileName;
    private long fileLength;
    private byte fileData[];

    public User(String username, String userSend, MsgTypeConfig msgType, long time, String fileName, long fileLength, byte[] fileData) {
        this.username = username;
        this.userSend = userSend;
        this.msgType = msgType;
        this.time = time;
        this.fileName = fileName;
        this.fileLength = fileLength;
        this.fileData = fileData;
    }

    public User(String username, String content, MsgTypeConfig msgType, long time, HashMap<String, Socket> hm) {
        this.username = username;
        this.content = content;
        this.msgType = msgType;
        this.time = time;
        this.hm = hm;
    }

    public User(String username, String content, MsgTypeConfig msgType, long time) {
        this.username = username;
        this.content = content;
        this.msgType = msgType;
        this.time = time;
    }

    public User(String userSend, String username, String content, MsgTypeConfig msgType, long time) {
        this.userSend = userSend;
        this.username = username;
        this.content = content;
        this.msgType = msgType;
        this.time = time;
    }

    public User() {
    }

    public String getFileName() {
        return fileName;
    }

    public long getFileLength() {
        return fileLength;
    }

    public byte[] getFileData() {
        return fileData;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", userSend='" + userSend + '\'' +
                ", content='" + content + '\'' +
                ", msgType=" + msgType +
                ", time=" + time +
                ", hm=" + hm +
                '}';
    }

    public String getUserSend() {
        return userSend;
    }

    public HashMap<String, Socket> getHm() {
        return hm;
    }

    public MsgTypeConfig getMsgType() {
        return msgType;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }
}

package SanWa.UI;

import javax.swing.*;
import java.awt.*;

public class BackGroundPanel extends JPanel {
    //声明图片
    private Image backIcon;

    public BackGroundPanel(Image backIcon) {
        this.backIcon = backIcon;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //绘制背景
        g.drawImage(backIcon, 0, 0, this.getWidth(), this.getHeight(), 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);
    }
}

package SanWa.UI;

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

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;

public class ClientReadMsgThread extends Thread {
    public static File wenJian;
    public static byte[] fileBytes;
    public static User re = null;
    public static volatile boolean isSave = true;
    public static volatile boolean isClose = false;
    private Socket s;

    public ClientReadMsgThread(Socket sk) {
        s = sk;
    }

    @Override
    public void run() {

        try {

            while (true) {
                ObjectInputStream in = new ObjectInputStream(s.getInputStream());
                //接收服务端回复的消息:发送者:内容:消息类型:时间
                try {

                    re = (User) in.readObject();

                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

                //截取服务端转发回来的消息


                String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(re.getTime());
                if (re.getMsgType() == (MsgTypeConfig.PRIVATE_MSG)) {
                    FunctionView.sp.setRightComponent(FunctionView.privateCompoent);
                    Font fon = new Font("黑体", 1, 25);
                    PrivateCompoent.infortextArea1.setFont(fon);
                    PrivateCompoent.infortextArea1.append(re.getUsername() + "向你发来一条消息:" + "【" + timeStr + "】" + "\n");
                    PrivateCompoent.infortextArea2.setText(re.getUsername());
                    PrivateCompoent.infortextArea1.append(re.getContent() + "\n");
                    String jname = MainView.ss + "MEtoYOU" + re.getUsername() + ".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(re.getUsername() + "向你发来一条消息:" + "【" + timeStr + "】" + "\n");
                    bu.write(re.getContent() + "\n");
                    bu.flush();
                } else if (re.getMsgType() == MsgTypeConfig.ONLINE_MSG) {
                    if (re.getContent().equals("您已成功隐身!")) {
                        JOptionPane.showMessageDialog(null, "您已成功隐身!");
                    }
                    if (re.getContent().equals("您已成功上线!")) {
                        ShangXiaCompoent.infortextArea1.append("【上线信息】:【用户:" + MainView.ss + ":" + "上线了】【" + timeStr + "】【我】" + "\n");
                        JOptionPane.showMessageDialog(null, "您已成功上线!");
                    }
                } else if (re.getMsgType() == MsgTypeConfig.PUBLIC_MSG) {
                    FunctionView.sp.setRightComponent(FunctionView.publicCompoent);
                    Font fon = new Font("黑体", 1, 25);
                    PublicCompoent.infortextArea1.setFont(fon);
                    PublicCompoent.infortextArea1.append(re.getUsername() + "向大家发来一条消息:" + "【" + timeStr + "】" + "\n");
                    PublicCompoent.infortextArea1.append(re.getContent() + "\n");
                    String jname = MainView.ss + "MEto.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(re.getUsername() + "向大家发来一条消息:" + "【" + timeStr + "】" + "\n");
                    bu.write(re.getContent() + "\n");
                    bu.flush();
                } else if (re.getMsgType() == MsgTypeConfig.ONLINE_LIST) {
                    Font fon = new Font("黑体", 1, 20);
                    UserManageCompoent.infortextArea1.setFont(fon);
                    UserManageCompoent.infortextArea1.append(re.getContent());
                } else if (re.getMsgType() == MsgTypeConfig.FANKUI) {
                    Font fon = new Font("黑体", 1, 20);
                    WenJianconpoent.infortextArea1.setFont(fon);
                    if (re.getContent().equals("yes")) {
                        WenJianconpoent.infortextArea1.append("对方已经同意接受文件!" + "\n");
                    } else {
                        WenJianconpoent.infortextArea1.append("对方拒绝接受文件!请重新发送!舔到最后应有尽有!" + "\n");
                    }

                } else if (re.getMsgType() == MsgTypeConfig.SHANGXIAN) {
                    // FunctionView.sp.setRightComponent(FunctionView.shangXiaCompoent);
                    Font fon = new Font("黑体", 1, 20);
                    ShangXiaCompoent.infortextArea1.setFont(fon);
                    ShangXiaCompoent.infortextArea1.append(re.getContent() + "\n");
                } else if (re.getMsgType() == MsgTypeConfig.ONLINE_LIST1) {
                    Font fon = new Font("黑体", 1, 20);
                    YinShenCompoent.infortextArea1.setFont(fon);
                    YinShenCompoent.infortextArea1.append(re.getContent());
                } else if (re.getMsgType() == MsgTypeConfig.WENJIAN) {
                    FunctionView.sp.setRightComponent(FunctionView.wenJianconpoent);
                    String fileName = timeStr + re.getFileName();
                    long fileLength = re.getFileLength();
                    WenJianconpoent.infortextArea.append(re.getUserSend() + ":给你发来一个文件:" + fileName + "大小:" + (fileLength / 1024 / 1024.0) + "M" + "\n");
                    WenJianconpoent.infortextArea.append("请您选择处理方式  接受/拒绝 此文件?" + "\n");

                    while (isSave) {
                        if (isClose) {
                            break;
                        }
                    }
                    if (isClose) {
                        byte[] fileData = re.getFileData();
                        fileData = null;
                        User no = new User(re.getUserSend(), MainView.ss, "no", MsgTypeConfig.FANKUI, System.currentTimeMillis());
                        MainView.out.writeObject(no);
                        WenJianconpoent.infortextArea.append("文件拒绝成功!" + "\n");
                    } else {
                        fileBytes = re.getFileData();
                        File file = new File("E:\\WenJian");
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        wenJian = new File(file, re.getFileName());
                        FileUtils.writeFile("E:\\WenJian\\" + re.getFileName(), fileBytes);
                        boolean a = wenJian.exists();
                        if (a) {
                            WenJianconpoent.infortextArea.append("文件保存成功!在" + wenJian.getAbsolutePath() + "\n");
                        } else {
                            WenJianconpoent.infortextArea.append("文件保存失败!" + "\n");

                        }
                        User yes = new User(re.getUserSend(), MainView.ss, "yes", MsgTypeConfig.FANKUI, System.currentTimeMillis());
                        MainView.out.writeObject(yes);

                    }
                    isSave = true;
                    isClose = false;

                } else if (re.getMsgType() == MsgTypeConfig.EXIT_MSG) {
                    Font fon = new Font("黑体", 1, 20);
                    ShangXiaCompoent.infortextArea1.setFont(fon);
                    ShangXiaCompoent.infortextArea1.append(re.getContent() + "\n");
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

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);
    }
}

package SanWa.UI;

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


import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.*;
import java.util.Scanner;


public class FunctionView {
    public static JSplitPane sp;
    public static PublicCompoent publicCompoent = new PublicCompoent();
    public static UserManageCompoent userManageCompoent = new UserManageCompoent();
    public static PrivateCompoent privateCompoent = new PrivateCompoent();
    public static ShangXiaCompoent shangXiaCompoent = new ShangXiaCompoent();
    public static ChaXunCompoent chaXunCompoent = new ChaXunCompoent();
    public static DeleteCompoent deleteCompoent = new DeleteCompoent();
    public static WenJianconpoent wenJianconpoent = new WenJianconpoent();
    public static YinShenCompoent yinShenCompoent = new YinShenCompoent();
    final int WIDTH = 1000;
    final int HEIGHT = 700;
    JFrame jf = new JFrame("弎屲ChatRoom");


    //程序入口
    public static void main(String[] args) {
        new FunctionView().init();
    }


    //组装视图
    public void init() {
        //设置窗口的相关属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setVisible(false);
        try {
            jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("04.JPG"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        BackGroundPanel bgPanel = null;
        BackGroundPanel bgPanel1 = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("01.jpg"))));
            bgPanel1 = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("07.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置窗口内容
        JMenuBar jbar = new JMenuBar();
        JMenu j = null;
        if (MainView.ss == null) {
            j = new JMenu(RegisterView.ss);
        } else {
            j = new JMenu(MainView.ss);
        }

        JMenuItem j1 = new JMenuItem("切换账号");
        JMenuItem j3 = new JMenuItem("隐身上线");
        JMenuItem j2 = new JMenuItem("退出程序");
        j1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MainView.ss == null) {
                    RegisterView.cli.stop();
                } else {
                    MainView.cli.stop();
                }

                User us = new User(MainView.ss, null, MsgTypeConfig.EXIT_MSG, System.currentTimeMillis());
                try {
                    MainView.out.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                JOptionPane.showMessageDialog(jf, "聊天室即将退出");
                try {
                    MainView.sk.close();
                    jf.dispose();
                    new MainView().init();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        j2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (MainView.ss == null) {
                    RegisterView.cli.stop();
                } else {
                    MainView.cli.stop();
                }

                User us = new User(MainView.ss, null, MsgTypeConfig.EXIT_MSG, System.currentTimeMillis());
                try {
                    MainView.out.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                JOptionPane.showMessageDialog(jf, "聊天室即将退出");
                try {
                    MainView.sk.close();
                    System.exit(0);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        j3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_MSG,System.currentTimeMillis());
                try {
                    MainView.out.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

            }
        });
        j.add(j1);
        j.add(j3);
        j.add(j2);

        jbar.add(j);
        jf.setJMenuBar(jbar);

        //设置分割页面
        sp = new JSplitPane();
        //连续布局
        sp.setContinuousLayout(true);
        sp.setDividerLocation(1);
        sp.setDividerSize(10);
        DefaultMutableTreeNode gn = new DefaultMutableTreeNode("功能选项");
        DefaultMutableTreeNode gn1 = new DefaultMutableTreeNode("上下线提醒");
        DefaultMutableTreeNode gn2 = new DefaultMutableTreeNode("获取在线列表");
        DefaultMutableTreeNode gn3 = new DefaultMutableTreeNode("私人频道聊天");
        DefaultMutableTreeNode gn4 = new DefaultMutableTreeNode("公共频道聊天");
        DefaultMutableTreeNode gn5 = new DefaultMutableTreeNode("用户文件发送");
        DefaultMutableTreeNode gn6 = new DefaultMutableTreeNode("切换在线隐身");
        DefaultMutableTreeNode gn7 = new DefaultMutableTreeNode("查询聊天记录");
        DefaultMutableTreeNode gn8 = new DefaultMutableTreeNode("删除聊天记录");
        DefaultMutableTreeNode gn9 = new DefaultMutableTreeNode("下线");
        gn.add(gn1);
        gn.add(gn2);
        gn.add(gn3);
        gn.add(gn4);
        gn.add(gn5);
        gn.add(gn6);
        gn.add(gn7);
        gn.add(gn8);
        gn.add(gn9);
        Color color = new Color(208, 208, 208);
        Color color1 = new Color(151, 151, 152);
        JTree jTree = new JTree(gn);
        jTree.setFont(new java.awt.Font("楷体", 1, 15));
        jTree.setBackground(color);
        Myrend my = new Myrend();
        my.setBackgroundNonSelectionColor(color);
        my.setBackgroundSelectionColor(color1);
        jTree.setCellRenderer(my);

        //默认选中
        jTree.setSelectionRow(0);
        jTree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                Object l = e.getNewLeadSelectionPath().getLastPathComponent();
                if (gn1.equals(l)) {

                    sp.setRightComponent(shangXiaCompoent);
                    sp.setDividerLocation(200);

                }
                if (gn2.equals(l)) {
                    sp.setRightComponent(userManageCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn3.equals(l)) {
                    sp.setRightComponent(privateCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn4.equals(l)) {
                    sp.setRightComponent(publicCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn5.equals(l)) {
                    sp.setRightComponent(wenJianconpoent);
                    sp.setDividerLocation(200);
                }
                if (gn6.equals(l)) {
                    sp.setRightComponent(yinShenCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn7.equals(l)) {
                    sp.setRightComponent(chaXunCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn8.equals(l)) {
                    sp.setRightComponent(deleteCompoent);
                    sp.setDividerLocation(200);
                }
                if (gn9.equals(l)) {
                    if (MainView.ss == null) {
                        RegisterView.cli.stop();
                    } else {
                        MainView.cli.stop();
                    }

                    User us = new User(MainView.ss, null, MsgTypeConfig.EXIT_MSG, System.currentTimeMillis());
                    try {
                        MainView.out.writeObject(us);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(jf, "聊天室即将退出");
                    try {
                        MainView.sk.close();
                        System.exit(0);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }

                }
            }
        });
        sp.setRightComponent(new TiShi());

        sp.setLeftComponent(jTree);
        jf.add(sp);

        jf.setVisible(true);
    }

    //自定义绘制节点器
    private class Myrend extends DefaultTreeCellRenderer {
        private ImageIcon r = null;
        private ImageIcon r1 = null;
        private ImageIcon r2 = null;
        private ImageIcon r3 = null;
        private ImageIcon r4 = null;
        private ImageIcon r5 = null;
        private ImageIcon r6 = null;
        private ImageIcon r7 = null;
        private ImageIcon r8 = null;
        private ImageIcon r9 = null;

        public Myrend() {
            InputStream inputStream;
            r = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r1 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r2 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r3 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r4 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r5 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r6 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r7 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r8 = new ImageIcon(PathUtils.getRealPath("11.jpg"));
            r9 = new ImageIcon(PathUtils.getRealPath("11.jpg"));

        }

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            //使用默认绘制
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            ImageIcon image = null;
            switch (row) {
                case 0:
                    image = r;
                    break;
                case 1:
                    image = r1;
                    break;
                case 2:
                    image = r2;
                    break;
                case 3:
                    image = r3;
                    break;
                case 4:
                    image = r4;
                    break;
                case 5:
                    image = r5;
                    break;
                case 6:
                    image = r6;
                    break;
                case 7:
                    image = r7;
                    break;
                case 8:
                    image = r8;
                    break;
                case 9:
                    image = r9;
                    break;

            }
            this.setIcon(image);
            return this;
        }
    }
}

package SanWa.UI;

import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import SanWa.Util.PathUtils;
import SanWa.Util.ScreenUtils;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;
import java.net.Socket;
import java.util.Properties;

public class MainView {
    public static ObjectInputStream in;
    public static ObjectOutputStream out;
    public static String ss = null;
    public static String pp = null;
    public static String ii = null;
    public static int dd = 0;
    public static Socket sk;
    public static ClientReadMsgThread cli;
    static JTextField uField;
    final int WIDTH = 600;
    final int HEIGHT = 600;
    JFrame jf = new JFrame("弎屲聊天室");
    JTextField pField;
    JTextField d1Field;
    JTextField d2Field;

    //程序入口
    public static void main(String[] args) {

        new MainView().init();

    }

    //组装视图
    public void init() {
        //设置窗口的相关属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setVisible(false);
        try {
            jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("04.JPG"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置窗口内容
        BackGroundPanel bgPanel = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //组装登录相关的元素
        Box vBox = Box.createVerticalBox();
        //封装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel uLabel = new JLabel("用户名:");
        uField = new JTextField("请输入用户名", 16);
        uField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (uField.getText().equals("请输入用户名")) {
                    uField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (uField.getText().equals("")) {
                    uField.setText("请输入用户名");
                }

            }
        });
        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(25));
        uBox.add(uField);

        //封装密码
        Box pBox = Box.createHorizontalBox();
        JLabel pLabel = new JLabel("密     码:");
        pField = new JTextField("请输入密码", 16);
        pField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (pField.getText().equals("请输入密码")) {
                    pField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (pField.getText().equals("")) {
                    pField.setText("请输入密码");

                }
            }
        });
        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(25));
        pBox.add(pField);

        Box dBox = Box.createHorizontalBox();
        Box d1Box = Box.createHorizontalBox();
        JLabel dLabel = new JLabel("地    址:");
        d1Field = new JTextField("192.168.17.", 23);
        d1Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

                if (d1Field.getText().equals("请输入您的IP地址!")) {
                    d1Field.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (d1Field.getText().equals("")) {
                    d1Field.setText("请输入您的IP地址!");
                }

            }
        });
        JLabel d1Label = new JLabel("端    口:");
        d2Field = new JTextField("8888", 23);
        d2Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

                if (d2Field.getText().equals("请输入您的端口号!")) {
                    d2Field.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (d2Field.getText().equals("")) {
                    d2Field.setText("请输入您的端口号!");
                }
            }
        });
        dBox.add(dLabel);
        dBox.add(Box.createHorizontalStrut(25));
        dBox.add(d1Field);
        dBox.add(Box.createHorizontalStrut(15));
        d1Box.add(d1Label);
        d1Box.add(Box.createHorizontalStrut(25));
        d1Box.add(d2Field);
        d1Box.add(Box.createHorizontalStrut(15));

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("登录");
        JButton clcBtn = new JButton("重置");
        Login lg = new Login();
        loginBtn.addActionListener(lg);

        clcBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pField.setText("请输入密码");
                uField.setText("请输入用户名");
                d2Field.setText("请输入您的端口号!");
                d1Field.setText("请输入您的IP地址!");
            }
        });

        JButton registBtn = new JButton("注册");
        registBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegisterView().init();
                jf.dispose();
            }
        });
        //退出按钮
        Box closeBox = Box.createHorizontalBox();
        JButton coBtn = new JButton("退出");
        coBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        closeBox.add(coBtn);


        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(55));
        btnBox.add(clcBtn);
        btnBox.add(Box.createHorizontalStrut(55));
        btnBox.add(registBtn);

        vBox.add(Box.createVerticalStrut(70));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(dBox);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(d1Box);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(btnBox);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(closeBox);

        bgPanel.add(vBox);
        jf.add(bgPanel);

        jf.setVisible(true);
    }

    //事件监听
    class Login implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            String str0 = uField.getText();
            String str1 = pField.getText();

            try {
                File file = new File("ChatRoom\\info.properties");
                if (!file.exists()) {
                    file.createNewFile();
                }
                Properties pr = new Properties();

                pr.load(new FileInputStream("ChatRoom\\info.properties"));

                if (pr.containsKey(str0)) {

                    if (pr.getProperty(str0).equals(str1)) {

                        String iprule = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
                        if (d1Field.getText().trim().matches(iprule)) {
                            dd = Integer.parseInt(d2Field.getText().trim());
                            if (dd >= 7777 && dd <= 65535) {
                                ss = uField.getText().trim();
                                pp = pField.getText().trim();
                                ii = d1Field.getText().trim();
                                dd = Integer.parseInt(d2Field.getText().trim());
                                sk = new Socket(ii, dd);
                                //获取通道中的输入输出流
                                out = new ObjectOutputStream(sk.getOutputStream());
                                in = new ObjectInputStream(sk.getInputStream());

                                out.writeObject(new User(ss, null, null, 0));
                                //读取服务端的反馈
                                User user = (User) in.readObject();
                                if (user.getUsername().equals("通过验证")) {
                                    cli = new ClientReadMsgThread(sk);
                                    cli.start();
                                    new FunctionView().init();
                                    jf.dispose();
                                    User us = new User(MainView.ss, null, MsgTypeConfig.SHANGXIAN, System.currentTimeMillis());
                                    try {
                                        MainView.out.writeObject(us);
                                    } catch (IOException ee) {
                                        ee.printStackTrace();
                                    }
                                } else {
                                    JOptionPane.showMessageDialog(null, "未通过服务端的验证!该用户已经在线!");
                                }
                            } else {
                                JOptionPane.showMessageDialog(jf, "端口号有误!请重新输入!");
                            }
                        } else {
                            JOptionPane.showMessageDialog(jf, "IP地址有误!请重新输入!");
                        }


                    } else {
                        JOptionPane.showMessageDialog(null, "密码错误!请重新输入!");
                    }
                } else {
                    if (str0.equals("")) {
                        JOptionPane.showMessageDialog(null, "用户名为空!请输入用户名!");
                    } else {
                        JOptionPane.showMessageDialog(null, "用户名不存在!");
                    }

                }
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
    }
}

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);

    }
}

package SanWa.UI;

import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;

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 PublicCompoent extends Box {
    private JPanel c1;
    private JPanel c2;
    String setMessage;
    public static JTextArea infortextArea;
    public static JTextArea infortextArea1;
    public static ObjectOutputStream send= MainView.out;


    public PublicCompoent() {
        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(50,50,50);
        d1Label.setFont(new java.awt.Font("楷体", 1, 30));
        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,330));
        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);
        c2.add(btn1Panel);
        this.add(c2);



        Box uBox = Box.createHorizontalBox();
        uBox.setPreferredSize(new Dimension(850,180));
        infortextArea = new JTextArea();
        infortextArea.setFont(new java.awt.Font("楷体", 1, 16));
        infortextArea.setBounds(0, 0, 850, 200);
        infortextArea.setBackground(new Color(208,208,208));
        JScrollPane scrollPane_1 = new JScrollPane();
        uBox.add(scrollPane_1);
        scrollPane_1.setViewportView(infortextArea);
        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();


                if(!Objects.equals(setMessage, "")){
                    User us = new User(MainView.ss,setMessage, MsgTypeConfig.PUBLIC_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+"MEto.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, "不能发送空的消息!请输入消息!");
                }

            }

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


    }
}

package SanWa.UI;

import SanWa.Util.PathUtils;
import SanWa.Util.ScreenUtils;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;
import java.net.Socket;
import java.util.Properties;


public class RegisterView {
    public static ClientReadMsgThread cli;
    public static ObjectInputStream in;
    public static ObjectOutputStream out;
    public static String ss = null;
    public static int dd = 0;
    final int WIDTH = 700;
    final int HEIGHT = 600;
    JFrame jf = new JFrame("弎屲账号注册");
    JTextField uField;
    JTextField pField;
    JTextField p1Field;
    JTextField d1Field;
    JTextField d2Field;

    //程序入口
    public static void main(String[] args) {
        new RegisterView().init();
    }

    //组装视图
    public void init() {
        //设置窗口的相关属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setVisible(false);
        try {
            jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("04.JPG"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置窗口内容
        BackGroundPanel bgPanel = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //组装登录相关的元素
        Box vBox = Box.createVerticalBox();
        //封装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel uLabel = new JLabel("用户名:");
        uField = new JTextField("请输入大小写字母汉字与数字,长度2到16位!", 23);
        uField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (uField.getText().equals("请输入大小写字母汉字与数字,长度2到16位!")) {
                    uField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (uField.getText().equals("")) {
                    uField.setText("请输入大小写字母汉字与数字,长度2到16位!");
                }

            }
        });
        JButton ck1Btn = new JButton("校验");
        ck1Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = uField.getText().trim();
                Properties pr = null;
                File file = new File("ChatRoom\\info.properties");
                try {
                    file.createNewFile();
                    pr = new Properties();
                    pr.load(new FileInputStream("ChatRoom\\info.properties"));
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                if (pr.containsKey(username)) {
                    JOptionPane.showMessageDialog(null, "用户已存在!请重新输入用户名!");
                } else {
                    if (username.matches("[\u4e00-\u9fa5a-zA-Z1-9]{2,16}")) {
                        JOptionPane.showMessageDialog(jf, "用户名格式正确!");
                    } else {
                        JOptionPane.showMessageDialog(jf, "用户名格式有误!请重新输入!");
                    }
                }
            }
        });
        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(25));
        uBox.add(uField);
        uBox.add(Box.createHorizontalStrut(15));
        uBox.add(ck1Btn);

        //封装密码
        Box pBox = Box.createHorizontalBox();
        JLabel pLabel = new JLabel("密     码:");
        pField = new JTextField("请输入大写小写字母与数字,长度6到16位!", 23);
        pField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (pField.getText().equals("请输入大写小写字母与数字,长度6到16位!")) {
                    pField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (pField.getText().equals("")) {
                    pField.setText("请输入大写小写字母与数字,长度6到16位!");
                }
            }
        });
        JButton ck2Btn = new JButton("校验");
        ck2Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = pField.getText().trim();
                if (username.matches("[a-zA-Z1-9]{6,16}")) {
                    JOptionPane.showMessageDialog(jf, "密码格式正确!");
                } else {
                    JOptionPane.showMessageDialog(jf, "密码格式有误!请重新输入!");
                }
            }
        });
        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(25));
        pBox.add(pField);
        pBox.add(Box.createHorizontalStrut(15));
        pBox.add(ck2Btn);



        Box p1Box = Box.createHorizontalBox();
        JLabel p1Label = new JLabel("确    认:");
        p1Field = new JTextField("请再次输入密码!", 23);
        p1Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

                if (p1Field.getText().equals("请再次输入密码!")) {
                    p1Field.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (p1Field.getText().equals("")) {
                    p1Field.setText("请再次输入密码!");
                }
            }
        });
        JButton ck5Btn = new JButton("校验");
        ck5Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String p = pField.getText().trim();
                String p1 = p1Field.getText().trim();

                if (p.equals(p1)) {
                    JOptionPane.showMessageDialog(jf, "密码确认成功!");
                } else {
                    JOptionPane.showMessageDialog(jf, "两次输入密码不一致!请重新输入!");
                }
            }
        });
        p1Box.add(p1Label);
        p1Box.add(Box.createHorizontalStrut(25));
        p1Box.add(p1Field);
        p1Box.add(Box.createHorizontalStrut(15));
        p1Box.add(ck5Btn);


        Box dBox = Box.createHorizontalBox();
        Box d1Box = Box.createHorizontalBox();
        JLabel dLabel = new JLabel("地    址");
        d1Field = new JTextField("请输入您的IP地址!", 23);
        d1Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

                if (d1Field.getText().equals("请输入您的IP地址!")) {
                    d1Field.setText("");
                }
            }


            @Override
            public void focusLost(FocusEvent e) {
                if (d1Field.getText().equals("")) {
                    d1Field.setText("请输入您的IP地址!");
                }

            }
        });
        JButton ck3Btn = new JButton("校验");
        ck3Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String iprule = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
                if (d1Field.getText().trim().matches(iprule)) {
                    JOptionPane.showMessageDialog(jf, "IP地址正确!");
                } else {
                    JOptionPane.showMessageDialog(jf, "IP地址有误!请重新输入!");
                }
            }
        });
        JLabel d1Label = new JLabel("端    口");
        d2Field = new JTextField("请输入您的端口号:7777-65535!", 23);
        d2Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

                if (d2Field.getText().equals("请输入您的端口号:7777-65535!")) {
                    d2Field.setText("");
                }
            }
            /*
            请输入大小写字母汉字与数字,长度2到16位!
            请输入大写小写字母与数字,长度6到16位!
            请再次输入密码!
            请输入您的IP地址!
            请输入您的端口号:7777-65535!
             */
            @Override
            public void focusLost(FocusEvent e) {
                if (d2Field.getText().equals("")) {
                    d2Field.setText("请输入您的端口号:7777-65535!");
                }
            }
        });
        JButton ck4Btn = new JButton("校验");
        ck4Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dd = Integer.parseInt(d2Field.getText().trim());
                if (dd >= 7777 && dd <= 65535) {
                    JOptionPane.showMessageDialog(jf, "端口号正确!");
                } else {
                    JOptionPane.showMessageDialog(jf, "端口号有误!请重新输入!");
                }
            }
        });
        dBox.add(dLabel);
        dBox.add(Box.createHorizontalStrut(25));
        dBox.add(d1Field);
        dBox.add(Box.createHorizontalStrut(15));
        dBox.add(ck3Btn);
        d1Box.add(d1Label);
        d1Box.add(Box.createHorizontalStrut(25));
        d1Box.add(d2Field);
        d1Box.add(Box.createHorizontalStrut(15));
        d1Box.add(ck4Btn);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("注册");
        JButton clcBtn = new JButton("重置");
        Regist re = new Regist();
        loginBtn.addActionListener(re);


        clcBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                uField.setText("请输入大写小写字母与数字,长度6到16位!");
                pField.setText("请输入大写小写字母与数字,长度6到16位!");
                d2Field.setText("请输入您的端口号!");
                p1Field.setText("请再次输入密码!");
                d1Field.setText("请输入您的IP地址!");
            }
        });

        JButton registBtn = new JButton("登录");
        registBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                new MainView().init();
                jf.dispose();
            }
        });
        //退出按钮
        Box closeBox = Box.createHorizontalBox();
        JButton co1Btn = new JButton("关于我们");
        co1Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "(⑅˃◡˂⑅)沈老师和他的“孩子们”(づ◡ど)");
            }
        });
        JButton coBtn = new JButton("退出");
        coBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        closeBox.add(coBtn);
        closeBox.add(Box.createHorizontalStrut(110));
        closeBox.add(co1Btn);


        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(70));
        btnBox.add(clcBtn);
        btnBox.add(Box.createHorizontalStrut(70));
        btnBox.add(registBtn);

        vBox.add(Box.createVerticalStrut(80));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(p1Box);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(dBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(d1Box);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(btnBox);
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(closeBox);

        bgPanel.add(vBox);
        jf.add(bgPanel);

        jf.setVisible(true);
    }

    //事件监听
    class Regist implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String str0 = uField.getText();
            String str1 = pField.getText();
            String str2 = p1Field.getText();
            String str3 = d1Field.getText();
            String str4 = d2Field.getText();
           // dd = Integer.parseInt(d2Field.getText().trim());
            try {
                File file = new File("ChatRoom\\info.properties");
                if (!file.exists()) {
                    file.createNewFile();
                }

                Properties pr = new Properties();
                pr.load(new FileInputStream("ChatRoom\\info.properties"));
                File file1 = new File("ChatRoom\\info1.properties");
                file1.createNewFile();
                Properties pr1 = new Properties();
                pr1.load(new FileInputStream("ChatRoom\\info1.properties"));
                       /*
            请输入大小写字母汉字与数字,长度2到16位!
            请输入大写小写字母与数字,长度6到16位!
            请再次输入密码!
            请输入您的IP地址!
            请输入您的端口号:7777-65535!
            &&!(str0.equals("请输入大小写字母汉字与数字,长度2到16位!"))&&!(str0.equals(""))
             */
                if (pr.containsKey(str0)) {
                    JOptionPane.showMessageDialog(null, "用户已存在!请重新输入用户名!");
                } else {
                    if (str0.matches("[\u4e00-\u9fa5a-zA-Z1-9]{2,16}")) {
                        if (str1.matches("[a-zA-Z1-9]{6,16}")) {
                            if (str1.equals(str2)) {
                                String iprule = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
                                if (str3.matches(iprule)) {
                                    if (Integer.parseInt(d2Field.getText().trim()) >= 7777 && Integer.parseInt(d2Field.getText().trim()) <= 65535) {
                                        JOptionPane.showMessageDialog(jf, "注册成功!");

                                        pr.setProperty(str0, str1);
                                        pr1.setProperty(str3, str4);
                                        pr1.store(new FileOutputStream("ChatRoom\\info1.properties"), null);
                                        pr.store(new FileOutputStream("ChatRoom\\info.properties"), null);
                                    } else {
                                        JOptionPane.showMessageDialog(jf, "端口号有误!请重新输入!");
                                    }
                                } else {
                                    JOptionPane.showMessageDialog(jf, "IP地址有误!请重新输入!");
                                }


                            } else {
                                JOptionPane.showMessageDialog(jf, "两次输入密码不一致!请重新输入!");
                            }
                        } else {

                            JOptionPane.showMessageDialog(jf, "密码格式有误!请重新输入!");
                        }
                    } else {
                        JOptionPane.showMessageDialog(jf, "用户名格式有误!请重新输入!");
                    }
                }


            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
    }
}

package SanWa.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ShangXiaCompoent extends Box {
    public static JTextArea infortextArea1;

    public ShangXiaCompoent() {
        super(BoxLayout.Y_AXIS);

        //垂直布局

        //组装视图
this.setBackground(new Color(208,208,208));
        Box u1Box = Box.createHorizontalBox();
        u1Box.setPreferredSize(new Dimension(500, 550));
        infortextArea1 = new JTextArea();
        infortextArea1.setEditable(false);
        infortextArea1.setBounds(0, 0, 500, 200);
        infortextArea1.setBackground(new Color(208,208,208));
        JScrollPane scrollPane21 = new JScrollPane();
        scrollPane21.setBounds(0, 0, 850, 300);
        scrollPane21.setViewportView(infortextArea1);
        u1Box.add(scrollPane21);


        JPanel btnPanel = new JPanel();
        Color color = new Color(208,208,208);
        btnPanel.setBackground(new Color(50,50,50));
        btnPanel.setMaximumSize(new Dimension(1500, 80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JLabel d11Label = new JLabel("弎屲聊天室         ");
        d11Label.setBackground(new Color(50,50,50));
        d11Label.setForeground(Color.white);
        d11Label.setFont(new java.awt.Font("楷体", 1, 35));
        JButton getButton = new JButton("神秘按钮");
        getButton.setFont(new java.awt.Font("楷体", 1, 20));
        getButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "(⑅˃◡˂⑅)弎屲-常祥(づ◡ど)");


            }
        });
        btnPanel.add(d11Label);
        btnPanel.add(getButton);
        this.add(btnPanel);

        Box u5Box = Box.createHorizontalBox();

        JScrollPane scrollPane_5 = new JScrollPane();
        Box btn1Panel = Box.createHorizontalBox();
        btn1Panel.setPreferredSize(new Dimension(600, 60));
        JTextArea infortext = new JTextArea();
        infortext.setEditable(false);
        infortext.setText("    【欢 迎 来 到 弎 屲 聊 天 室】");
        infortext.setFont(new java.awt.Font("楷体", 1, 40));
        infortext.setBackground(color);
        JLabel d1Label = new JLabel("                     【欢 迎 来 到 弎 屲 聊 天 室】");
        d1Label.setFont(new java.awt.Font("楷体", 1, 20));
        scrollPane_5.setViewportView(infortext);
        u5Box.add(scrollPane_5);
        u5Box.setBackground(color);
        scrollPane_5.setBackground(color);
        this.add(u5Box);


        this.add(u1Box);
    }
}


package SanWa.UI;

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

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

    public TiShi() {

        //垂直布局
        super(BoxLayout.Y_AXIS);
        //this.setBackground(new Color(208,208,208));
        BackGroundPanel bgPanel = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x001.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Box u5Box = Box.createHorizontalBox();
        u5Box.setPreferredSize(new Dimension(5000,600));
        JScrollPane scrollPane_5 = new JScrollPane();
        scrollPane_5.setPreferredSize(new Dimension(5000,600));
        scrollPane_5.setViewportView(bgPanel);
        u5Box.add(scrollPane_5);
this.add(u5Box);
    }
}



package SanWa.UI;

import SanWa.Net.User;
import SanWa.Net.MsgTypeConfig;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class UserManageCompoent extends Box {
    public static JTextArea infortextArea1;
    public static ObjectOutputStream send= MainView.out;

    public UserManageCompoent() {

        //垂直布局
        super(BoxLayout.Y_AXIS);
        //组装视图

        Box u1Box = Box.createHorizontalBox();
        u1Box.setPreferredSize(new Dimension(850, 560));
        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);


        JPanel btnPanel = new JPanel();
        Color color = new Color(208,208,208);
        btnPanel.setBackground(new Color(50,50,50));
        btnPanel.setMaximumSize(new Dimension(1500, 80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JLabel d11Label = new JLabel("  当前在线成员列表           ");
        d11Label.setBackground(new Color(50,50,50));
        d11Label.setForeground(Color.white);
        d11Label.setFont(new java.awt.Font("楷体", 1, 35));
        JButton getButton = new JButton("获取");
        getButton.setFont(new java.awt.Font("楷体", 1, 20));
        getButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_LIST,System.currentTimeMillis());
                try {
                    send.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                infortextArea1.setText("");

            }
        });
        btnPanel.add(d11Label);
        btnPanel.add(getButton);
        this.add(btnPanel);

        Box u5Box = Box.createHorizontalBox(); u5Box.setBackground(new Color(208,208,208));
        JScrollPane scrollPane_5 = new JScrollPane();
        scrollPane_5.setBackground(new Color(208,208,208));
        Box btn1Panel = Box.createHorizontalBox();
        btn1Panel.setPreferredSize(new Dimension(600, 60));
        btn1Panel.setBackground(new Color(208,208,208));
        JLabel d1Label = new JLabel("【编号】    ");
        JLabel d2Label = new JLabel("【编号】    【姓名】    ");
        JLabel d3Label = new JLabel("【编号】    【姓名】    【IP 地 址】       ");
        JLabel d4Label = new JLabel("【编号】    【姓名】    【IP 地 址】       【端口号】   ");//组装视图
        JLabel d5Label = new JLabel("【编号】    【姓名】    【IP 地 址】       【端口号】   【本地端口号】    ");//组装视图
        JLabel d6Label = new JLabel("【编号】    【姓名】    【IP 地 址】       【端口号】   【本地端口号】    【在线状态】");//组装视图

        Color color1 = new Color(208,208,208);
        d1Label.setFont(new java.awt.Font("楷体", 1, 17));
        d2Label.setFont(new java.awt.Font("楷体", 1, 17));
        d3Label.setFont(new java.awt.Font("楷体", 1, 17));
        d4Label.setFont(new java.awt.Font("楷体", 1, 17));
        d5Label.setFont(new java.awt.Font("楷体", 1, 17));
        d6Label.setFont(new java.awt.Font("楷体", 1, 17));
        btn1Panel.setBackground(color1);
        btn1Panel.setMaximumSize(new Dimension(600, 40));
        btn1Panel.add(d1Label);
        btn1Panel.add(d2Label);
        btn1Panel.add(d3Label);
        btn1Panel.add(d4Label);
        btn1Panel.add(d5Label);
        btn1Panel.add(d6Label);
        JTextArea infortext = new JTextArea();
        infortext.setEditable(false);
        infortext.setText("【编号】  【姓名】   【IP地址】   【端口号】   【本地端口号】  【在线状态】");
        infortext.setFont(new java.awt.Font("楷体", 1, 20));
        infortext.setBackground(color);
        infortext.add(btn1Panel);
        scrollPane_5.setViewportView(infortext);
        u5Box.add(scrollPane_5);
        this.add(u5Box);
        this.add(u1Box);
    }
}

package SanWa.UI;

import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import SanWa.Util.FileUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Objects;

public class WenJianconpoent extends Box {
    public static int i;
    public static JTextArea infortextArea;
    public static JTextArea infortextArea1;
    public static JTextArea infortextArea21;
    public static JTextArea infortextArea22;
    public static JButton checkButton;
    public static JButton checkButtonxx;
    private JPanel c2;
    private JPanel c21;
    private JPanel c22;

    public WenJianconpoent() {

        //垂直布局
        super(BoxLayout.Y_AXIS);
        Font fon = new Font("黑体", 1, 12);

        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 u3Box = Box.createHorizontalBox();
        Box u4Box = Box.createHorizontalBox();
        JScrollPane scrollPane_8 = new JScrollPane();
        JScrollPane scrollPane_9 = new JScrollPane();
        JScrollPane scrollPane_7 = new JScrollPane();
        JScrollPane scrollPane_6 = new JScrollPane();

        JLabel pLabel1 = new JLabel("请在此输入发送文件路径:");
        pLabel1.setFont(new Font("黑体", 1, 16));
        pLabel1.setPreferredSize(new Dimension(200, 30));
        c21 = new JPanel();
        c21.setLayout(new FlowLayout(FlowLayout.LEFT));
        c21.setBackground(new Color(208,208,208));
        c21.setPreferredSize(new Dimension(600, 30));
        JPanel btn1Panel1 = new JPanel();
        Color color1 = new Color(208,208,208);
        btn1Panel1.setBackground(color1);
        btn1Panel1.add(pLabel1);
        JPanel btn11Panel11 = new JPanel();
        Color color11 = new Color(208,208,208);
        infortextArea21 = new JTextArea();
        infortextArea21.setFont(new Font("黑体", 1, 18));
        infortextArea21.setPreferredSize(new Dimension(400, 29));
        infortextArea21.setBackground(new Color(208,208,208));
        infortextArea21.append("");
        btn11Panel11.setLayout(new FlowLayout(FlowLayout.RIGHT));
        btn11Panel11.setBackground(color11);
        btn11Panel11.add(infortextArea21);
        scrollPane_8.setViewportView(infortextArea21);
        c21.add(btn1Panel1);
        c21.add(scrollPane_8);
        u3Box.add(c21);
        scrollPane_7.setViewportView(u3Box);
        this.add(scrollPane_7);

        JLabel pLabel2 = new JLabel("请在此输入文件的接收者:");
        pLabel2.setFont(new Font("黑体", 1, 16));
        pLabel2.setPreferredSize(new Dimension(200, 30));
        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(pLabel2);
        JPanel btn11Panel22 = new JPanel();
        infortextArea22 = new JTextArea();
        infortextArea22.setFont(new Font("黑体", 1, 18));
        infortextArea22.setPreferredSize(new Dimension(400, 29));
        infortextArea22.setBackground(new Color(208,208,208));
        infortextArea22.append("");

        JButton checkButton2 = new JButton("发送文件");
        checkButton2.setPreferredSize(new Dimension(85, 39));
        checkButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String receiver = infortextArea22.getText();
                String path = infortextArea21.getText();
                if (!Objects.equals(path, "")) {
                    if (!Objects.equals(receiver, "")) {
                        File file = new File(path);
                        if (file.exists()) {
                            byte[] bytes = FileUtils.readFile(path);
                            User user = new User(receiver, MainView.ss, MsgTypeConfig.WENJIAN, System.currentTimeMillis(), file.getName(), file.length(), bytes);
                            try {
                                MainView.out.writeObject(user);
                                infortextArea1.append("您正在给"+receiver+"用户发送文件!等待对方接受或拒绝!"+"\n");
                            } catch (IOException ioException) {
                                ioException.printStackTrace();
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "文件找不到!请重新输入路径名!");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "接收者为空!请输入接收者!");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "路径为空!请输入路径名!");
                }

            }
        });
        btn11Panel22.setLayout(new FlowLayout(FlowLayout.RIGHT));
        btn11Panel22.setBackground(color11);
        btn11Panel22.add(infortextArea22);
        scrollPane_9.setViewportView(infortextArea22);
        c22.add(btn1Panel2);
        c22.add(scrollPane_9);
        c22.add(checkButton2);
        u4Box.add(c22);
        scrollPane_6.setViewportView(u4Box);
        this.add(scrollPane_6);


        Box u1Box = Box.createHorizontalBox();
        u1Box.setPreferredSize(new Dimension(850, 190));
        JLabel uLabel = new JLabel(" ");
        infortextArea1 = new JTextArea();
        infortextArea1.setEditable(false);
        infortextArea1.setFont(new Font("黑体", 1, 18));
        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);

        Box u5Box = Box.createHorizontalBox();
        JScrollPane scrollPane_5 = new JScrollPane();
        JLabel pLabel = new JLabel("请在此选择是否接受文件!");
        pLabel.setFont(new Font("黑体", 1, 16));
        pLabel.setPreferredSize(new Dimension(210, 30));
        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();
        btn1Panel.setBackground(color1);
        btn1Panel.add(pLabel);

        checkButton = new JButton("接受文件");

        checkButtonxx = new JButton("拒绝文件");
        checkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ClientReadMsgThread.isSave = false;
            }
        });
       checkButtonxx.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ClientReadMsgThread.isClose = true;
            }
        });

        c2.add(btn1Panel);
        c2.add(checkButton);
        c2.add(checkButtonxx);
        u5Box.add(c2);
        scrollPane_5.setViewportView(u5Box);
        this.add(scrollPane_5);


        Box uBox = Box.createHorizontalBox();
        uBox.setPreferredSize(new Dimension(850, 190));
        infortextArea = new JTextArea();
infortextArea.setFont(new Font("黑体", 1, 18));
infortextArea.setEditable(false);
        infortextArea.setBackground(new Color(208,208,208));
        JScrollPane scrollPane_1 = new JScrollPane();

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


        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 getButton1 = new JButton("打开文件夹");
        getButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File file = new File("E:\\WenJian");
                if (!file.exists()) {
                    file.mkdirs();
                }
                try {
                    Desktop.getDesktop().open(file);
                } catch (IOException e12) {
                    e12.printStackTrace();
                }
            }
        });
        btnPanel1.add(getButton1);
        this.add(btnPanel1);

    }
}

package SanWa.UI;

import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;


public class YinShenCompoent extends Box {
    public static JTextArea infortextArea1;
    public static ObjectOutputStream send= MainView.out;


    public YinShenCompoent() {

        //垂直布局
        super(BoxLayout.Y_AXIS);
        //组装视图

        Box u1Box = Box.createHorizontalBox();
        u1Box.setPreferredSize(new Dimension(850, 505));
        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);


        JPanel btnPanel = new JPanel();
        Color color = new Color(208,208,208);
        btnPanel.setBackground(new Color(50,50,50));
        btnPanel.setMaximumSize(new Dimension(1500, 80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JLabel d11Label = new JLabel("  当前在线成员列表    ");
        d11Label.setBackground(new Color(50,50,50));
        d11Label.setForeground(Color.white);
        d11Label.setFont(new java.awt.Font("楷体", 1, 35));
        JButton getButton = new JButton("获取");
        getButton.setFont(new java.awt.Font("楷体", 1, 20));
        getButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_LIST1,System.currentTimeMillis());
                try {
                    send.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                infortextArea1.setText("");

            }
        });
        JButton getButton1 = new JButton("刷新");
        getButton1.setFont(new java.awt.Font("楷体", 1, 20));
        getButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_LIST1,System.currentTimeMillis());
                try {
                    send.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                infortextArea1.setText("");

            }
        });
        btnPanel.add(d11Label);
        btnPanel.add(getButton);
        btnPanel.add(getButton1);
        this.add(btnPanel);


        Box u5Box = Box.createHorizontalBox();
u5Box.setPreferredSize(new Dimension(650,15));
        JScrollPane scrollPane_5 = new JScrollPane();
        scrollPane_5.setPreferredSize(new Dimension(650,10));
        JPanel btn1Panel = new JPanel();
       // btn1Panel.setPreferredSize(new Dimension(650, 30));
        btn1Panel.setBackground(new Color(208,208,208));
        JLabel d1Label = new JLabel("【编号】    ");
        JLabel d2Label = new JLabel("【姓名】    ");
        JLabel d3Label = new JLabel("【IP 地 址】    ");
        JLabel d4Label = new JLabel("【端口号】   ");//组装视图
        JLabel d5Label = new JLabel("【本地端口号】   ");//组装视图
        JLabel d6Label = new JLabel("【在线状态】");//组装视图

        Color color1 = new Color(208,208,208);
        d1Label.setFont(new java.awt.Font("楷体", 1, 17));
        d2Label.setFont(new java.awt.Font("楷体", 1, 17));
        d3Label.setFont(new java.awt.Font("楷体", 1, 17));
        d4Label.setFont(new java.awt.Font("楷体", 1, 17));
        d5Label.setFont(new java.awt.Font("楷体", 1, 17));
        d6Label.setFont(new java.awt.Font("楷体", 1, 17));
        btn1Panel.setBackground(color1);
        btn1Panel.setMaximumSize(new Dimension(650, 35));
        btn1Panel.add(d1Label);
        btn1Panel.add(d2Label);
        btn1Panel.add(d3Label);
        btn1Panel.add(d4Label);
        btn1Panel.add(d5Label);
        btn1Panel.add(d6Label);

        scrollPane_5.setViewportView(btn1Panel);u5Box.add(scrollPane_5);
        this.add(u5Box);


        this.add(u1Box);

        JPanel huan = new JPanel();
        Color ys = new Color(208,208,208);
        huan.setBackground(ys);
        huan.setMaximumSize(new Dimension(1500, 80));
        huan.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JButton yinshen = new JButton("隐身");
        yinshen.setFont(new java.awt.Font("楷体", 1, 20));
        yinshen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_MSG,System.currentTimeMillis());
                try {
                    send.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

            }
        }); JButton zaixian = new JButton("上线");
        zaixian.setFont(new java.awt.Font("楷体", 1, 20));
        zaixian.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User us = new User(MainView.ss,null,null,  MsgTypeConfig.ONLINE_MSG,System.currentTimeMillis());
                try {
                    send.writeObject(us);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

            }
        });
        huan.add(yinshen);
        huan.add(zaixian);
        this.add(huan);
    }
}

package SanWa.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileUtils {
    public static byte[] readFile(String path) {
        File file = new File(path);
        byte datas[] = null;
        if (!file.exists()) {
            datas = null;
        } else {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                FileInputStream fis = new FileInputStream(file);
                byte data[] = new byte[1024 * 1024];
                int len = 0;
                while ((len = fis.read(data)) > 0) {
                    baos.write(data, 0, len);
                }
                datas = baos.toByteArray();
                baos.flush();
                baos.close();
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return datas;
    }

    public static boolean writeFile(String path, byte datas[]) {
        try {
            File file = new File(path);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(datas);
            fos.flush();
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

package SanWa.Util;

public class PathUtils {
    //获取路径
    private static final String IMG_PATH="ChatRoom\\Image\\";
    public static String getRealPath(String relativePath) {
        return IMG_PATH + relativePath;
    }
}

package SanWa.Util;

import java.awt.*;

public class ScreenUtils {
    //获取电脑屏幕的宽度
    public static int getScreenWidth() {
        return Toolkit.getDefaultToolkit().getScreenSize().width;
    }

    //获取电脑屏幕的高度
    public static int getScreenHeight() {
        return Toolkit.getDefaultToolkit().getScreenSize().height;
    }
}

服务端

package SanWa.Net;

public enum MsgTypeConfig {
     PRIVATE_MSG ,ONLINE_LIST1,SHANGXIAN,WENJIAN,FANKUI,
     PUBLIC_MSG ,
     ONLINE_MSG ,
     ONLINE_LIST ,
     EXIT_MSG;
}
package SanWa.Net;

import java.io.Serializable;
import java.net.Socket;
import java.util.HashMap;

public class User implements Serializable {
    static final long serialVersionUID = 456789L;
    //String zfMsg = username + ":" + content + ":" + msgType + ":" + System.currentTimeMillis();
    private String username;
    private String userSend;
    private String content;
    private MsgTypeConfig msgType;
    private long time;
    private HashMap<String, Socket> hm;
    private String fileName;
    private long fileLength;
    private byte fileData[];

    public User(String username, String userSend, MsgTypeConfig msgType, long time, String fileName, long fileLength, byte[] fileData) {
        this.username = username;
        this.userSend = userSend;
        this.msgType = msgType;
        this.time = time;
        this.fileName = fileName;
        this.fileLength = fileLength;
        this.fileData = fileData;
    }

    public User(String username, String content, MsgTypeConfig msgType, long time, HashMap<String, Socket> hm) {
        this.username = username;
        this.content = content;
        this.msgType = msgType;
        this.time = time;
        this.hm = hm;
    }

    public User(String username, String content, MsgTypeConfig msgType, long time) {
        this.username = username;
        this.content = content;
        this.msgType = msgType;
        this.time = time;
    }

    public User(String userSend, String username, String content, MsgTypeConfig msgType, long time) {
        this.userSend = userSend;
        this.username = username;
        this.content = content;
        this.msgType = msgType;
        this.time = time;
    }

    public User() {
    }

    public String getFileName() {
        return fileName;
    }

    public long getFileLength() {
        return fileLength;
    }

    public byte[] getFileData() {
        return fileData;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", userSend='" + userSend + '\'' +
                ", content='" + content + '\'' +
                ", msgType=" + msgType +
                ", time=" + time +
                ", hm=" + hm +
                '}';
    }

    public String getUserSend() {
        return userSend;
    }

    public HashMap<String, Socket> getHm() {
        return hm;
    }

    public MsgTypeConfig getMsgType() {
        return msgType;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }
}

package SanWa.UI;

import javax.swing.*;
import java.awt.*;

public class BackGroundPanel extends JPanel {
    //声明图片
    private Image backIcon;

    public BackGroundPanel(Image backIcon) {
        this.backIcon = backIcon;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //绘制背景
        g.drawImage(backIcon,0,0,this.getWidth(),this.getHeight(),null);

    }
}

package SanWa.UI;

import SanWa.Net.User;
import java.io.*;
import java.net.Socket;
import java.util.HashMap;

public class RegisterUserThread extends Thread {
    public static ObjectInputStream in;
    public static ObjectOutputStream out;
    public static String username;
    private Socket sk;
    private HashMap<String, Socket> hm;

    public RegisterUserThread(Socket sk, HashMap<String, Socket> hm) {
        this.sk = sk;
        this.hm = hm;
    }


    @Override
    public void run() {
        try {

            in = new ObjectInputStream(sk.getInputStream());
            out = new ObjectOutputStream(sk.getOutputStream());

            while (true) {
                User user = null;
                try {
                    user = (User) in.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                username = user.getUsername();
                if (!hm.containsKey(user.getUsername())) {
                    hm.put(user.getUsername(), sk);
                    String s = ":      " + user.getUsername() + "   " + sk.getInetAddress() + "      " + sk.getPort() + "         " +
                            sk.getLocalPort() + "       在线】";
                    Server.info.add(s);
                    out.writeObject(new User("通过验证", null, null, 0));
                    break;
                } else {
                    //给客户端反馈
                    out.writeObject(new User("拒绝", null, null, 0));
                }
            }
            new ServerReadMsgThread(sk, hm).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package SanWa.UI;

import SanWa.Util.PathUtils;
import SanWa.Util.ScreenUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

public class Server {
    public static JTextArea infortextArea1;
    public static Socket sk;
    public static ArrayList<String> info = new ArrayList<>();
    final int WIDTH = 600;
    final int HEIGHT = 600;
    JFrame jf = new JFrame("弎屲服务端");
    JTextField d2Field;

    //程序入口
    public static void main(String[] args) {

        new Server().init();


    }

    //组装视图
    public void init() {
        //设置窗口的相关属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setVisible(false);
        try {
            jf.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("04.JPG"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置窗口内容
        BackGroundPanel bgPanel = null;
        try {
            bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("x002.jpg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //组装登录相关的元素
        Box vBox = Box.createVerticalBox();
        Box dkBox = Box.createHorizontalBox();
        JLabel d1Label = new JLabel("请 输 入 您 的 端 口 号:");
        //d2Field = new JTextField("请输入您的端口号!", 23);
        d2Field = new JTextField("8888", 23);
        d2Field.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

                if (d2Field.getText().equals("请输入您的端口号!")) {
                    d2Field.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (d2Field.getText().equals("")) {
                    d2Field.setText("请输入您的端口号!");
                }
            }
        });
        dkBox.add(d1Label);
        dkBox.add(Box.createHorizontalStrut(25));
        dkBox.add(d2Field);
        Box u1Box = Box.createHorizontalBox();
        u1Box.setPreferredSize(new Dimension(250, 350));
        infortextArea1 = new JTextArea();
        infortextArea1.setBackground(new Color(217, 217, 217));
        JScrollPane scrollPane21 = new JScrollPane();
        scrollPane21.setViewportView(infortextArea1);
        u1Box.add(scrollPane21);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("启动服务器");
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                HashMap<String, Socket> hm = new HashMap<>();
                int dk = Integer.parseInt(d2Field.getText().trim());

                ServerSocket ss = null;
                try {
                    ss = new ServerSocket(dk);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                infortextArea1.append("服务器已经开启,等待连接。。。"+"\n");
                JOptionPane.showMessageDialog(null, "服务器已开启!");

                //jf.dispose();
                int i = 1;
                while (true) {
                    try {
                        sk = ss.accept();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
               infortextArea1.append((i++) + " 个客户端已经连接"+"\n");
                    new RegisterUserThread(sk, hm).start();
                }
            }
        });
        JButton clcBtn = new JButton("关闭服务器");
        clcBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });


        //退出按钮
        Box closeBox = Box.createHorizontalBox();
        JButton coBtn = new JButton("退出");
        coBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        closeBox.add(coBtn);


        JButton coBtn1 = new JButton("重置");
        coBtn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d2Field.setText("请输入您的端口号!");
            }
        });

        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(coBtn1);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(clcBtn);
        vBox.add(Box.createVerticalStrut(35));
        vBox.add(dkBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(u1Box);
        vBox.add(Box.createVerticalStrut(30));
        vBox.add(closeBox);

        bgPanel.add(vBox);
        jf.add(bgPanel);
        jf.setVisible(true);
    }
}
/*
package SanWa.UI;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

public class Server {
    public static HashMap<String, Socket> hm=new HashMap<>();
    public static Socket sk;
    public static ArrayList<String> info=new ArrayList<>();

    public static void main(String[] args) throws IOException {
        HashMap<String, Socket> hm = new HashMap<>();
        ServerSocket ss = new ServerSocket(8888);
        System.out.println("服务器已经开启,等待连接。。。");
        int i = 1;
        while (true) {
            sk = ss.accept();
            System.out.println((i++) + " 个客户端已经连接");
            new RegisterUserThread(sk,hm).start();
        }
    }
}
*/

package SanWa.UI;


import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;

import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Set;


public class ServerReadMsgThread extends Thread {

    public static User receiver = null;
    private Socket sk;
    private HashMap<String, Socket> hm;

    public ServerReadMsgThread(Socket sk, HashMap<String, Socket> hm) {
        this.sk = sk;
        this.hm = hm;
    }

    @Override
    public void run() {
        try {
            ObjectInputStream oin = RegisterUserThread.in;

            while (true) {
                try {
                    receiver = (User) oin.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                if (receiver.getMsgType() == (MsgTypeConfig.PRIVATE_MSG)) {


                    User sendUser = new User(receiver.getUsername(), receiver.getUserSend(), receiver.getContent(), receiver.getMsgType(), receiver.getTime());
                    ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUsername()).getOutputStream());
                    sendStream.writeObject(sendUser);
                } else if (receiver.getMsgType() == MsgTypeConfig.PUBLIC_MSG) {
                    Set<String> keySet = hm.keySet();
                    for (String key : keySet) {
                        if (key.equals(receiver.getUsername())) {
                            continue;
                        }
                        User sendUser = new User(receiver.getUsername(), receiver.getContent(), receiver.getMsgType(), receiver.getTime());
                        ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
                        sendStream.writeObject(sendUser);
                    }
                } else if (receiver.getMsgType() == MsgTypeConfig.SHANGXIAN) {
                    Set<String> keySet1 = hm.keySet();
                    ObjectOutputStream sendStream;
                    String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(receiver.getTime());
                    for (String key : keySet1) {
                        if (key.equals(RegisterUserThread.username)) {
                            String Msg = "【上线信息】:【用户:" + RegisterUserThread.username + ":" + "上线了】【" + timeStr + "】【我】";
                            User sendUser = new User(RegisterUserThread.username, null, Msg, MsgTypeConfig.SHANGXIAN, System.currentTimeMillis());
                            sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
                            sendStream.writeObject(sendUser);
                        } else {
                            String Msg = "【上线信息】:【用户:" + RegisterUserThread.username + ":" + "上线了】【" + timeStr + "】";
                            User sendUser = new User(RegisterUserThread.username, null, Msg, MsgTypeConfig.SHANGXIAN, System.currentTimeMillis());
                            sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
                            sendStream.writeObject(sendUser);
                        }

                    }
                } else if (receiver.getMsgType() == MsgTypeConfig.ONLINE_LIST) {
                    String ss = receiver.getUserSend();
                    String xi = "";
                    int i = 1;
                    for (String s : Server.info) {
                        if (s.equals(":      " + ss + "   " + hm.get(ss).getInetAddress() + "      " + hm.get(ss).getPort() + "         " +
                                hm.get(ss).getLocalPort() + "       在线】")) {
                            xi = xi + +i + s + "【我】" + "\n";

                        }
                        if (!(s.equals(":      " + ss + "   " + hm.get(ss).getInetAddress() + "      " + hm.get(ss).getPort() + "         " +
                                hm.get(ss).getLocalPort() + "       在线】"))) {
                            xi = xi + i + s + "\n";
                        }
                        i++;
                    }
                    User sendUser = new User(receiver.getUsername(), xi, receiver.getMsgType(), receiver.getTime());
                    ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
                    sendStream.writeObject(sendUser);
                } else if (receiver.getMsgType() == MsgTypeConfig.ONLINE_LIST1) {
                    String ss = receiver.getUserSend();
                    String xi = "";
                    int i = 1;
                    for (String s : Server.info) {
                        if (s.equals(":      " + ss + "   " + hm.get(ss).getInetAddress() + "      " + hm.get(ss).getPort() + "         " +
                                hm.get(ss).getLocalPort() + "       在线】")) {
                            xi = xi + +i + s + "【我】" + "\n";
                        }
                        if (!(s.equals(":      " + ss + "   " + hm.get(ss).getInetAddress() + "      " + hm.get(ss).getPort() + "         " +
                                hm.get(ss).getLocalPort() + "       在线】"))) {
                            xi = xi + i + s + "\n";
                        }
                        i++;
                    }
                    User sendUser = new User(receiver.getUsername(), xi, receiver.getMsgType(), receiver.getTime());
                    ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
                    sendStream.writeObject(sendUser);
                } else if (receiver.getMsgType() == MsgTypeConfig.ONLINE_MSG) {
                    String ss = receiver.getUserSend();
                    String mu = ":      " + ss + "   " + hm.get(ss).getInetAddress() + "      " + hm.get(ss).getPort() + "         " +
                            hm.get(ss).getLocalPort() + "       在线】";
                    if (Server.info.contains(mu)) {
                        Server.info.remove(mu);
                        User sendUser = new User(receiver.getUserSend(), "您已成功隐身!", receiver.getMsgType(), receiver.getTime());
                        ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
                        sendStream.writeObject(sendUser);
                    } else {
                        Server.info.add(mu);
                        User sendUser = new User(receiver.getUserSend(), "您已成功上线!", receiver.getMsgType(), receiver.getTime());
                        ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
                        sendStream.writeObject(sendUser);
                    }

                } else if (receiver.getMsgType() == MsgTypeConfig.WENJIAN) {
                    ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUsername()).getOutputStream());
                    sendStream.writeObject(receiver);
                } else if (receiver.getMsgType() == MsgTypeConfig.FANKUI) {
                    if (receiver.getContent().equals("yes")) {
                        ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
                        sendStream.writeObject(receiver);
                    } else {
                        ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(receiver.getUserSend()).getOutputStream());
                        sendStream.writeObject(receiver);
                    }

                } else if (receiver.getMsgType() == MsgTypeConfig.EXIT_MSG) {
                    //下线提醒
                    Set<String> keySet = hm.keySet();
                    for (String key : keySet) {
                        if (key.equals(receiver.getUsername())) {
                            continue;
                        }

                        String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(receiver.getTime());
                        String Msg = "【下线信息】:【用户:" + RegisterUserThread.username + ":" + "下线了】【" + timeStr + "】";
                        //转发格式:发送者:内容:消息类型:时间
                        User sendUser = new User(null, Msg, receiver.getMsgType(), receiver.getTime());
                        ObjectOutputStream sendStream = new ObjectOutputStream(hm.get(key).getOutputStream());
                        sendStream.writeObject(sendUser);
                        String ss = receiver.getUsername();
                        Server.info.remove(":      " + ss + "   " + hm.get(ss).getInetAddress() + "      " + hm.get(ss).getPort() + "         " +
                                hm.get(ss).getLocalPort() + "       在线】");
                    }
                    break;
                }
            }
            //关闭这个人的管道
            hm.get(receiver.getUsername()).close();
            //移除这个用户
            hm.remove(receiver.getUsername());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package SanWa.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileUtils {
    public static byte[] readFile(String path) {
        File file = new File(path);
        byte datas[] = null;
        if (!file.exists()) {
            datas = null;
        } else {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                FileInputStream fis = new FileInputStream(file);
                byte data[] = new byte[1024 * 1024];
                int len = 0;
                while ((len = fis.read(data)) > 0) {
                    baos.write(data, 0, len);
                }
                datas = baos.toByteArray();
                baos.flush();
                baos.close();
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return datas;
    }

    public static boolean writeFile(String path, byte datas[]) {
        try {
            File file = new File(path);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(datas);
            fos.flush();
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

package SanWa.Util;

public class PathUtils {
    //获取路径
    private static final String IMG_PATH="ChatRooms\\Image\\";
    public static String getRealPath(String relativePath) {
        return IMG_PATH + relativePath;
    }
}

package SanWa.Util;

import java.awt.*;

public class ScreenUtils {
    //获取电脑屏幕的宽度
    public static int getScreenWidth() {
        return Toolkit.getDefaultToolkit().getScreenSize().width;
    }

    //获取电脑屏幕的高度
    public static int getScreenHeight() {
        return Toolkit.getDefaultToolkit().getScreenSize().height;
    }
}

Java聊天室程序 需求分析 2.1 业务需求 1. 与聊天室成员一起聊天。 2. 可以与聊天室成员私聊。 3. 可以改变聊天内容风格。 4. 用户注册(含头像)、登录。 5. 服务器监控聊天内容。 6. 服务器过滤非法内容。 7. 服务器发送通知。 8. 服务器踢人。 9. 保存服务器日志。 10.保存用户聊天信息。 2.2 系统功能模块 2.2.1 服务器端 1.处理用户注册 2.处理用户登录 3.处理用户发送信息 4.处理用户得到信息 5.处理用户退出 2.2.2 客户端 1.用户注册界面及结果 2.用户登录界面及结果 3.用户发送信息界面及结果 4.用户得到信息界面及结果 5.用户退出界面及结果 2.3 性能需求 运行环境:Windows 9x、2000、xp、2003,Linux 必要环境:JDK 1.5 以上 硬件环境:CPU 400MHz以上,内存64MB以上 3.1.2 客户端结构 ChatClient.java 为客户端程序启动类,负责客户端的启动和退出。 Login.java 为客户端程序登录界面,负责用户帐号信息的验证与反馈。 Register.java 为客户端程序注册界面,负责用户帐号信息的注册验证与反馈。 ChatRoom.java 为客户端程序聊天室主界面,负责接收、发送聊天内容与服务器端的Connection.java 亲密合作。 Windowclose 为ChatRoom.java的内部类,负责监听聊天室界面的操作,当用户退出时返回给服务器信息。 Clock.java 为客户端程序的一个小程序,实现的一个石英钟功能。 3. 2 系统实现原理 当用户聊天时,将当前用户名、聊天对象、聊天内容、聊天语气和是否私聊进行封装,然后与服务器建立Socket连接,再用对象输出包装Socket的输出将聊天信息对象发送给服务器端 当用户发送聊天信息时,服务端将会收到客户端用Socket传输过来的聊天信息对象,然后将其强制转换为Chat对象,并将本次用户的聊天信息对象添加到聊天对象集Message中,以供所有聊天用户访问。 接收用户的聊天信息是由多线程技术实现的,因为客户端必须时时关注更新服务器上是否有最新消息,在本程序中设定的是3秒刷新服务器一次,如果间隔时间太短将会增加客户端与服务器端的通信负担,而间隔时间长就会让人感觉没有时效性,所以经过权衡后认为3秒最佳,因为每个用户都不可能在3秒内连续发送信息。 当每次用户接收到聊天信息后将会开始分析聊天信息然后将适合自己的信息人性化地显示在聊天信息界面上。 4.1.1 问题陈述 1.接受用户注册信息并保存在一个基于文件的对象型数据库。 2.能够允许注册过的用户登陆聊天界面并可以聊天。 3.能够接受私聊信息并发送给特定的用户。 4.服务器运行在自定义的端口上#1001。 5.服务器监控用户列表和用户聊天信息(私聊除外)。 6.服务器踢人,发送通知。 7.服务器保存日志。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值