0


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

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;/*
*   登录窗口
*/publicclassLoginDemoextendsJFrame{
    JTextField uField;
    JPasswordField pFd;
    JTextField QQField ;
    JTextField tField;staticint random;staticboolean flag=false;
    Login login=newLogin();publicLoginDemo(){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 =newImageIcon(image);
        background.setImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        JLabel label =newJLabel(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 =newJPanel();//盒子模块
        Box ubox = Box.createHorizontalBox();
        Box pbox = Box.createHorizontalBox();
        Box vbox = Box.createVerticalBox();//创建界面工具类
        JLabel uLabel =newJLabel("用户名:");//文本设计
        uLabel.setFont(newFont("微软雅黑", Font.BOLD,15));
        uLabel.setForeground(newColor(0xFBFCFD));// 设置前景色
        uField =newJTextField();//输入框
        uField.setToolTipText("HI");// 悬停显示
        uField.setFont(newFont("微软雅黑", Font.BOLD,15));
        uField.setColumns(12);

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

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

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

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

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

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

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

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

        Menubutton6.setFont(newFont("微软雅黑", Font.BOLD,15));
        Menubutton6.setForeground(newColor(0x383939));// 设置前景色
        Menubutton6.setUI(newBasicButtonUI());//恢复基本视觉效果
        Menubutton6.setBounds(width-190,225,100,20);// 设置按钮位置,及按钮大小
        Menubutton6.setContentAreaFilled(false);// 设置按钮透明
        Menubutton6.setCursor(newCursor(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(newBasicPanelUI());//恢复基本视觉效果
        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 =newJTextField();//输入框
        tField =newJTextField();//输入框
        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=newRePassword();});

        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(newFont("黑体", Font.BOLD,18));
            QQField.setColumns(12);
            tField.setToolTipText("验证码");// 悬停显示
            tField.setFont(newFont("黑体", 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分钟内有效,请勿泄露于他人。";newThread(newSendMail(QQField.getText(),str)).start();newThread(newTimeDown(button3,QQField)).start();}else{if(QQField.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"电子邮件不能为空!","警告", JOptionPane.WARNING_MESSAGE);return;}
                JOptionPane.showMessageDialog(null,"电子邮件输入有误,请正确输入!","警告", JOptionPane.WARNING_MESSAGE);}});
        uField.addFocusListener(newFocusListener(){@OverridepublicvoidfocusGained(FocusEvent e){//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if(uField.getText().equals("请输入您的用户名")){
                    uField.setText("");//将提示文字清空
                    uField.setFont(newFont("微软雅黑", Font.BOLD,15));
                    uField.setForeground(newColor(1));//设置用户输入的字体颜色为黑色}}@OverridepublicvoidfocusLost(FocusEvent e){//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字if(uField.getText().equals("")){
                    uField.setForeground(newColor(0xC7C5C5));//将提示文字设置为灰色
                    uField.setFont(newFont("微软雅黑", Font.PLAIN,12));
                    uField.setText("请输入您的用户名");//显示提示文字}}});

        pFd.addFocusListener(newFocusListener(){@OverridepublicvoidfocusGained(FocusEvent e){//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if(pFd.getText().equals("请输入密码")){
                    pFd.setText("");//将提示文字清空
                    pFd.setFont(newFont("微软雅黑", Font.BOLD,15));
                    pFd.setForeground(newColor(222,221,221));//设置用户输入的字体颜色为黑色}}@OverridepublicvoidfocusLost(FocusEvent e){//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字if(pFd.getText().equals("")){
                    pFd.setForeground(Color.gray);//将提示文字设置为灰色
                    pFd.setFont(newFont("微软雅黑", 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);newRegisterDemo();}catch(InterruptedException interruptedException){
                JOptionPane.showMessageDialog(null,"异常","警告", JOptionPane.WARNING_MESSAGE);}});}publicvoidinit(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(newString(p));}
            UserLogin =newUserLogin();
            UserLogin.readLogin(login);}catch(SQLException| ClassNotFoundException e){
            JOptionPane.showMessageDialog(null,"异常","警告", JOptionPane.WARNING_MESSAGE);}}}classTimeDownimplementsRunnable{
    JButton button;
    JTextField textField;publicTimeDown(JButton button,JTextField textField){this.button=button;this.textField=textField;}@Overridepublicvoidrun(){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);}}}classRePasswordextendsJFrame{staticint random;
    Login login =newLogin();static JTextField mailField =newJTextField();static JTextField tFd =newJTextField();publicRePassword(){//获取显示屏的大小
        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 =newJLabel("");
        label.setForeground(newColor(0x3E5C5C));
        label.setFont(newFont("黑体", Font.BOLD,13));
        JPanel headpanel =newJPanel();
        headpanel.add(label);
        headpanel.setSize(width,30);
        headpanel.setBackground(newColor(0x08BDFD));//      创建界面工具类
        JLabel mailLabel =newJLabel("邮箱:");//文本设计
        mailLabel.setFont(newFont("微软雅黑", Font.BOLD,13));
        mailLabel.setForeground(newColor(0x3E5C5C));// 设置前景色
        mailField.setFont(newFont("Arial", Font.BOLD,13));
        mailField.setToolTipText("邮箱");// 悬停显示
        mailField.setColumns(12);

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

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

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

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

        Menubutton.setFont(newFont("微软雅黑", Font.BOLD,12));
        Menubutton.setForeground(newColor(0x324945));// 设置前景色
        Menubutton.setUI(newBasicButtonUI());//恢复基本视觉效果
        Menubutton.setBounds(0,height-30,85,20);// 设置按钮位置,及按钮大小
        Menubutton.setContentAreaFilled(false);// 设置按钮透明
        Menubutton.setCursor(newCursor(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 =newJPanel();
        panel.setUI(newBasicPanelUI());//恢复基本视觉效果
        panel.setBackground(newColor(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);newMousePressedSet().mousePressedSet(this);//鼠标拖动窗口

        button1.addActionListener(e ->{
            label.setText("正在通过邮箱找回密码 ...");try{if(login.isTem()){
                   SetPassword.userMail= mailField.getText();newUserLogin().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分钟内有效,请勿泄露于他人。";newThread(newSendMail(mailField.getText(),str)).start();newThread(newTimeDown(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);newLoginDemo();}catch(InterruptedException interruptedException){
                interruptedException.printStackTrace();}});}}classSetPasswordextendsJFrame{static JTextField newField =newJTextField();static JPasswordField newFd =newJPasswordField();static RePassword rePassword = null;static String userMail=null;publicSetPassword(){
        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 =newJLabel("重置密码");
        label.setForeground(newColor(0x3E5C5C));
        label.setFont(newFont("黑体", Font.BOLD,13));
        JPanel headpanel =newJPanel();
        headpanel.add(label);
        headpanel.setSize(width,30);
        headpanel.setBackground(newColor(0x08BDFD));//      创建界面工具类
        JLabel newLabel =newJLabel("重置密码:");//文本设计
        newLabel.setFont(newFont("微软雅黑", Font.BOLD,13));
        newLabel.setForeground(newColor(0x3E5C5C));// 设置前景色
        newField.setFont(newFont("Arial", Font.BOLD,13));
        newField.setToolTipText("重置密码");// 悬停显示
        newField.setColumns(12);

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

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

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

        Menubutton.setFont(newFont("微软雅黑", Font.BOLD,12));
        Menubutton.setForeground(newColor(0x324945));// 设置前景色
        Menubutton.setUI(newBasicButtonUI());//恢复基本视觉效果
        Menubutton.setBounds(0,height-30,85,20);// 设置按钮位置,及按钮大小
        Menubutton.setContentAreaFilled(false);// 设置按钮透明
        Menubutton.setCursor(newCursor(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 =newJPanel();
        panel.setUI(newBasicPanelUI());//恢复基本视觉效果
        panel.setBackground(newColor(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);newMousePressedSet().mousePressedSet(this);//鼠标拖动窗口

        button1.addActionListener(e ->{
            label.setText("正在上传中...");
            PreparedStatement prepare;
            Connection connection;
            String sql;if(newField.getText().equals(newFd.getText())&&!newField.getText().isEmpty()){try{
                    connection=newJDBCMysql().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();newLoginDemo();}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);newLoginDemo();}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;/*
 *   注册窗口
 */publicclassRegisterDemoextendsJFrame{publicRegisterDemo(){//获取显示屏的大小
        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 =newImageIcon(image);// 背景图片
        background.setImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
        JLabel label =newJLabel(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 =newJLabel("用 户 名 :");//文本设计
        nameLabel.setFont(newFont("微软雅黑", Font.BOLD,13));
        nameLabel.setForeground(newColor(0xFF03FC6E,true));// 设置前景色
        JTextField nameField =newJTextField();//输入框
        nameField.setFont(newFont("黑体", Font.BOLD,15));
        nameField.setToolTipText("用户名");
        nameField.setColumns(15);

        JLabel uLabel =newJLabel("电子邮件:");//文本设计
        uLabel.setFont(newFont("微软雅黑", Font.BOLD,13));
        uLabel.setForeground(newColor(0xFF03FC6E,true));// 设置前景色
        JTextField uField =newJTextField("[email protected]");//输入框
        uField.setFont(newFont("Arial", Font.BOLD,15));
        uField.setToolTipText("电子邮件");
        uField.setColumns(15);

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

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

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

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

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

        Menubutton3.setFont(newFont("微软雅黑", Font.BOLD,12));
        Menubutton3.setForeground(Color.white);// 设置前景色
        Menubutton3.setBackground(newColor(0x08BDFD));
        Menubutton3.setUI(newBasicButtonUI());//恢复基本视觉效果
        Menubutton3.setBounds(5,this.getHeight()-70,85,20);// 设置按钮位置,及按钮大小
        Menubutton3.setContentAreaFilled(false);// 设置按钮透明
        Menubutton3.setCursor(newCursor(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 =newJPanel();
        panel.setUI(newBasicPanelUI());//恢复基本视觉效果
        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(newFocusListener(){@OverridepublicvoidfocusGained(FocusEvent e){//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if(nameField.getText().equals("填写用户名")){
                    nameField.setText("");//将提示文字清空
                    nameField.setFont(newFont("微软雅黑", Font.BOLD,15));
                    nameField.setForeground(newColor(1));//设置用户输入的字体颜色为黑色}}@OverridepublicvoidfocusLost(FocusEvent e){//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字if(nameField.getText().equals("")){
                    nameField.setForeground(newColor(0xC7C5C5));//将提示文字设置为灰色
                    nameField.setFont(newFont("微软雅黑", Font.PLAIN,12));
                    nameField.setText("填写用户名");//显示提示文字}}});

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

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

        RepFd.addFocusListener(newFocusListener(){@OverridepublicvoidfocusGained(FocusEvent e){//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if( RepFd.getText().equals("填写密码")){
                    RepFd.setText("");//将提示文字清空
                    RepFd.setFont(newFont("微软雅黑", Font.BOLD,15));
                    RepFd.setForeground(newColor(1));//设置用户输入的字体颜色为黑色}}@OverridepublicvoidfocusLost(FocusEvent e){//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字if(RepFd.getText().equals("")){
                    RepFd.setForeground(newColor(0xC7C5C5));//将提示文字设置为灰色
                    RepFd.setFont(newFont("微软雅黑", Font.PLAIN,12));
                    RepFd.setText("填写密码");//显示提示文字}}});//提交按钮监听
        button1.addActionListener(e ->{
            String pField =newString(pFd.getPassword());//转换
            String repField =newString(RepFd.getPassword());//转换try{if(nameField.getText().isEmpty()|| pField.isEmpty()|| repField.isEmpty()||uField.getText().isEmpty()){if(nameField.getText().isEmpty()){
                        JOptionPane.showMessageDialog(null,"用户名不能为空","提示", JOptionPane.WARNING_MESSAGE);}elseif(uField.getText().isEmpty()){
                        JOptionPane.showMessageDialog(null,"电子邮件不能为空","提示", JOptionPane.WARNING_MESSAGE);}elseif(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);newLoginDemo();}catch(Exception exception){
                JOptionPane.showMessageDialog(null,"异常","警告", JOptionPane.WARNING_MESSAGE);}});}publicvoidinit(JTextField nameField,JTextField uField, JPasswordField pFd){
        Register register;
        UserRegister userRegister;try{
            register =newRegister();
            register.setName(nameField.getText());
            register.setMile(uField.getText());char[] p1 = pFd.getPassword();
            register.setPassword(newString(p1));
            userRegister =newUserRegister();
            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;publicclassJDBCMysql{
    Connection connection;publicJDBCMysql(){try{
            MysqlDataSource mysqlDataSource =newMysqlDataSource();
            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;//登录所需要的数据转入publicclassLogin{
    String name;
    String password;
    LoginDemo loginDemo;
    String mail;
    String tField;boolean tem;publicbooleanisTem(){return tem;}publicvoidsetTem(boolean tem){this.tem = tem;}public String gettField(){return tField;}publicvoidsettField(String tField){this.tField = tField;}boolean loginSuccess =false;public String getQQmail(){return mail;}publicvoidsetQQmail(String mail){this.mail = mail;}public LoginDemo getLoginDemo(){return loginDemo;}publicvoidsetLoginDemo(LoginDemo loginDemo){this.loginDemo = loginDemo;}public String getName(){return name;}publicvoidsetName(String id){this.name = id;}public String getPassword(){return password;}publicvoidsetPassword(String password){this.password = password;}publicbooleanisLoginSuccess(){return loginSuccess;}publicvoidsetLoginSuccess(boolean loginSuccess){this.loginSuccess = loginSuccess;}}

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

package com.weh.User;//注册所需要的数据转入publicclassRegister{
    String name;
    String Mail;
    String password;public String getName(){return name;}publicvoidsetName(String name){this.name = name;}public String getMail(){return Mail;}publicvoidsetMile(String Mail){this.Mail = Mail;}public String getPassword(){return password;}publicvoidsetPassword(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;//匹配输入的登录数据,以及对数据库驱动的开启publicclassUserLogin{
    Connection connection;
    PreparedStatement prepare;
    ResultSet resultSet;
    String sql;boolean loginSuccess;publicUserLogin()throws SQLException, ClassNotFoundException {//连接mysql数据库
        connection=newJDBCMysql().connection;}publicvoidreadLogin(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();//执行查询语句,如数据匹配成功,则返回trueif(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 =newClient(resultSet.getString(1).substring(0,resultSet.getString(1).indexOf("@")),
                                resultSet.getString(3));
                        PresentFrame.setClient(client);newClientFrame().setVisible(true);newThread(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();//执行查询语句,如数据匹配成功,则返回trueif(resultSet.next()){//迭代查询
                        String randomData=String.valueOf(RePassword.random);if(RePassword.tFd.getText().equals(randomData)){
                            JOptionPane.showMessageDialog(null,"审核成功!","提示", JOptionPane.WARNING_MESSAGE);newSetPassword();}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();//执行查询语句,如数据匹配成功,则返回trueif(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;//插入数据,以及对数据库驱动的开启publicclassUserRegister{//注册数据处理者
    Connection connection = null;
    PreparedStatement presql1;//预处理对象publicUserRegister()throws SQLException {//连接mysql数据库
        connection=newJDBCMysql().connection;}publicvoidwriteRegister(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();//成功插入式返回1newThread(newSendMail(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)重置密码

classSetPasswordextendsJFrame{static JTextField newField =newJTextField();static JPasswordField newFd =newJPasswordField();static RePassword rePassword = null;static String userMail=null;publicSetPassword(){
        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 =newJLabel("重置密码");
        label.setForeground(newColor(0x3E5C5C));
        label.setFont(newFont("黑体", Font.BOLD,13));
        JPanel headpanel =newJPanel();
        headpanel.add(label);
        headpanel.setSize(width,30);
        headpanel.setBackground(newColor(0x08BDFD));//      创建界面工具类
        JLabel newLabel =newJLabel("重置密码:");//文本设计
        newLabel.setFont(newFont("微软雅黑", Font.BOLD,13));
        newLabel.setForeground(newColor(0x3E5C5C));// 设置前景色
        newField.setFont(newFont("Arial", Font.BOLD,13));
        newField.setToolTipText("重置密码");// 悬停显示
        newField.setColumns(12);

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

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

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

        Menubutton.setFont(newFont("微软雅黑", Font.BOLD,12));
        Menubutton.setForeground(newColor(0x324945));// 设置前景色
        Menubutton.setUI(newBasicButtonUI());//恢复基本视觉效果
        Menubutton.setBounds(0,height-30,85,20);// 设置按钮位置,及按钮大小
        Menubutton.setContentAreaFilled(false);// 设置按钮透明
        Menubutton.setCursor(newCursor(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 =newJPanel();
        panel.setUI(newBasicPanelUI());//恢复基本视觉效果
        panel.setBackground(newColor(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);newMousePressedSet().mousePressedSet(this);//鼠标拖动窗口

        button1.addActionListener(e ->{
            label.setText("正在上传中...");
            PreparedStatement prepare;
            Connection connection;
            String sql;if(newField.getText().equals(newFd.getText())&&!newField.getText().isEmpty()){try{
                    connection=newJDBCMysql().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();newLoginDemo();}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);newLoginDemo();}catch(InterruptedException interruptedException){
                interruptedException.printStackTrace();}});}}
标签: mysql java 数据库

本文转载自: https://blog.csdn.net/IT_WEH_coder/article/details/124599915
版权归原作者 IT_WEH_coder 所有, 如有侵权,请联系我们删除。

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

还没有评论