JavaWeb实现用户登录系统以及管理员登录模式-1(未完善)
初步设置一下四个类
1. User
2. UserUI
3. UserListener
4. UserService
User.java
User类主要定义了用户自身的一些属性,例如账号密码之类的
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class User {
private String name;
private String pwd;
private int score;
private String birthday;
private boolean isLogin;
public User(String name,String pwd){
this.name=name;
this.pwd=pwd;
this.score=10;
this.birthday="2000/01/01";
}
public String getpwd(){
return this.pwd;
}
public boolean checkPassword(String pwdIn){
return pwd.equals(pwdIn);//判断输入框中的代码是否正确
}
public int login(String pwdIn){
if(checkPassword(pwdIn)){
if(isLogin){
System.out.println("已经登陆过了");
return 2;
}
isLogin=true;
score+=10;
System.out.println("登陆成功");
return 1;
}else{
System.out.println("密码错误");
return 0;
}
}
}
UserUI
UserUI就是主函数所在之处,用来展示GUI以及相关交互功能的按钮
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class UserUI {
UserListener ul = new UserListener ();
public void showLoginUI(){
// 创建一个窗体 并设置窗体的属性
JFrame jf = new JFrame ();
jf.setTitle ("登录界面");
jf.setSize (400, 500);
jf.setLocationRelativeTo (null);// 居中显示
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
jf.setVisible (true);// 显示到屏幕上
FlowLayout fl = new FlowLayout ();// 流式布局
jf.setLayout (fl);
// 创建一个图标对象
ImageIcon icon = new ImageIcon ("C:\\Users\\ASUS\\IdeaProjects\\User\\src\\img.jpg");
JLabel nameJla = new JLabel ("账号:");
JLabel pwdJla = new JLabel ("密码:");
JTextField nameJtf = new JTextField ();
JPasswordField pwdJpf = new JPasswordField ();
JLabel iconJla = new JLabel (icon);
// 设置尺寸
Dimension dim = new Dimension (320,35);
nameJtf.setPreferredSize (dim);
pwdJpf.setPreferredSize (dim);
JButton loginBtn = new JButton ("登录");
JButton registerBtn = new JButton ("注册");
// 添加组件
jf.add (nameJla);jf.add (nameJtf);
jf.add (pwdJla);jf.add (pwdJpf);
jf.add (loginBtn);
jf.add (registerBtn);
jf.add (iconJla);
jf.setVisible (true);// 可视化
//监听器加载给按钮
ActionListener listener =ul;
loginBtn.addActionListener (listener);
registerBtn.addActionListener (listener);
// 初始化监听器对象中的输入框对象变量名 为了在监听器中使用
ul.nameJtf = nameJtf;
ul.pwdJpf = pwdJpf;
}
public static void main(String[] args){
UserUI ui = new UserUI ();
ui.showLoginUI ();
}
}
UserListener
这个类用于鼠标事件的监听,以达到相应功能的目的
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserListener implements ActionListener {
public int loginCode;
JTextField nameJtf;
JPasswordField pwdJpf;
static final UserService userService = new UserService ();
public void showMainUI(){
// 创建一个窗体 并设置窗体的属性
JFrame jf1 = new JFrame ();
jf1.setTitle ("信息界面");
jf1.setSize (360, 450);
jf1.setLocationRelativeTo (null);// 居中显示
jf1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FlowLayout fl1 = new FlowLayout ();// 流式布局
jf1.setLayout (fl1);
// 创建一些组件并添加到窗体中
JLabel infoLabel = new JLabel("欢迎进入信息界面");
JLabel nameJla1 =new JLabel("昵称:");
JLabel scoreJla =new JLabel("分数:");
JLabel birthJla =new JLabel("生日: ");
JTextField nameJtf1=new JTextField(nameJtf.getText ());//初始化文本框
JTextField scoreJtf1=new JTextField("10");
JTextField birthJtf=new JTextField("2000/01/01");
Dimension dim = new Dimension (310,35);
infoLabel.setPreferredSize (dim);
nameJtf1.setPreferredSize(dim);
nameJtf1.setSelectedTextColor(Color.yellow);
scoreJtf1.setPreferredSize(dim);
scoreJtf1.setEditable(false);//分数不可修改
birthJtf.setPreferredSize(dim);
JButton saveBtn = new JButton("保存修改");
JButton backButton = new JButton("返回登录界面");
jf1.add(infoLabel);
jf1.add(nameJla1);jf1.add(nameJtf1);
jf1.add(birthJla);jf1.add(birthJtf);
jf1.add(scoreJla);jf1.add(scoreJtf1);
jf1.add(saveBtn);
jf1.add(backButton);
jf1.setVisible(true);// 显示到屏幕上
}
@Override
public void actionPerformed(ActionEvent e){
System.out.println ("点击了按钮");
String btnAc = e.getActionCommand ();//响应事件的名称
System.out.println (btnAc);
// 获取输入框中文本
String nameIn = nameJtf.getText ();
String pwdIn = pwdJpf.getText ();
System.out.println (nameIn + " " + pwdIn);
if(btnAc.equals ("登录")){
int loginCode = userService.login (nameIn, pwdIn);
if(loginCode == 0){
// 弹窗
JOptionPane.showMessageDialog (null, "密码错误");
} else if(loginCode == 1){
JOptionPane.showMessageDialog (null, "登录成功");
showMainUI();
} else if(loginCode == 2){
JOptionPane.showMessageDialog (null, "已经登录过了");
} else if(loginCode == 3){
JOptionPane.showMessageDialog (null, "用户名不存在");
}
} else if(btnAc.equals ("注册")){
boolean rcod = userService.register (nameIn, pwdIn);
if(rcod){
JOptionPane.showMessageDialog (null, "注册成功");
}else{
JOptionPane.showMessageDialog (null, "用户名已经被注册");
}
}
if(btnAc.equals("返回登录界面")){
// 清空输入框
//nameJtf.setText("");
//pwdJpf.setText("");
// 关闭当前窗口
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(nameJtf);
frame.dispose();
// 打开登录界面
// UserUI ui = new UserUI();
// ui.showLoginUI();
}
}
}
UserService
UserService用于用户登录背后的逻辑判断
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class UserService {
HashMap<String,User> userMap=new HashMap<>();
String filePath="C:\\Users\\ASUS\\IdeaProjects\\User\\src\\record.txt";
public boolean register(String name,String pwd){
//先判断是否已经注册过了
boolean containsKey=userMap.containsKey(name);
if (containsKey) {
System.out.println("用户名已经被注册");
return false;
}else{
User user =new User(name,pwd);
userMap.put(name,user);
System.out.println("注册成功");
// 将哈希表中的记录写入文本文件
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath,true));
for (Map.Entry<String, User> entry : userMap.entrySet()) {
String line = "账号:"+entry.getKey() + " 密码:" + entry.getValue().getpwd();
writer.write(line);
writer.newLine(); // 写入换行符
}
writer.close();
System.out.println("Records written to file successfully.");
} catch (IOException e) {
System.err.println("Error writing records to file: " + e.getMessage());
}
return true;
}
}
public int login(String name,String pwdIn){
boolean containskey =userMap.containsKey(name);
if(!containskey){
System.out.println("用户名不存在");
return 3;
}else{
User user =userMap.get(name);
int loginCode =user.login(pwdIn);
return loginCode;
}
}
}