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

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

界面及解释

注册主页面 五个输入框 五个校验按钮 检验输入框的不同格式 注册账号按钮 重置输入框信息按钮 登录按钮 退出按钮 关于我们按钮


用户名校验按钮 正确与错误与用户名已存在

密码格式正确与错误

密码的确认与失败

IP地址的正确与错误检验

端口号的正确与错误检验

注册成功与用户名已存在错误

重置按钮效果

关于我们

登录跳转到登录界面

代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值