Java--用户登录/注册界面(连接Mysql数据库)并可以通过验证码登录

如果有小伙伴们想看Python实现用户登录功能,可以点击👉👉👉Python–实现用户登录功能(GUI图形界面)

观看B站视频

1 效果展示

(1)登录界面
在这里插入图片描述
(2)注册界面
在这里插入图片描述
(3)动图展示
在这里插入图片描述

2 内容说明

(1)开发前,需引入一个连接Mysql 数据库驱动mysql-connector-java-5.1.30-bin.jar包
提取码:6666
(2)构建路径
在这里插入图片描述
(3)需要下载xampp软件
xampp软件下载
提取码:2255
xampp软件包含Apache Web服务器、Mysql Web服务器、Filezilla Web服务器、Mercury服务器以及Tomcat Web服务器。在本项目中,我们只需要开启Apache服务器和Mysql服务器即可。
在这里插入图片描述

3 主要代码

(1)GUI登录界面设计的代码如下:

package com.weh.User;

import com.weh.ClientDemo.MousePressedSet;
import com.weh.Mail.SendMail;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicPanelUI;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
*   登录窗口
*/
public class LoginDemo extends JFrame{
    JTextField uField;
    JPasswordField pFd;
    JTextField QQField ;
    JTextField tField;
    static int random;
    static boolean flag=false;
    Login login=new Login();
    public LoginDemo() {
        super("HI登录界面");
        //获取显示屏的大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int sw = screenSize.width;
        int sh = screenSize.height;
        //窗口宽高大小
        int width = 500;
        int height = 340;
        this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);//设置窗口的位置
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(LoginDemo.class.getResource("/com/weh/img/chatping.jpg")));//窗口图标
        Image image = Toolkit.getDefaultToolkit().getImage(LoginDemo.class.getResource("/com/weh/img/chat.jpg"));//背景图
        ImageIcon background = new ImageIcon(image);
        background.setImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        JLabel label = new JLabel(background); // 把背景图片显示在一个标签里面
        label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
        JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
        imagePanel.setOpaque(false); // 窗口透明
        this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE)); // 把背景图片添加到分层窗格的最底层作为背景

        JPanel panel = new JPanel();
        //盒子模块
        Box ubox = Box.createHorizontalBox();
        Box pbox = Box.createHorizontalBox();
        Box vbox = Box.createVerticalBox();

        //创建界面工具类
        JLabel uLabel = new JLabel("用户名:"); //文本设计
        uLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
        uLabel.setForeground(new Color(0xFBFCFD)); // 设置前景色
        uField = new JTextField(); //输入框
        uField.setToolTipText("HI");// 悬停显示
        uField.setFont(new Font("微软雅黑", Font.BOLD, 15));
        uField.setColumns(12);

        JLabel pLabel = new JLabel("密   码:");    //文本设计
        pLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
        pLabel.setForeground(new Color(0xFBFCFD)); // 设置前景色
        pFd = new JPasswordField();
        pFd.setToolTipText("密码");// 悬停显示
        pFd.setFont(new Font("微软雅黑", Font.BOLD, 15));
        pFd.setColumns(12);
        // 如果使用其他回显字符,可以设置大小,但是星星不可以。。。
        pFd.setEchoChar('●');// 星星符号

        JButton button1 = new JButton("登录");    //登录按钮
        button1.setToolTipText("登录");// 悬停显示
        JButton button2 = new JButton("重置");    //重置按钮
        button2.setToolTipText("重置");// 悬停显示
        JButton button3 = new JButton("发送验证码");    //发送验证码按钮
        button3.setToolTipText("发送验证码");// 悬停显示
        JMenu Menubutton3 = new JMenu("注册账号");    //注册账号按钮
        Menubutton3.setToolTipText("注册账号");// 悬停显示
        JMenu Menubutton4 = new JMenu("已有账号,忘记密码?");    //已有账号,忘记密码?按钮
        Menubutton4.setToolTipText("已有账号,忘记密码?");// 悬停显示
        JMenu Menubutton5 = new JMenu("邮箱验证登录");    //邮箱验证登录按钮
        Menubutton5.setToolTipText("邮箱验证登录");// 悬停显示
        JMenu Menubutton6 = new JMenu("密码登录");    //密码登录按钮
        Menubutton6.setToolTipText("密码登录");// 悬停显示
        // 字体设置
        button1.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button1.setForeground(Color.white); // 设置前景色
        button1.setBackground(new Color(0x08BDFD));
        button1.setDefaultCapable(true);
        button1.setBounds((this.getWidth() - 120 - 180) / 2, 250, 120, 30); // 设置按钮位置,及按钮大小
        button1.setCursor(new Cursor(Cursor.HAND_CURSOR));  //鼠标手势的设置

        button2.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button2.setForeground(Color.white); // 设置前景色
        button2.setBackground(new Color(0x08BDFD));
        button2.setDefaultCapable(true);
        button2.setCursor(new Cursor(Cursor.HAND_CURSOR));//鼠标手势的设置
        button2.setBounds((this.getWidth() - 120 + 180) / 2, 250, 120, 30); // 设置按钮位置,及按钮大小

        button3.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button3.setForeground(new Color(1)); // 设置前景色
        button3.setBackground(new Color(0x08BDFD));
        button3.setDefaultCapable(true);

        button3.setCursor(new Cursor(Cursor.HAND_CURSOR));//鼠标手势的设置

        Menubutton3.setFont(new Font("微软雅黑", Font.BOLD, 12));
        Menubutton3.setForeground(new Color(0x02FCFC)); // 设置前景色
        Menubutton3.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton3.setBounds(5, 280, 85, 20); // 设置按钮位置,及按钮大小
        Menubutton3.setContentAreaFilled(false); // 设置按钮透明
        Menubutton3.setCursor(new Cursor(Cursor.HAND_CURSOR));

        Menubutton4.setFont(new Font("微软雅黑", Font.BOLD, 12));
        Menubutton4.setForeground(new Color(0xC3FFFF)); // 设置前景色
        Menubutton4.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton4.setBounds(width-150, 280, 150, 20); // 设置按钮位置,及按钮大小
        Menubutton4.setContentAreaFilled(false); // 设置按钮透明
        Menubutton4.setCursor(new Cursor(Cursor.HAND_CURSOR));

        Menubutton5.setFont(new Font("微软雅黑", Font.BOLD, 15));
        Menubutton5.setForeground(new Color(0x383939)); // 设置前景色
        Menubutton5.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton5.setBounds(width-190, 225, 100, 20); // 设置按钮位置,及按钮大小
        Menubutton5.setContentAreaFilled(false); // 设置按钮透明
        Menubutton5.setCursor(new Cursor(Cursor.HAND_CURSOR));

        Menubutton6.setFont(new Font("微软雅黑", Font.BOLD, 15));
        Menubutton6.setForeground(new Color(0x383939)); // 设置前景色
        Menubutton6.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton6.setBounds(width-190, 225, 100, 20); // 设置按钮位置,及按钮大小
        Menubutton6.setContentAreaFilled(false); // 设置按钮透明
        Menubutton6.setCursor(new Cursor(Cursor.HAND_CURSOR));

        /*
         分区模块布局
        */
        //小盒子,设计用户名布局模块
        ubox.add(uLabel);
        ubox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5,作为相邻文本的空隙
        ubox.add(uField);
        //小盒子,设计密码框布局模块
        pbox.add(pLabel);
        pbox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5,作为相邻文本的空隙
        pbox.add(pFd);

        //大盒子
        vbox.add(Box.createVerticalStrut(80));//插入中间盒子高度为80,作为上下文本的空隙
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(60));//插入中间盒子高度为60,作为上下文本的空隙
        vbox.add(pbox);


        uField.setText("weh");   //设置默认账号
        pFd.setText("123456");  //设置默认密码
        panel.setUI(new BasicPanelUI());  //恢复基本视觉效果
        panel.setOpaque(false); // 面板透明
        panel.add(vbox, BorderLayout.CENTER);//vbox盒子居中
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(button1);
        this.add(button2);
        this.add(Menubutton3);
        this.add(Menubutton4);
        this.add(Menubutton5);
        this.add(Menubutton6);
        this.add(panel);
        Menubutton6.setVisible(false);
        this.setVisible(true);
        this.setResizable(false);
        this.setAlwaysOnTop(true);//设置窗口最上层

        QQField = new JTextField(); //输入框
        tField = new JTextField(); //输入框
        QQField.setVisible(false);
        tField.setVisible(false);
        button3.setVisible(false);

        Menubutton4.addActionListener(e -> {
            this.dispose();
            try {
                Thread.sleep(500);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
            SetPassword.rePassword= new RePassword();
        });

        Menubutton5.addActionListener(e -> {//邮箱验证按钮监听事件
            uLabel.setText("电子邮箱:");
            pLabel.setText("  验证码:");
            uField.setVisible(false);
            Menubutton6.setVisible(true);
            Menubutton5.setVisible(false);
            button3.setVisible(true);
            pFd.setVisible(false);
            QQField.setVisible(true);
            tField.setVisible(true);
            QQField.setToolTipText("电子邮箱");// 悬停显示
            QQField.setFont(new Font("黑体", Font.BOLD, 18));
            QQField.setColumns(12);
            tField.setToolTipText("验证码");// 悬停显示
            tField.setFont(new Font("黑体", Font.BOLD, 18));
            tField.setColumns(12);
            ubox.add(QQField);
            pbox.add(tField);
            pbox.add(button3);
            LoginDemo.flag=true;
        });

        Menubutton6.addActionListener(e -> {//用户名登录按钮监听
            Menubutton6.setVisible(false);
            Menubutton5.setVisible(true);
            QQField.setVisible(false);
            tField.setVisible(false);
            uField.setVisible(true);
            button3.setVisible(false);
            uLabel.setText("用户名:");
            pFd.setVisible(true);
            pLabel.setText("密   码:");
            LoginDemo.flag=false;
        });
        button3.addActionListener(e -> {
            String QF =QQField.getText().trim();
            String Qregex = "^\\d{6,10}@qq.com$";
            Pattern pt =Pattern.compile(Qregex);
            Matcher mc = pt.matcher(QF);
            if(mc.matches()){
                button3.setEnabled(false);
                random=(int) (Math.random()*999999+100000);//随机产生6位数的随机数作为验证码
                System.out.println(random);
                String str="HI登录验证/您的验证码为:\n"+random+",该验证码1分钟内有效,请勿泄露于他人。";
                new Thread(new SendMail(QQField.getText(),str)).start();
                new Thread(new TimeDown(button3,QQField)).start();
            }else{
                if(QQField.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "电子邮件不能为空!", "警告", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                JOptionPane.showMessageDialog(null, "电子邮件输入有误,请正确输入!", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });
        uField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
                if (uField.getText().equals("请输入您的用户名")) {
                    uField.setText("");     //将提示文字清空
                    uField.setFont(new Font("微软雅黑", Font.BOLD, 15));
                    uField.setForeground(new Color(1));  //设置用户输入的字体颜色为黑色
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
                if (uField.getText().equals("")) {
                    uField.setForeground(new Color(0xC7C5C5)); //将提示文字设置为灰色
                    uField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                    uField.setText("请输入您的用户名");     //显示提示文字
                }
            }
        });

        pFd.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
                if (pFd.getText().equals("请输入密码")) {
                    pFd.setText("");     //将提示文字清空
                    pFd.setFont(new Font("微软雅黑", Font.BOLD, 15));
                    pFd.setForeground(new Color(222, 221, 221));  //设置用户输入的字体颜色为黑色
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
                if (pFd.getText().equals("")) {
                    pFd.setForeground(Color.gray); //将提示文字设置为灰色
                    pFd.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                    pFd.setText("请输入密码");     //显示提示文字
                }
            }
        });

            //点击按钮的监听事件
        button1.addActionListener(e -> {    //登录按钮监听事件
            try {
                init(this);
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });

        button2.addActionListener(e -> {//重置按钮监听事件
            uField.setText("");
            pFd.setText("");
            QQField.setText("");
            tField.setText("");
        });
        Menubutton3.addActionListener(e -> {//跳转到注册窗口按钮监听事件
            try {
                this.dispose();
                Thread.sleep(1000);
                new RegisterDemo();
            } catch (InterruptedException interruptedException) {
                JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });
    }

    public void init(LoginDemo loginDemo) {
        UserLogin UserLogin;
        try {
            login.setLoginDemo(loginDemo);
            if(LoginDemo.flag){
                login.setQQmail(QQField.getText());
                login.settField(tField.getText());
            }else{
                login.setName(uField.getText());
                char[] p = pFd.getPassword();
                login.setPassword(new String(p));
            }
            UserLogin = new UserLogin();
            UserLogin.readLogin(login);
        } catch (SQLException | ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
        }

    }
}

