1.项目演示
2.项目说明
在注册窗体,用户名或密码输入不能为空,否则会有弹窗提示,如果输入的用户名存在,也会有弹窗提示。在登录窗体,输入的用户名不存在,也会有弹窗提示,同时用户名或密码不能为空。
3.项目代码
项目代码都放在同一个包下,记得修改你的包名。
3.1.数据库代码
create database gui;
use gui;
create TABLE user
(
id int unsigned primary key auto_increment,
username varchar(20) not null unique,
password varchar(20) not null
) comment '用户';
insert into user(username, password)
values ('lisi', '111');
3.2.java代码
APP.java
package com.example;
import java.sql.SQLException;
/*
启动程序
*/
public class APP {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
new Login();
}
}
Connect.java
package com.example;
import java.sql.*;
import java.util.ArrayList;
/**
* 数据库连接
*/
public class Connect {
public static ArrayList<User> getUser() throws ClassNotFoundException, SQLException {
ArrayList<User> list = new ArrayList<>();
Class.forName("com.mysql.cj.jdbc.Driver");
Connection coon = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/gui", "root", "你的数据库密码");
Statement st = coon.createStatement();
ResultSet rs = st.executeQuery("select * from user");
while (rs.next()) {
list.add(new User(rs.getString("username"), rs.getString("password")));
}
rs.close();
st.close();
coon.close();
return list;
}
public static void editSQL(String sql) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection coon = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/gui", "root", "你的数据库密码");
Statement st = coon.createStatement();
st.executeUpdate(sql);
st.close();
coon.close();
}
}
Login.java
package com.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* 登录
*/
public class Login extends JFrame implements ActionListener, MouseListener {
//账号输入框
JTextField usernameInput = new JTextField();
//密码输入框
JPasswordField passwordInput = new JPasswordField();
//登录按钮
JButton loginButton = new JButton("点击登录");
//注册超链接
JLabel registerLink = new JLabel("<html><a href=\"\">没有账号?点击注册</a></html>");
public static ArrayList<User> list;
public Login() throws SQLException, ClassNotFoundException {
list = Connect.getUser();
setFroms();
setLabel();
}
//窗体布局
public void setFroms() {
this.setSize(500, 500);
this.setTitle("登录");
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
this.setVisible(true);
}
//控件
public void setLabel() {
registerLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标移到注册超链接上,鼠标箭头变为手形
registerLink.setBounds(250, 350, 150, 50);
this.getContentPane().add(registerLink);
registerLink.addMouseListener(this);
loginButton.setBounds(160, 300, 100, 25);
this.getContentPane().add(loginButton);
loginButton.addActionListener(this);
JLabel ts1 = new JLabel("用户名:");
JLabel ts2 = new JLabel("密码:");
ts1.setBounds(120, 140, 50, 50);
this.getContentPane().add(ts1);
ts2.setBounds(120, 180, 50, 50);
this.getContentPane().add(ts2);
usernameInput.setBounds(170, 155, 100, 25);
this.getContentPane().add(usernameInput);
passwordInput.setBounds(170, 195, 100, 25);
this.getContentPane().add(passwordInput);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == loginButton) {
System.out.println("登录");
if (usernameInput.getText().isEmpty() || passwordInput.getPassword().length == 0) {
JOptionPane.showMessageDialog(Login.this, "用户名或密码不能输入为空!");
return;
}
String uname = usernameInput.getText();
char[] pwd = passwordInput.getPassword();
String password = new String(pwd);
int index = getIndex(list, uname);
if (index < 0) {
JOptionPane.showMessageDialog(Login.this, "用户名不存在!");
} else {
if (password.equals(list.get(index).getPassword())) {
JOptionPane.showMessageDialog(Login.this, "登陆成功!");
} else {
JOptionPane.showMessageDialog(Login.this, "用户名或密码错误!");
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
//超链接逻辑
Object source = e.getSource();
if (source == registerLink) {
System.out.println("注册");
this.setVisible(false);
try {
new Register();
} catch (SQLException ex) {
throw new RuntimeException(ex);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
//方法,判断输入的用户名是否存在
public int getIndex(ArrayList<User> list, String username) {
for (int i = 0; i < list.size(); i++) {
if (username.equals(list.get(i).getUsername())) {
return i;
}
}
return -1;
}
}
Register.java
package com.example;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* 注册
*/
public class Register extends JFrame implements ActionListener {
//账号输入框
JTextField usernameInput = new JTextField();
//密码输入框
JPasswordField passwordInput1 = new JPasswordField();
JPasswordField passwordInput2 = new JPasswordField();
//登录按钮
JButton registerButton = new JButton("点击注册");
public static ArrayList<User> list;
public Register() throws SQLException, ClassNotFoundException {
list = Connect.getUser();
setFroms();
setLabel();
}
//窗体布局
public void setFroms() {
this.setSize(500, 500);
this.setTitle("注册");
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
this.setVisible(true);
}
//控件
public void setLabel() {
registerButton.setBounds(160, 300, 100, 25);
this.getContentPane().add(registerButton);
registerButton.addActionListener(this);
JLabel ts1 = new JLabel("用户名:");
JLabel ts2 = new JLabel("输入密码:");
JLabel ts3 = new JLabel("确认密码:");
ts1.setBounds(120, 140, 50, 50);
this.getContentPane().add(ts1);
ts2.setBounds(120, 180, 60, 50);
this.getContentPane().add(ts2);
ts3.setBounds(120, 220, 60, 50);
this.getContentPane().add(ts3);
usernameInput.setBounds(170, 155, 100, 25);
this.getContentPane().add(usernameInput);
passwordInput1.setBounds(180, 190, 100, 25);
this.getContentPane().add(passwordInput1);
passwordInput2.setBounds(180, 230, 100, 25);
this.getContentPane().add(passwordInput2);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == registerButton) {
System.out.println("注册");
if (usernameInput.getText().isEmpty() || passwordInput1.getPassword().length == 0 ||
passwordInput2.getPassword().length == 0) {
JOptionPane.showMessageDialog(Register.this, "账号或密码不能为空");
return;
}
String uname = usernameInput.getText();
char[] password1 = passwordInput1.getPassword();
String pwd1 = new String(password1);
char[] password2 = passwordInput2.getPassword();
String pwd2 = new String(password2);
boolean flag = getFlag(uname, list);
if (flag) {
JOptionPane.showMessageDialog(Register.this, "用户名已经存在");
} else {
if (pwd2.equals(pwd1)) {
String sql = "insert into user(username, password)" +
"values ('" + uname + "', '" + pwd2 + "');";
try {
Connect.editSQL(sql);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
JOptionPane.showMessageDialog(Register.this, "注册成功");
try {
new Login();
this.setVisible(false);
} catch (SQLException ex) {
throw new RuntimeException(ex);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
} else {
JOptionPane.showMessageDialog(Register.this, "两次密码输入不一致");
}
}
}
}
//判断用户名是否重复
public boolean getFlag(String username, ArrayList<User> list) {
for (int i = 0; i < list.size(); i++) {
if (username.equals(list.get(i).getUsername())) {
return true;
}
}
return false;
}
}
User.java
package com.example;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}