700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java--用户登录/注册界面(连接Mysql数据库)并可以通过验证码登录

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

时间:2018-06-11 05:38:53

相关推荐

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

1 效果展示

(1)登录界面

(2)注册界面

(3)动图展示

2 内容说明

(1)开发前,需引入一个连接Mysql 数据库驱动mysql-connector-java-5.1.30-bin.jar包

提取码:6666

(2)构建路径

(3)需要下载xampp软件

xampp软件下载

提取码:2255

xampp软件包含ApacheWeb服务器、MysqlWeb服务器、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}@$";Pattern pt =pile(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() {@Overridepublic void focusGained(FocusEvent e) {//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if (uField.getText().equals("请输入您的用户名")) {uField.setText("");//将提示文字清空uField.setFont(new Font("微软雅黑", Font.BOLD, 15));uField.setForeground(new Color(1)); //设置用户输入的字体颜色为黑色}}@Overridepublic 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() {@Overridepublic void focusGained(FocusEvent e) {//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if (pFd.getText().equals("请输入密码")) {pFd.setText("");//将提示文字清空pFd.setFont(new Font("微软雅黑", Font.BOLD, 15));pFd.setForeground(new Color(222, 221, 221)); //设置用户输入的字体颜色为黑色}}@Overridepublic 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;}@Overridepublic 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}@$";Pattern pt =pile(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@"); //输入框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() {@Overridepublic void focusGained(FocusEvent e) {//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if (nameField.getText().equals("填写用户名")) {nameField.setText("");//将提示文字清空nameField.setFont(new Font("微软雅黑", Font.BOLD, 15));nameField.setForeground(new Color(1)); //设置用户输入的字体颜色为黑色}}@Overridepublic 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() {@Overridepublic void focusGained(FocusEvent e) {//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if (uField.getText().equals("填写QQ邮箱,例如,***@")) {uField.setText("");//将提示文字清空uField.setFont(new Font("微软雅黑", Font.BOLD, 15));uField.setForeground(new Color(1)); //设置用户输入的字体颜色为黑色}}@Overridepublic void focusLost(FocusEvent e) {//失去焦点时,用户尚未在文本框内输入任何内容,所以依旧显示提示文字if (uField.getText().equals("")) {uField.setForeground(new Color(0xC7C5C5)); //将提示文字设置为灰色uField.setFont(new Font("微软雅黑", Font.PLAIN, 12));uField.setText("填写QQ邮箱,例如,***@");//显示提示文字}}});pFd.addFocusListener(new FocusListener() {@Overridepublic void focusGained(FocusEvent e) {//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if ( pFd.getText().equals("填写密码")) {pFd.setText("");//将提示文字清空pFd.setFont(new Font("微软雅黑", Font.BOLD, 15));pFd.setForeground(new Color(1)); //设置用户输入的字体颜色为黑色}}@Overridepublic 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() {@Overridepublic void focusGained(FocusEvent e) {//得到焦点时,当前文本框的提示文字和创建该对象时的提示文字一样,说明用户正要键入内容if ( RepFd.getText().equals("填写密码")) {RepFd.setText("");//将提示文字清空RepFd.setFont(new Font("微软雅黑", Font.BOLD, 15));RepFd.setForeground(new Color(1)); //设置用户输入的字体颜色为黑色}}@Overridepublic 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}@$";Pattern p =pile(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();//执行查询语句,如数据匹配成功,则返回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 = 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();//执行查询语句,如数据匹配成功,则返回trueif (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();//执行查询语句,如数据匹配成功,则返回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;//插入数据,以及对数据库驱动的开启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(); //成功插入式返回1new 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();}});}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。