class TimeDown implements Runnable{
    JButton button;
    JTextField textField;
    public TimeDown(JButton button,JTextField textField){
        this.button=button;
        this.textField=textField;
    }
    @Override
    public void run() {
        try {
            int setTime=60;
            while (true){
                    button.setText("       "+setTime+"       ");
                    Thread.sleep(1000);
                    setTime--;
                if (setTime==0){
                    button.setText("发送验证码");
                    LoginDemo.random=(int) (Math.random()*9999999+1000000);
                    button.setEnabled(true);
                    break;
                }
            }
        } catch (InterruptedException e) {
            JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
        }
    }
}

class RePassword extends JFrame{
    static int random;
    Login login = new Login();
    static JTextField mailField = new JTextField();
    static JTextField tFd = new JTextField();
    public RePassword(){
        //获取显示屏的大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int sw = screenSize.width;
        int sh = screenSize.height;
        //设置窗口的位置
        int width = 420;
        int height = 250;
        this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);
        this.setUndecorated(true);//不要边框
        //      盒子模块
        Box ubox = Box.createHorizontalBox();
        Box pbox = Box.createHorizontalBox();
        Box vbox = Box.createVerticalBox();
        JLabel label = new JLabel("");
        label.setForeground(new Color(0x3E5C5C));
        label.setFont(new Font("黑体", Font.BOLD, 13));
        JPanel headpanel = new JPanel();
        headpanel.add(label);
        headpanel.setSize(width,30);
        headpanel.setBackground(new Color(0x08BDFD));

