目录
1.项目功能展示
2.新建一个以Maven为系统的的项目
3.创建User类以及Login类以及Register类
(1)首先我们先创建一个User的JavaBean,包括id,username以及password
(2)创建Login类以及利用Swing实现GUI图形设计并实现读取Json数据与用户输入信息进行校验
(3)创建Register类以及利用Swing实现GUI图形设计并实现创建用户更新Json数据
4.新建测试类以及Json数据
此项目是将用户的所有信息存放在本地JSON文件中,所以我们需要解析JSON数据,本项目使用的是google的Gson,我们需要在项目的配置文件pom.xml中写入以下代码:
com.google.code.gson
gson
2.8.2
我们需要解析时,只要导入Gson即可,可以根据需求按需导入
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class User {
private String id;
private String username;
private String password;
public User(String id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User() {
}
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;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class Login extends JFrame implements ActionListener {
private JTextField usernameText;
private JPasswordField passwordText;
private JButton loginBtn;
private JButton registerBtn;
public Login() {
setTitle("登录");
setAlwaysOnTop(true);
setLayout(null);//关闭默认布局类型 自己手动设置布局
setSize(400, 400);
setDefaultCloseOperation(3);//设置关闭模式
setLocationRelativeTo(null);//设置GUI显示居中
//创建界面组件
JLabel username = new JLabel("账号:");
JLabel password = new JLabel("密码:");
loginBtn = new JButton("登录");
loginBtn.addActionListener(this);//监听登录事件
registerBtn = new JButton("注册");
registerBtn.addActionListener(this);//监听注册事件
usernameText = new JTextField(15);
passwordText = new JPasswordField(15);
// 设置字体和背景颜色
usernameText.setForeground(Color.BLACK);
passwordText.setForeground(Color.BLACK);
usernameText.setBorder(BorderFactory.createLineBorder(Color.GRAY));
passwordText.setBorder(BorderFactory.createLineBorder(Color.GRAY));
//创建装组件的容器
JPanel usernameJp = new JPanel();
JPanel passwordJp = new JPanel();
JPanel loginJp = new JPanel();
//设置容器的位置
usernameJp.setBounds(30, 0, 300, 50);
passwordJp.setBounds(30, 40, 300, 50);
loginJp.setBounds(30, 90, 300, 60);
usernameJp.add(username);
usernameJp.add(usernameText);
passwordJp.add(password);
passwordJp.add(passwordText);
loginJp.add(loginBtn);
loginJp.add(registerBtn);
//将组件装入GUI
add(usernameJp);
add(passwordJp);
add(loginJp);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginBtn) {
String username = usernameText.getText();
String password = new String(passwordText.getPassword());
// 根据读取的用户账号信息进行校验
boolean temp = readUser(username, password);
System.out.println(temp);
if (temp) {
JOptionPane.showMessageDialog(this, "登录成功", "Success", JOptionPane.INFORMATION_MESSAGE);
this.dispose();//登录成功关闭此窗口 跳转页面
} else {
JOptionPane.showMessageDialog(this, "登陆失败", "Error", JOptionPane.ERROR_MESSAGE);
}
}//注册操作
else {
this.dispose();
new Register();
}
}
//读取用户数据
public boolean readUser(String username, String password) {
try (BufferedReader br = new BufferedReader(new FileReader("Json文件的相对路径或绝对路径"))) {
String json = "";
String line;
while ((line = br.readLine()) != null) {
json += line + "\n";
}
Gson gson = new Gson();
ArrayList dataList = gson.fromJson(json, new TypeToken>() {
}.getType());
for (User data : dataList) {
if (data.getUsername().equals(username) && data.getPassword().equals(password)) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
此时的运行图为:
等我们点击登录就会校验用户输入的账号和密码,若点击注册则会关闭当前的窗口,跳转到注册的窗口。
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
public class Register extends JFrame implements ActionListener {
private JTextField usernameText;
private JPasswordField passwordText;
private JPasswordField passwordTextTrue;
private JButton registerBtn;
public Register() {
setLayout(null);
setTitle("注册");
setSize(400, 400);
setAlwaysOnTop(true);//设置界面一直处于最上层
setDefaultCloseOperation(3);
setLocationRelativeTo(null);
//组件
JLabel username = new JLabel("账号:");
JLabel password = new JLabel("密码:");
JLabel passwordTrue = new JLabel("确认密码:");
registerBtn = new JButton("注册");
registerBtn.addActionListener(this);
usernameText = new JTextField(15);
passwordText = new JPasswordField(15);
passwordTextTrue = new JPasswordField(15);
// 设置字体和背景颜色
usernameText.setForeground(Color.BLACK);
passwordText.setForeground(Color.BLACK);
passwordTextTrue.setForeground(Color.BLACK);
usernameText.setBorder(BorderFactory.createLineBorder(Color.GRAY));
passwordText.setBorder(BorderFactory.createLineBorder(Color.GRAY));
passwordTextTrue.setBorder(BorderFactory.createLineBorder(Color.GRAY));
//创建装组件的容器
JPanel usernameJp = new JPanel();
JPanel passwordJp = new JPanel();
JPanel passwordTrueJp = new JPanel();
JPanel registerJp = new JPanel();
//设置容器的位置
usernameJp.setBounds(30, 0, 300, 40);
passwordJp.setBounds(30, 50, 300, 40);
passwordTrueJp.setBounds(18, 100, 300, 40);
registerJp.setBounds(60, 140, 300, 50);
usernameJp.add(username);
usernameJp.add(usernameText);
passwordJp.add(password);
passwordJp.add(passwordText);
passwordTrueJp.add(passwordTrue);
passwordTrueJp.add(passwordTextTrue);
registerJp.add(registerBtn);
//将组件装入GUI
add(usernameJp);
add(passwordJp);
add(passwordTrueJp);
add(registerJp);
setVisible(true);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameText.getText();
String password = new String(passwordText.getPassword());
String passwordTrue = new String(passwordTextTrue.getPassword());
ArrayList user = getUserList();
for (User data : user) {
System.out.println(data.getUsername());
}
boolean found = false;
found = (!username.equals("") && !password.equals("") && !passwordTrue.equals("") && password.equals(passwordTrue));
System.out.println(found);
if (found) {
boolean temp = readUser(username, user);//检测用户名是否重复
if (temp) {
JOptionPane.showMessageDialog(this, "注册成功", "Success", JOptionPane.INFORMATION_MESSAGE);
String id = "00" + (user.toArray().length + 1);
user.add(new User(id, username, password));
writeUser(username, password, user);//将新用户的数据写入json表中
this.dispose();
new Login();
} else {
JOptionPane.showMessageDialog(this, "用户名重复,注册失败", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "注册失败,请正确填写用户名和密码", "Error", JOptionPane.ERROR_MESSAGE);
}
}
//获取用户的所有信息数据
public ArrayList getUserList() {
try (BufferedReader br = new BufferedReader(new FileReader("json文件的路径"))) {
String json = "";
String line;
while ((line = br.readLine()) != null) {
json += line + "\n";
}
Gson gson = new Gson();
ArrayList dataList = gson.fromJson(json, new TypeToken>() {
}.getType());
return dataList;
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList();
}
//读取用户数据 检查有没有出现用户名重复的情况
public boolean readUser(String username, ArrayList user) {
for (User data : user) {
if (data.getUsername().equals(username)) {
return false;
}
}
return true;
}
//将用户信息写入json文件中
public void writeUser(String username, String password, ArrayList user) {
try (Writer writer = new FileWriter("json文件路径")) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(user, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
注册的运行图为:
此窗口的功能可以实现注册用户并更新json文件数据,还可以进行安全性检验,防止用户名出现重复的情况,当注册成功之后就会跳转到登录页面。
(1)我们可以随意新建一个测试类,然后在测试类中写入以下代码
public class Main {
public static void main(String[] args) {
new Login();
}
}
(2)新建一个data.json文件
[
{
"id": "001",
"username": "zs",
"password": "123456"
},
{
"id": "002",
"username": "ls",
"password": "123456"
},
{
"id": "003",
"username": "wu",
"password": "123456"
}
]
我们尝试登录,如果账号密码输入正确就会弹出成功的提示框,否则会弹出失败的提示框
我们也可以尝试注册一个新用户
当点击确定后就会跳转到登录页面,而且可以看到我们的json数据已经及时更新了
当然也会出现注册失败的情况,比如用户名重复,或者是密码输入前后二次不相同
项目源码在:GitHub - jiangjunjie666/JavaGui: JavaGui实现登录注册业务并将用户数据存储在Json文件中