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();
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));
ubox.add(uField);
pbox.add(pLabel);
pbox.add(Box.createHorizontalStrut(5));
pbox.add(pFd);
vbox.add(Box.createVerticalStrut(80));
vbox.add(ubox);
vbox.add(Box.createVerticalStrut(60));
vbox.add(pbox);
uField.setText("weh");
pFd.setText("123456");
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(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);
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));
ubox.add(mailField);
pbox.add(tLabel);
pbox.add(Box.createHorizontalStrut(5));
pbox.add(tFd);
pbox.add(button2);
vbox.add(Box.createVerticalStrut(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);
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);
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));
ubox.add(newField);
pbox.add(tLabel);
pbox.add(Box.createHorizontalStrut(5));
pbox.add(newFd);
vbox.add(Box.createVerticalStrut(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);
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();
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));
namebox.add(nameField);
ubox.add(uLabel);
ubox.add(Box.createHorizontalStrut(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));
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");
mysqlDataSource.setUser("root");
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 {
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();
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();
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();
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 {
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();
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));
ubox.add(newField);
pbox.add(tLabel);
pbox.add(Box.createHorizontalStrut(5));
pbox.add(newFd);
vbox.add(Box.createVerticalStrut(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);
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();
}
});
}
}
|