        //      创建界面工具类
        JLabel mailLabel = new JLabel("邮箱:"); //文本设计
        mailLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        mailLabel.setForeground(new Color(0x3E5C5C)); // 设置前景色
        mailField.setFont(new Font("Arial", Font.BOLD, 13));
        mailField.setToolTipText("邮箱");// 悬停显示
        mailField.setColumns(12);

        JLabel tLabel = new JLabel("验证码:");    //文本设计
        tLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        tLabel.setForeground(new Color(0x3E5C5C)); // 设置前景色
        tFd.setFont(new Font("Arial", Font.BOLD, 13));
        tFd.setToolTipText("验证码");// 悬停显示
        tFd.setColumns(12);

        JButton button1 = new JButton("确定");    //登录按钮
        button1.setToolTipText("确定");// 悬停显示

        JMenu Menubutton = new JMenu("返回登录");    //重置按钮
        Menubutton.setToolTipText("返回登录");// 悬停显示
        //      字体设置
        button1.setFont(new Font("黑体", Font.BOLD, 18));
        button1.setForeground(Color.white); // 设置前景色
        button1.setBackground(new Color(0x08BDFD));
        button1.setDefaultCapable(true);
        button1.setBounds((this.getWidth() - 200) / 2, 190, 200, 30); // 设置按钮位置,及按钮大小
        button1.setCursor(new Cursor(Cursor.HAND_CURSOR));  //鼠标手势的设置

        JButton button2 = new JButton("发送验证码");    //发送验证码按钮
        button2.setToolTipText("发送验证码");// 悬停显示
        button2.setFont(new Font("黑体", Font.BOLD, 14));
        button2.setForeground(Color.gray); // 设置前景色
        button2.setBackground(new Color(0x08BDFD));
        button2.setDefaultCapable(true);
        button2.setCursor(new Cursor(Cursor.HAND_CURSOR));//鼠标手势的设置

        Menubutton.setFont(new Font("微软雅黑", Font.BOLD, 12));
        Menubutton.setForeground(new Color(0x324945)); // 设置前景色
        Menubutton.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton.setBounds(0,height-30, 85, 20); // 设置按钮位置,及按钮大小
        Menubutton.setContentAreaFilled(false); // 设置按钮透明
        Menubutton.setCursor(new Cursor(Cursor.HAND_CURSOR));


        //        小盒子,设计邮箱模块
        ubox.add(mailLabel);
        ubox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5,作为相邻文本的空隙
        ubox.add(mailField);
        //        小盒子,设计验证码模块
        pbox.add(tLabel);
        pbox.add(Box.createHorizontalStrut(5));
        pbox.add(tFd);
        pbox.add(button2);

