前面讲述的内容可以实现基本的登录用户界面,但是监听其中有许多代码是需要封装的,比如判断用户是否存在的代码,弹窗,用户信息以及检查用户是否在线等代码,这些代码进行封装,一方面可以简化监听器中繁琐的判断以及循环语句,另一方面便于维护以及保护初始代码。
下面就对一些代码进行封装。
下面是LoginUI(登录)类:
主类的代码没有什么改动。
import javax.swing.*;
import java.awt.*;
public class LoginUI {
public void showUI(){
JFrame jf = new JFrame("用户登录界面");
jf.setSize(400, 600);
jf.setLocation(500, 200);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flowLayout = new FlowLayout();
jf.setLayout(flowLayout);
ImageIcon icon = new ImageIcon("C:\\Users\\bkf\\IdeaProjects\\SummerTest\\.idea\\img.png");
JLabel label = new JLabel(icon);
JLabel nameJla = new JLabel("账号:");
JLabel pwdJla = new JLabel("密码:");
JTextField nameJtf = new JTextField(34);
JPasswordField pwdJtf = new JPasswordField(34);
JButton loginBtn = new JButton("登录");
JButton regBtn = new JButton("注册");
JCheckBox checkBox1 = new JCheckBox("记住密码");
checkBox1.setBounds(100, 300, 100, 20);
checkBox1.setSelected(false);
checkBox1.setBackground(Color.white);
JCheckBox checkBox2 = new JCheckBox("自动登录");
checkBox2.setBounds(100, 350, 100, 20);
checkBox2.setSelected(false);
checkBox2.setBackground(Color.white);
jf.add(label);
jf.add(nameJla);
jf.add(nameJtf);
jf.add(pwdJla);
jf.add(pwdJtf);
jf.add(loginBtn);
jf.add(regBtn);
jf.add(checkBox1);
jf.add(checkBox2);
jf.setVisible(true);
//创建一个监听器对象
LoginListener loginListen = new LoginListener();
loginBtn.addActionListener(loginListen);
regBtn.addActionListener(loginListen);
loginListen.nameJtf1 = nameJtf;
loginListen.passwordJpf1 = pwdJtf;
}
public static void main(String[] args) {
LoginUI loginUI = new LoginUI();
loginUI.showUI();
}
}
下面是LoginListener(监听器)类:
在监听器中,出现了许多新的类。
不管是登录,注册,更新还是退出,都要首先调用服务类查找一下用户是否存在,密码是否正确。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginListener implements ActionListener {
JTextField nameJtf1;
JPasswordField passwordJpf1;
//注册一个服务类对象 用来处理业务逻辑
UserService userService = new UserService();
// 用户信息更新表单
UserInfoYable userInfoYable;
//登录成功后显示的界面 用户主页
UserUI userUI;
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了");
String nameIn = nameJtf1.getText();
System.out.println("账号:" + nameIn);
String pwdIn = new String(passwordJpf1.getPassword());
System.out.println("密码:" + pwdIn);
// 获取按钮的指令文本
String ac = e.getActionCommand();
System.out.println("按钮的指令文本:" + ac);
if (ac.equals("登录"))
{
//调用服务登录
int stateCode = userService.userLogin(nameIn,pwdIn);
if (stateCode == 1001)
{
showMessage("用户已在线");
}
else if (stateCode == 1002)
{
if (userUI != null)
{
showMessage("请先退出当前账号");
return;
}
showMessage("登陆成功");
//调用新界面
UserUI userUI = new UserUI(this);
userUI.showUI(userService.getUserByName(nameIn));
}
else if (stateCode == 1003)
{
showMessage("密码错误");
}
else if (stateCode == 1004)
{
showMessage("用户不存在");
}
}
else if (ac.equals("注册"))
{
int stateCode = userService.register(nameIn,pwdIn);
if (stateCode == 2001)
{
showMessage("用户已存在");
}
else if (stateCode == 2002)
{
showMessage("注册成功");
}
}
else if (ac.equals("更新"))
{
String name = userInfoYable.getUserName();
String pwd = userInfoYable.getUserPwd();
String phone = userInfoYable.getUserPhone();
String email = userInfoYable.getUserEmail();
String address = userInfoYable.getUserAddress();
int age = userInfoYable.getUserAge();
int score = userInfoYable.getUserScore();
userService.upDate(name,pwd,phone,email,address,age,score);
}
else if (ac.equals("退出")) {
if (userUI != null) {
userUI.hideUI();
}
userService.logOut(userInfoYable.getUserName());
userUI = null;
}
}
public void showMessage(String message)
{
JOptionPane.showMessageDialog(null,message);
}
}
首先创建一个服务类(UserService)对象,用来处理业务逻辑:
里面写了
添加用户类:用于添加用户信息。
通过名字获取用户类:遍历整个用户数组1,查找该用户是否存在。
用户登录类:通过账号密码登录。
用户注册类:查找用户是否存在,若不存在则注册。
用户更新类:查找用户是否存在,若存在则更新。
用户退出类:查找用户是否存在,若存在则退出用户。
/**
* 用户的功能服务类
* 可以实现用户对象的存储 登录,注册,查找,删除,管理等操作
*/
public class UserService {
UserAll[] userList = new UserAll[100];
int count = 0;
public void addUser(UserAll userIn)
{
userList[count] = userIn;
count++;
}
/**
* 根据用户名获取用户对象
* @param nameIn 用户输入的账号
* @return userList[i] 返回已有用户的信息 null 返回空值
*/
public UserAll getUserByName(String nameIn){
for (int i = 0; i < count; i++)
{
if (userList[i].getName().equals(nameIn))
{
return userList[i];
}
}
return null;
}
/**
* 用户登录
* @param nameIn 用户输入的账号
* @param pwdIn 用户输入的密码
* @return 1001 用户已在线 1002 登陆成功 1003 密码错误 1004用户未注册
*/
public int userLogin(String nameIn,String pwdIn){
UserAll user = getUserByName(nameIn);
if (user != null)
{
return user.login(pwdIn);
}else {
return 1004;
}
}
/**
* 用户注册
* @param nameIn 用户输入的账号
* @param pwdIn 用户输入的密码
* @return 2001 用户已注册 2002 注册成功
*/
public int register(String nameIn,String pwdIn){
UserAll user = getUserByName(nameIn);
if(user != null){
System.out.println("用户已注册");
return 2001;
}else{
user = new UserAll(nameIn,pwdIn);
addUser(user);
return 2002;
}
}
/**
* 修改用户信息
* @param name 用户账号
* @param pwd 密码
* @param phone 手机号
* @param email 邮箱
* @param address 地址
* @param age 年龄
* @param score 分数
*/
public void upDate(String name,String pwd,String phone,String email,String address,int age,int score)
{
UserAll u1 = getUserByName(name);
if (u1 != null){
u1.setPassword(pwd);
u1.setPhone(phone);
u1.setEmail(email);
u1.setAddress(address);
u1.setAge(age);
u1.setScore(score);
u1.setLogin(true);
}
else {
System.out.println("用户不存在");
}
}
/**
* 登出
* @param userName 用户账号
*/
public void logOut(String userName){
UserAll u1 = getUserByName(userName);
if (u1 != null)
{
u1.setLogin(false);
}
}
}
再创建一个用户更新表单类(UserInfoYable),用于用户更新内容时调用:
import javax.swing.*;
public class UserInfoYable {
JTextField nameJtf;
JTextField pwdJtf;
JTextField phoneJtf;
JTextField emailJtf;
JTextField addressJtf;
JTextField ageJtf;
JTextField scoreJtf;
JTextField isLoginJtf;
public UserInfoYable(JTextField nameJtf,JTextField pwdJtf,
JTextField phoneJtf,JTextField emailJtf,
JTextField addressJtf,JTextField ageJtf,
JTextField scoreJtf,JTextField isLoginJtf)
{
this.nameJtf = nameJtf;
this.pwdJtf = pwdJtf;
this.phoneJtf = phoneJtf;
this.emailJtf = emailJtf;
this.addressJtf = addressJtf;
this.ageJtf = ageJtf;
this.scoreJtf = scoreJtf;
this.isLoginJtf = isLoginJtf;
}
//实现获取输入框值的方法
public String getUserName()
{
return nameJtf.getText();
}
public String getUserPwd()
{
return pwdJtf.getText();
}
public String getUserPhone()
{
return phoneJtf.getText();
}
public String getUserEmail()
{
return emailJtf.getText();
}
public String getUserAddress()
{
return addressJtf.getText();
}
public int getUserAge()
{
return Integer.parseInt(ageJtf.getText());
}
public int getUserScore()
{
return Integer.parseInt(scoreJtf.getText());
}
}
用户类(UserAll):将用户的所有个人信息进行封装,这样在后面更容易添加其他信息。
public class UserAll {
String name;
String password;
int age;
int score;
boolean isLogin;
String phone;
String email;
String address;
public UserAll()
{
}
public UserAll(String name, String password)
{
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean login) {
isLogin = login;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int login(String pwdIn){
if (pwdIn.equals(password)){
if (isLogin){
System.out.println("用户已登录");
return 1001;
}
isLogin = true;
score += 100;
System.out.println("用户登录成功");
return 1002;
}
else {
System.out.println("密码错误,请重新输入");
return 1003;
}
}
}
用户主页类(UserUI):当用户登陆成功时,会调用此类。
import javax.swing.*;
import java.awt.*;
public class UserUI {
LoginListener loginListen;
JFrame jf;
public UserUI(LoginListener loginListen)
{
this.loginListen = loginListen;
}
public void showUI(UserAll user)
{
jf = new JFrame(user.getName() + "个人主页");//创建窗体
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//点击关闭按钮时的默认操作 - 退出程序
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
jf.setLayout(new FlowLayout());
JLabel nameJla = new JLabel("账号:");
JTextField nameJtf = new JTextField(34);
nameJtf.setText(user.getName());
nameJtf.setEditable(false);//设置为不可编辑
JLabel pwdJla = new JLabel("密码:");
JTextField pwdJtf = new JTextField(34);
pwdJtf.setText(user.getPassword());
JLabel ageJla = new JLabel("年龄:");
JTextField ageJtf = new JTextField(34);
ageJtf.setText(String.valueOf(user.getAge()));
JLabel scoreJla = new JLabel("积分:");
JTextField scoreJtf = new JTextField(34);
scoreJtf.setText(user.getScore() + "");
JLabel phoneJla = new JLabel("手机:");
JTextField phoneJtf = new JTextField(34);
phoneJtf.setText(user.getPhone());
JLabel emailJla = new JLabel("邮箱:");
JTextField emailJtf = new JTextField(34);
emailJtf.setText(user.getEmail());
JLabel addressJla = new JLabel("地址:");
JTextField addressJtf = new JTextField(34);
addressJtf.setText(user.getAddress());
JLabel isLoginJla = new JLabel("是否登录:");
JTextField isLoginJtf = new JTextField(34);
isLoginJtf.setText(user.isLogin() ? "是" : "否");
jf.add(nameJla);
jf.add(nameJtf);
jf.add(pwdJla);
jf.add(pwdJtf);
jf.add(ageJla);
jf.add(ageJtf);
jf.add(scoreJla);
jf.add(scoreJtf);
jf.add(phoneJla);
jf.add(phoneJtf);
jf.add(emailJla);
jf.add(emailJtf);
jf.add(addressJla);
jf.add(addressJtf);
jf.add(isLoginJla);
jf.add(isLoginJtf);
JButton upDateBtn = new JButton("更新");
JButton exitBtn = new JButton("退出");
jf.add(upDateBtn);
jf.add(exitBtn);
upDateBtn.addActionListener(loginListen);
exitBtn.addActionListener(loginListen);
jf.setVisible(true);
UserInfoYable userInfoYable = new UserInfoYable(nameJtf,pwdJtf,
ageJtf,scoreJtf,phoneJtf,emailJtf,addressJtf,isLoginJtf);
loginListen.userInfoYable = userInfoYable;
}
//隐藏窗体
public void hideUI()
{
//隐藏窗体
jf.setVisible(false);
}
}
以上就是比较完善的用户登录页面,希望对程序员们有帮助嗷~