        //      大盒子
        vbox.add(Box.createVerticalStrut(60));//插入中间盒子高度为60,作为上下文本的空隙
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(40));
        vbox.add(pbox);


        //转换
        JPanel panel = new JPanel();
        panel.setUI(new BasicPanelUI());  //恢复基本视觉效果
        panel.setBackground(new Color(0xFDFFFF));
        panel.add(vbox, BorderLayout.CENTER);//将大盒子添加到jLabel盒子里
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(headpanel);
        this.add(button1);
        this.add(Menubutton);
        this.add(panel);
        this.setVisible(true);
        this.setResizable(false);
        new MousePressedSet().mousePressedSet(this);//鼠标拖动窗口


        button1.addActionListener(e -> {
            label.setText("正在通过邮箱找回密码 ...");
            try {
                if(login.isTem()){
                   SetPassword.userMail= mailField.getText();
                    new UserLogin().readLogin(login);
                }
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
        });
        button2.addActionListener(e -> {
            String mail =mailField.getText().trim();
            String Qregex = "^\\d{6,10}@qq.com$";
            Pattern pt =Pattern.compile(Qregex);
            Matcher mc = pt.matcher(mail);
            if(mc.matches()){
                button2.setEnabled(false);
                login.setTem(true);
                random=(int) (Math.random()*999999+100000);//随机产生6位数的随机数作为验证码
                System.out.println(random);
                String str="找回密码验证/您的验证码为:\n"+random+",该验证码1分钟内有效,请勿泄露于他人。";
                new Thread(new SendMail(mailField.getText(),str)).start();
                new Thread(new TimeDown(button2,mailField)).start();
            }else{
                if(mailField.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "电子邮件不能为空!", "警告", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                JOptionPane.showMessageDialog(null, "电子邮件输入有误,请正确输入!", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });
        Menubutton.addActionListener(e -> {
            this.dispose();
            try {
                Thread.sleep(1000);
                new LoginDemo();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        });
    }
}

class SetPassword extends JFrame{
    static JTextField newField = new JTextField();
    static JPasswordField newFd = new JPasswordField();
    static RePassword rePassword = null;
    static String userMail=null;
    public SetPassword(){
        rePassword.dispose();
        //获取显示屏的大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int sw = screenSize.width;
        int sh = screenSize.height;
        //设置窗口的位置
        int width = 420;
        int height = 250;
        this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);
        this.setUndecorated(true);//不要边框
        //      盒子模块
        Box ubox = Box.createHorizontalBox();
        Box pbox = Box.createHorizontalBox();
        Box vbox = Box.createVerticalBox();
        JLabel label = new JLabel("重置密码");
        label.setForeground(new Color(0x3E5C5C));
        label.setFont(new Font("黑体", Font.BOLD, 13));
        JPanel headpanel = new JPanel();
        headpanel.add(label);
        headpanel.setSize(width,30);
        headpanel.setBackground(new Color(0x08BDFD));

        //      创建界面工具类
        JLabel newLabel = new JLabel("重置密码:"); //文本设计
        newLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        newLabel.setForeground(new Color(0x3E5C5C)); // 设置前景色
        newField.setFont(new Font("Arial", Font.BOLD, 13));
        newField.setToolTipText("重置密码");// 悬停显示
        newField.setColumns(12);

        JLabel tLabel = new JLabel("确认密码:");    //文本设计
        tLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        tLabel.setForeground(new Color(0x3E5C5C)); // 设置前景色
        newFd.setFont(new Font("Arial", Font.BOLD, 13));
        newFd.setToolTipText("确认密码");// 悬停显示
        newFd.setColumns(12);
        newFd.setEchoChar('●');// 星星符号

        JButton button1 = new JButton("确定");    //登录按钮
        button1.setToolTipText("确定");// 悬停显示

        JMenu Menubutton = new JMenu("返回登录");    //重置按钮
        Menubutton.setToolTipText("返回登录");// 悬停显示
        //      字体设置
        button1.setFont(new Font("黑体", Font.BOLD, 18));
        button1.setForeground(Color.white); // 设置前景色
        button1.setBackground(new Color(0x08BDFD));
        button1.setDefaultCapable(true);
        button1.setBounds((this.getWidth() - 200) / 2, 190, 200, 30); // 设置按钮位置,及按钮大小
        button1.setCursor(new Cursor(Cursor.HAND_CURSOR));  //鼠标手势的设置


        Menubutton.setFont(new Font("微软雅黑", Font.BOLD, 12));
        Menubutton.setForeground(new Color(0x324945)); // 设置前景色
        Menubutton.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton.setBounds(0,height-30, 85, 20); // 设置按钮位置,及按钮大小
        Menubutton.setContentAreaFilled(false); // 设置按钮透明
        Menubutton.setCursor(new Cursor(Cursor.HAND_CURSOR));


        //        小盒子,设计邮箱模块
        ubox.add(newLabel);
        ubox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5,作为相邻文本的空隙
        ubox.add(newField);
        //        小盒子,设计验证码模块
        pbox.add(tLabel);
        pbox.add(Box.createHorizontalStrut(5));
        pbox.add(newFd);


        //      大盒子
        vbox.add(Box.createVerticalStrut(60));//插入中间盒子高度为60,作为上下文本的空隙
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(40));
        vbox.add(pbox);


        //转换
        JPanel panel = new JPanel();
        panel.setUI(new BasicPanelUI());  //恢复基本视觉效果
        panel.setBackground(new Color(0xFDFFFF));
        panel.add(vbox, BorderLayout.CENTER);//将大盒子添加到jLabel盒子里
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(headpanel);
        this.add(button1);
        this.add(Menubutton);
        this.add(panel);
        this.setVisible(true);
        this.setResizable(false);
        new MousePressedSet().mousePressedSet(this);//鼠标拖动窗口

        button1.addActionListener(e -> {
            label.setText("正在上传中...");
            PreparedStatement prepare;
            Connection connection;
            String sql;
            if(newField.getText().equals(newFd.getText()) && !newField.getText().isEmpty()){
                try {
                    connection=new JDBCMysql().connection;
                    sql="UPDATE test.User SET UserPassword = ? WHERE UserMail=?";
                    prepare = connection.prepareStatement(sql);//创建执行环境
                    prepare.setString(1, newField.getText());//重置后的密码
                    prepare.setString(2, userMail);//邮箱
                    int result = prepare.executeUpdate();
                    if(result==1){
                        JOptionPane.showMessageDialog(null, "修改成功!","提示", JOptionPane.WARNING_MESSAGE);
                        this.dispose();
                        new LoginDemo();
                    }else{
                        JOptionPane.showMessageDialog(null, "修改失败!","提示", JOptionPane.WARNING_MESSAGE);
                    }
                    connection.close();
                    prepare.close();
                } catch (Exception event) {
                    JOptionPane.showMessageDialog(null, "数据库连接失败!","提示", JOptionPane.WARNING_MESSAGE);
                }
            }else{
                if(newField.getText().isEmpty()){
                    JOptionPane.showMessageDialog(null, "密码不能为空!","提示", JOptionPane.WARNING_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null, "您输入的两次密码不一致,请重新输入!","提示", JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        Menubutton.addActionListener(e -> {
            this.dispose();
            try {
                Thread.sleep(1000);
                new LoginDemo();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        });
    }
}

(2)GUI注册界面设计的代码如下:

package com.weh.User;

import com.weh.ClientDemo.ClientFrame;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicPanelUI;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 *   注册窗口
 */
public class RegisterDemo extends JFrame {
    public RegisterDemo() {
        //获取显示屏的大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int sw = screenSize.width;
        int sh = screenSize.height;
        this.setTitle("HI注册界面");
        int width = 500;
        int height = 600;
        this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);// 设置窗口的位置
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(RegisterDemo.class.getResource("/com/weh/img/chatping.jpg")));// 图标
        Image image = Toolkit.getDefaultToolkit().getImage(RegisterDemo.class.getResource("/com/weh/img/pto.jpg"));//背景图
        ImageIcon background = new ImageIcon(image); // 背景图片
        background.setImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        JLabel label = new JLabel(background); // 把背景图片显示在一个标签里面
        label.setBounds(0, 0, this.getWidth(),  this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
        JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
        imagePanel.setOpaque(false); // 窗口透明
        this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE)); // 把背景图片添加到分层窗格的最底层作为背景

        //盒子模块
        Box namebox = Box.createHorizontalBox();
        Box ubox = Box.createHorizontalBox();
        Box pbox = Box.createHorizontalBox();
        Box repbox = Box.createHorizontalBox();
        Box vbox = Box.createVerticalBox();

        JLabel nameLabel = new JLabel("用 户 名 :"); //文本设计
        nameLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        nameLabel.setForeground(new Color(0xFF03FC6E, true)); // 设置前景色
        JTextField nameField = new JTextField(); //输入框
        nameField.setFont(new Font("黑体", Font.BOLD, 15));
        nameField.setToolTipText("用户名");
        nameField.setColumns(15);


        JLabel uLabel = new JLabel("电子邮件:"); //文本设计
        uLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        uLabel.setForeground(new Color(0xFF03FC6E, true)); // 设置前景色
        JTextField uField = new JTextField("2609931863@qq.com"); //输入框
        uField.setFont(new Font("Arial", Font.BOLD, 15));
        uField.setToolTipText("电子邮件");
        uField.setColumns(15);

        JLabel pLabel = new JLabel(" 密    码 :");    //文本设计
        pLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        pLabel.setForeground(new Color(0xFF03FC6E, true)); // 设置前景色
        JPasswordField pFd = new JPasswordField();
        pFd.setFont(new Font("Arial", Font.BOLD, 15));
        pFd.setToolTipText("密码");// 悬停显示
        pFd.setColumns(15);
        // 如果使用其他回显字符,可以设置大小,但是星星不可以。。。
        pFd.setEchoChar('●');// 星星符号


        JLabel RepLabel = new JLabel("确认密码:");    //文本设计
        RepLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        RepLabel.setForeground(new Color(0xFF03FC6E, true)); // 设置前景色
        JPasswordField RepFd = new JPasswordField();
        RepFd.setFont(new Font("Arial", Font.BOLD, 15));
        RepFd.setToolTipText("确认密码");// 悬停显示
        RepFd.setColumns(15);
        // 如果使用其他回显字符,可以设置大小,但是星星不可以。。。
        RepFd.setEchoChar('●');// 星星符号

        JButton button1 = new JButton("提交");    //登录按钮
        button1.setToolTipText("注册");// 悬停显示
        JButton button2 = new JButton("重置");    //重置按钮
        button2.setToolTipText("重置");// 悬停显示
        JMenu Menubutton3 = new JMenu("返回登录");    //重置按钮
        Menubutton3.setToolTipText("返回登录");// 悬停显示

        button1.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button1.setForeground(Color.white); // 设置前景色
        button1.setBackground(new Color(0x08BDFD));
        button1.setDefaultCapable(true);
        button1.setBounds((this.getWidth() - 120 - 180) / 2, this.getHeight() - 150, 120, 30); // 设置按钮位置,及按钮大小
        button1.setCursor(new Cursor(Cursor.HAND_CURSOR));  //鼠标手势的设置

        button2.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button2.setForeground(Color.white); // 设置前景色
        button2.setBackground(new Color(0x08BDFD));
        button2.setDefaultCapable(true);
        button2.setCursor(new Cursor(Cursor.HAND_CURSOR));//鼠标手势的设置
        button2.setBounds((this.getWidth() - 120 + 180) / 2, this.getHeight() - 150, 120, 30); // 设置按钮位置,及按钮大小

        Menubutton3.setFont(new Font("微软雅黑", Font.BOLD, 12));
        Menubutton3.setForeground(Color.white); // 设置前景色
        Menubutton3.setBackground(new Color(0x08BDFD));
        Menubutton3.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton3.setBounds(5, this.getHeight() - 70, 85, 20); // 设置按钮位置,及按钮大小
        Menubutton3.setContentAreaFilled(false); // 设置按钮透明
        Menubutton3.setCursor(new Cursor(Cursor.HAND_CURSOR));

        /*
         分区模块布局
        */

        //小盒子,设计用户名模块
        namebox.add(nameLabel);
        namebox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5,作为相邻文本的空隙
        namebox.add(nameField);

        ubox.add(uLabel);
        ubox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5,作为相邻文本的空隙
        ubox.add(uField);
        //小盒子,设计密码模块
        pbox.add(pLabel);
        pbox.add(Box.createHorizontalStrut(5));
        pbox.add(pFd);

        repbox.add(RepLabel);
        repbox.add(Box.createHorizontalStrut(5));
        repbox.add(RepFd);

        vbox.add(Box.createVerticalStrut(90));//插入中间盒子高度为90,作为上下文本的空隙
        vbox.add(namebox);
        vbox.add(Box.createVerticalStrut(65));
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(65));
        vbox.add(pbox);
        vbox.add(Box.createVerticalStrut(65));
        vbox.add(repbox);


        JPanel panel = new JPanel();
        panel.setUI(new BasicPanelUI());  //恢复基本视觉效果
        panel.setOpaque(false); // 面板透明
        panel.add(vbox, BorderLayout.CENTER);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(button1);
        this.add(button2);
        this.add(Menubutton3);
        this.add(panel);
        this.setVisible(true);
        this.setResizable(false);
        this.setAlwaysOnTop(true);//设置窗口最上层

        nameField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
                if (nameField.getText().equals("填写用户名")) {
                    nameField.setText("");     //将提示文字清空
                    nameField.setFont(new Font("微软雅黑", Font.BOLD, 15));
                    nameField.setForeground(new Color(1));  //设置用户输入的字体颜色为黑色
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
                if (nameField.getText().equals("")) {
                    nameField.setForeground(new Color(0xC7C5C5)); //将提示文字设置为灰色
                    nameField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                    nameField.setText("填写用户名");     //显示提示文字
                }
            }
        });


        uField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
                if (uField.getText().equals("填写QQ邮箱,例如,***@qq.com")) {
                    uField.setText("");     //将提示文字清空
                    uField.setFont(new Font("微软雅黑", Font.BOLD, 15));
                    uField.setForeground(new Color(1));  //设置用户输入的字体颜色为黑色
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
                if (uField.getText().equals("")) {
                    uField.setForeground(new Color(0xC7C5C5)); //将提示文字设置为灰色
                    uField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                    uField.setText("填写QQ邮箱,例如,***@qq.com");     //显示提示文字
                }
            }
        });

        pFd.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
                if ( pFd.getText().equals("填写密码")) {
                    pFd.setText("");     //将提示文字清空
                    pFd.setFont(new Font("微软雅黑", Font.BOLD, 15));
                    pFd.setForeground(new Color(1));  //设置用户输入的字体颜色为黑色
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
                if (pFd.getText().equals("")) {
                    pFd.setForeground(new Color(0xC7C5C5)); //将提示文字设置为灰色
                    pFd.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                    pFd.setText("填写密码");     //显示提示文字
                }
            }
        });

        RepFd.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容
                if ( RepFd.getText().equals("填写密码")) {
                    RepFd.setText("");     //将提示文字清空
                    RepFd.setFont(new Font("微软雅黑", Font.BOLD, 15));
                    RepFd.setForeground(new Color(1));  //设置用户输入的字体颜色为黑色
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字
                if (RepFd.getText().equals("")) {
                    RepFd.setForeground(new Color(0xC7C5C5)); //将提示文字设置为灰色
                    RepFd.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                    RepFd.setText("填写密码");     //显示提示文字
                }
            }
        });
        //提交按钮监听
        button1.addActionListener(e -> {
            String pField = new String(pFd.getPassword());//转换
            String repField = new String(RepFd.getPassword());//转换
            try {
                if (nameField.getText().isEmpty() || pField.isEmpty() || repField.isEmpty()||uField.getText().isEmpty()) {
                    if (nameField.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(null, "用户名不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    } else if(uField.getText().isEmpty()){
                        JOptionPane.showMessageDialog(null, "电子邮件不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    }
                    else if (pField.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "密码不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "确认密码不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    }
                } else {
                    if (!pField.equals(repField)) {
                        JOptionPane.showMessageDialog(null, "两次密码不一致", "提示", JOptionPane.WARNING_MESSAGE);
                    } else {
                        String HI =uField.getText().trim();
                        String regex = "^\\d{6,10}@qq.com$";
                        Pattern p =Pattern.compile(regex);
                        Matcher m = p.matcher(HI);
                        if(m.matches()){
                            init(nameField,uField, pFd);
                        }else{
                            JOptionPane.showMessageDialog(null, "电子邮件输入有误,请正确输入!", "警告", JOptionPane.WARNING_MESSAGE);
                        }
                    }
                }
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });

        //重置按钮监听
        button2.addActionListener(e -> {
            nameField.setText(null);
            uField.setText(null);
            pFd.setText(null);
            RepFd.setText(null);
        });

        //返回登录窗口按钮监听
        Menubutton3.addActionListener(e -> {
            try {
                this.dispose();
                Thread.sleep(1000);
                new LoginDemo();
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });
    }

    public void init(JTextField nameField,JTextField uField, JPasswordField pFd) {
        Register register;
        UserRegister userRegister;
        try {
            register = new Register();
            register.setName(nameField.getText());
            register.setMile(uField.getText());
            char[] p1 = pFd.getPassword();
            register.setPassword(new String(p1));
            userRegister = new UserRegister();
            userRegister.writeRegister(register);
            uField.setText(null);
           nameField.setText(null);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
        }
    }
}

(3)连接Mysql数据库的代码如下:

package com.weh.User;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.swing.*;
import java.sql.Connection;

public class JDBCMysql {
    Connection connection;
    public JDBCMysql() {
        try {
            MysqlDataSource mysqlDataSource = new MysqlDataSource();
            mysqlDataSource.setURL("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false");//本地IP:端口/数据库名
            mysqlDataSource.setUser("root");//mysql数据库的用户名
            mysqlDataSource.setPassword("123456");//密码
            connection = mysqlDataSource.getConnection();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "数据库连接失败", "提示", JOptionPane.WARNING_MESSAGE);
        }
    }
}

(4)登录所需要的数据转入

package com.weh.User;

//登录所需要的数据转入
public class Login {
    String name;
    String password;
    LoginDemo loginDemo;
    String mail;
    String tField;
    boolean tem;

    public boolean isTem() {
        return tem;
    }

    public void setTem(boolean tem) {
        this.tem = tem;
    }

    public String gettField() {
        return tField;
    }

    public void settField(String tField) {
        this.tField = tField;
    }

    boolean loginSuccess = false;

    public String getQQmail() {
        return mail;
    }

    public void setQQmail(String mail) {
        this.mail = mail;
    }

    public LoginDemo getLoginDemo() {
        return loginDemo;
    }
    public void setLoginDemo(LoginDemo loginDemo) {
        this.loginDemo = loginDemo;
    }

    public String getName() {
        return name;
    }

    public void setName(String id) {
        this.name = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isLoginSuccess() {
        return loginSuccess;
    }

    public void setLoginSuccess(boolean loginSuccess) {
        this.loginSuccess = loginSuccess;
    }
}

(5)注册所需要的数据转入

package com.weh.User;

//注册所需要的数据转入
public class Register {
    String name;
    String Mail;
    String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMail() {
        return Mail;
    }

    public void setMile(String Mail) {
        this.Mail = Mail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

(6)登录验证的主要代码:

package com.weh.User;

import com.weh.ClientDemo.Client;
import com.weh.ClientDemo.ClientFrame;
import com.weh.ClientDemo.PresentFrame;
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//匹配输入的登录数据,以及对数据库驱动的开启
public class UserLogin {
    Connection connection;
    PreparedStatement prepare;
    ResultSet resultSet;
    String sql;
    boolean loginSuccess;

    public UserLogin() throws SQLException, ClassNotFoundException {
        //连接mysql数据库
        connection= new JDBCMysql().connection;
    }
    public void readLogin(Login login) {
        try {
            if(LoginDemo.flag){
                //用户名密码登录
                sql = "SELECT * FROM test.User WHERE UserMail = ?";//查询语句
                prepare = connection.prepareStatement(sql);//创建执行环境
                //验证码登录
                prepare.setString(1, login.getQQmail());
                resultSet = prepare.executeQuery();//执行查询语句,如数据匹配成功,则返回true
                if (resultSet.next()) { //迭代查询
                    login.setLoginSuccess(true);
                    String randomData=String.valueOf(LoginDemo.random);
                    if(login.gettField().equals(randomData)){
                        JOptionPane.showMessageDialog(null, "登录成功");
                        login.getLoginDemo().dispose();
                        Thread.sleep(1000);
                        Client client = new Client(resultSet.getString(1).substring(0,resultSet.getString(1).indexOf("@")),
                                resultSet.getString(3));
                        PresentFrame.setClient(client);
                        new ClientFrame().setVisible(true);
                        new Thread(client).start();
                    }else{
                        if(login.gettField().isEmpty()){
                            JOptionPane.showMessageDialog(null, "请输入验证码","提示", JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                        JOptionPane.showMessageDialog(null, "您的验证码输入有误,请重新输入!","提示", JOptionPane.WARNING_MESSAGE);
                    }
                }else{
                    login.setLoginSuccess(false);
                    JOptionPane.showMessageDialog(null, "您的电子邮箱还未注册,请先注册,再登录!","提示", JOptionPane.WARNING_MESSAGE);
                }
            }else{
                if(login.isTem()){
                    sql = " SELECT * FROM test.User WHERE UserMail = ?";//查询语句
                    prepare = connection.prepareStatement(sql);//创建执行环境
                    //验证码登录
                    prepare.setString(1, RePassword.mailField.getText());
                    resultSet = prepare.executeQuery();//执行查询语句,如数据匹配成功,则返回true
                    if (resultSet.next()) { //迭代查询
                        String randomData=String.valueOf(RePassword.random);
                        if(RePassword.tFd.getText().equals(randomData)){
                            JOptionPane.showMessageDialog(null, "审核成功!","提示", JOptionPane.WARNING_MESSAGE);
                            new SetPassword();
                        }else{
                            if(RePassword.tFd.getText().isEmpty()){
                                JOptionPane.showMessageDialog(null, "请输入验证码","提示", JOptionPane.WARNING_MESSAGE);
                                return;
                            }
                            JOptionPane.showMessageDialog(null, "您的验证码输入有误,请重新输入!","提示", JOptionPane.WARNING_MESSAGE);
                        }
                    }else{
                        if(RePassword.mailField.getText().isEmpty()){
                            JOptionPane.showMessageDialog(null, "请输入您的邮箱","提示", JOptionPane.WARNING_MESSAGE);
                            return;
                        }
                        JOptionPane.showMessageDialog(null, "您的电子邮箱还未注册,请先注册,再登录!","提示", JOptionPane.WARNING_MESSAGE);
                    }
                }else{
                    //用户名密码登录
                    sql = "SELECT * FROM test.User WHERE UserName = ? AND UserPassword = ?";//查询语句
                    prepare = connection.prepareStatement(sql);//创建执行环境
                    prepare.setString(1, login.getName());
                    prepare.setString(2, login.getPassword());
                    resultSet = prepare.executeQuery();//执行查询语句,如数据匹配成功,则返回true
                    if (resultSet.next()) { //迭代查询
                        login.setLoginSuccess(true);
                        JOptionPane.showMessageDialog(null, "登录成功");
                        
                    } else {
                        login.setLoginSuccess(false);
                        if (login.getName().isEmpty() || login.getPassword().isEmpty()) {
                            if (login.getName().isEmpty()) {
                                JOptionPane.showMessageDialog(null, "用户名不能为空!", "提示", JOptionPane.WARNING_MESSAGE);
                            }
                            if (login.getPassword().isEmpty()){
                                JOptionPane.showMessageDialog(null, "密码不能为空!","提示", JOptionPane.WARNING_MESSAGE);
                            }
                        }else{
                            JOptionPane.showMessageDialog(null, "用户名不存在或者密码输入有误!", "警告", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
                //释放资源和空间
                connection.close();
                prepare.close();
                resultSet.close();
                loginSuccess = login.isLoginSuccess();
            } catch (SQLException | InterruptedException e) {
            JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.ERROR_MESSAGE);
        }

    }
}


(7)注册验证的代码如下:

package com.weh.User;

import com.weh.Mail.SendMail;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//插入数据,以及对数据库驱动的开启
public class UserRegister {	//注册数据处理者
    Connection connection = null;
    PreparedStatement presql1;   //预处理对象
    public UserRegister() throws SQLException {
        //连接mysql数据库
        connection= new JDBCMysql().connection;
    }
    public void writeRegister(Register register){
        int flag;
        String sql1 = "INSERT INTO test.User VALUES (?,?,?)"; //插入语句
        try {
            presql1 = connection.prepareStatement(sql1);//发送插入语句
            presql1.setString(1,register.getMail());
            presql1.setString(2,register.getPassword());
            presql1.setString(3,register.getName());
            flag = presql1.executeUpdate(); //成功插入式返回1
            new Thread(new SendMail(register.getMail(),"HI注册验证/您好:"+"\n\t您成功绑定了您的邮箱,您可登录HI。")).start();
            if (flag!=0){
                JOptionPane.showMessageDialog(null,"注册成功");
            }else {
                JOptionPane.showMessageDialog(null,"注册失败","提示",JOptionPane.WARNING_MESSAGE);
            }
            //释放资源和空间
            connection.close();
            presql1.close();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"注册失败!","警告",JOptionPane.WARNING_MESSAGE);
        }
    }
}

(8)重置密码

class SetPassword extends JFrame{
    static JTextField newField = new JTextField();
    static JPasswordField newFd = new JPasswordField();
    static RePassword rePassword = null;
    static String userMail=null;
    public SetPassword(){
        rePassword.dispose();
        //获取显示屏的大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int sw = screenSize.width;
        int sh = screenSize.height;
        //设置窗口的位置
        int width = 420;
        int height = 250;
        this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);
        this.setUndecorated(true);//不要边框
        //      盒子模块
        Box ubox = Box.createHorizontalBox();
        Box pbox = Box.createHorizontalBox();
        Box vbox = Box.createVerticalBox();
        JLabel label = new JLabel("重置密码");
        label.setForeground(new Color(0x3E5C5C));
        label.setFont(new Font("黑体", Font.BOLD, 13));
        JPanel headpanel = new JPanel();
        headpanel.add(label);
        headpanel.setSize(width,30);
        headpanel.setBackground(new Color(0x08BDFD));

        //      创建界面工具类
        JLabel newLabel = new JLabel("重置密码:"); //文本设计
        newLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        newLabel.setForeground(new Color(0x3E5C5C)); // 设置前景色
        newField.setFont(new Font("Arial", Font.BOLD, 13));
        newField.setToolTipText("重置密码");// 悬停显示
        newField.setColumns(12);

        JLabel tLabel = new JLabel("确认密码:");    //文本设计
        tLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        tLabel.setForeground(new Color(0x3E5C5C)); // 设置前景色
        newFd.setFont(new Font("Arial", Font.BOLD, 13));
        newFd.setToolTipText("确认密码");// 悬停显示
        newFd.setColumns(12);
        newFd.setEchoChar('●');// 星星符号

        JButton button1 = new JButton("确定");    //登录按钮
        button1.setToolTipText("确定");// 悬停显示

        JMenu Menubutton = new JMenu("返回登录");    //重置按钮
        Menubutton.setToolTipText("返回登录");// 悬停显示
        //      字体设置
        button1.setFont(new Font("黑体", Font.BOLD, 18));
        button1.setForeground(Color.white); // 设置前景色
        button1.setBackground(new Color(0x08BDFD));
        button1.setDefaultCapable(true);
        button1.setBounds((this.getWidth() - 200) / 2, 190, 200, 30); // 设置按钮位置,及按钮大小
        button1.setCursor(new Cursor(Cursor.HAND_CURSOR));  //鼠标手势的设置


        Menubutton.setFont(new Font("微软雅黑", Font.BOLD, 12));
        Menubutton.setForeground(new Color(0x324945)); // 设置前景色
        Menubutton.setUI(new BasicButtonUI());  //恢复基本视觉效果
        Menubutton.setBounds(0,height-30, 85, 20); // 设置按钮位置,及按钮大小
        Menubutton.setContentAreaFilled(false); // 设置按钮透明
        Menubutton.setCursor(new Cursor(Cursor.HAND_CURSOR));


        //        小盒子,设计邮箱模块
        ubox.add(newLabel);
        ubox.add(Box.createHorizontalStrut(5));//插入中间盒子宽度为5,作为相邻文本的空隙
        ubox.add(newField);
        //        小盒子,设计验证码模块
        pbox.add(tLabel);
        pbox.add(Box.createHorizontalStrut(5));
        pbox.add(newFd);


        //      大盒子
        vbox.add(Box.createVerticalStrut(60));//插入中间盒子高度为60,作为上下文本的空隙
        vbox.add(ubox);
        vbox.add(Box.createVerticalStrut(40));
        vbox.add(pbox);


        //转换
        JPanel panel = new JPanel();
        panel.setUI(new BasicPanelUI());  //恢复基本视觉效果
        panel.setBackground(new Color(0xFDFFFF));
        panel.add(vbox, BorderLayout.CENTER);//将大盒子添加到jLabel盒子里
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(headpanel);
        this.add(button1);
        this.add(Menubutton);
        this.add(panel);
        this.setVisible(true);
        this.setResizable(false);
        new MousePressedSet().mousePressedSet(this);//鼠标拖动窗口

        button1.addActionListener(e -> {
            label.setText("正在上传中...");
            PreparedStatement prepare;
            Connection connection;
            String sql;
            if(newField.getText().equals(newFd.getText()) && !newField.getText().isEmpty()){
                try {
                    connection=new JDBCMysql().connection;
                    sql="UPDATE test.User SET UserPassword = ? WHERE UserMail=?";
                    prepare = connection.prepareStatement(sql);//创建执行环境
                    prepare.setString(1, newField.getText());//重置后的密码
                    prepare.setString(2, userMail);//邮箱
                    int result = prepare.executeUpdate();
                    if(result==1){
                        JOptionPane.showMessageDialog(null, "修改成功!","提示", JOptionPane.WARNING_MESSAGE);
                        this.dispose();
                        new LoginDemo();
                    }else{
                        JOptionPane.showMessageDialog(null, "修改失败!","提示", JOptionPane.WARNING_MESSAGE);
                    }
                    connection.close();
                    prepare.close();
                } catch (Exception event) {
                    JOptionPane.showMessageDialog(null, "数据库连接失败!","提示", JOptionPane.WARNING_MESSAGE);
                }
            }else{
                if(newField.getText().isEmpty()){
                    JOptionPane.showMessageDialog(null, "密码不能为空!","提示", JOptionPane.WARNING_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null, "您输入的两次密码不一致,请重新输入!","提示", JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        Menubutton.addActionListener(e -> {
            this.dispose();
            try {
                Thread.sleep(1000);
                new LoginDemo();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        });
    }
}
  • 33
    点赞
  • 294
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT_WEH_coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值