用户信息管理系统2
这是对于上一条博客的补充,将展示用户信息界面的函数showmainUI()函数创建一个新的类——MainMsgUI类并添加管理员操作
相较于上次的blog–传送门,这次我添加了管理员进入按钮,从哈希表中读取存入的用户,再遍历用户昵称,在管理员管理界面针对每一个用户昵称生成相应的按钮,再调用MainMsgUI的函数来展示对应用户名字(昵称,也可以认为是账号)的信息界面进行修改
总的项目代码上传至github自取——用户信息管理系统代码
运行效果图如下:
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 ();
JTextField nameJtf1;
JTextField scoreJtf1;
JTextField birthJtf;
String mainUserName;
MainMsgUI mainMsgUI = new MainMsgUI ();
//MainMsgUI adminUI = new MainMsgUI();暂时认为不需要定义成用户信息类
public void initMainMsgUI(User user,UserListener userListener){//初始化用户的信息界面
mainMsgUI.showMainUI (user,userListener);
this.nameJtf1=mainMsgUI.nameJtf1;
this.scoreJtf1=mainMsgUI.scoreJtf1;
this.birthJtf=mainMsgUI.birthJtf;
this.mainUserName=mainMsgUI.mainUserName;
}
//展示管理员登录界面
public void adminUI(String[] allUsers){
JFrame jf2 = new JFrame ();
jf2.setTitle ("管理员信息界面");
jf2.setSize (360, 450);
jf2.setLocationRelativeTo (null);
jf2.setDefaultCloseOperation (JFrame.HIDE_ON_CLOSE);
FlowLayout fl =new FlowLayout();
jf2.setLayout(fl);
for(String i: allUsers ){
JButton userBtn =new JButton(i);
jf2.add(userBtn);
userBtn.addActionListener(this);
}
jf2.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){
System.out.println ("点击了按钮");
String btnAc = e.getActionCommand ();//响应事件的名称
System.out.println (btnAc);
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor (nameJtf);
// 获取输入框中文本
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, "登录成功");
initMainMsgUI(userService.getUserByName (nameIn),this);
frame.setVisible (false);
} 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 ("保存修改")){
String newUserName = nameJtf1.getText ();
String newBir = birthJtf.getText ();
userService.upDate (mainUserName, newUserName, newBir);
mainUserName = newUserName;
JOptionPane.showMessageDialog (null, "修改成功");
initMainMsgUI(userService.getUserByName (mainUserName),this);
}
if(btnAc.equals ("返回登录界面")){
// 关闭当前窗口
User user = userService.getUserByName (mainUserName);
user.logout ();
mainMsgUI.jf1.setVisible (false);
frame.setVisible (true);
}
if(btnAc.equals("管理员登录")){
adminUI(userService.getAllUsers());
}
for(String i: userService.getAllUsers() ){
if(btnAc.equals(i)){
JButton btn = (JButton)e.getSource();
String userName= btn.getText();
User user = userService.getUserByName(userName);
System.out.println(userName);
initMainMsgUI(user,this);
}
}
}
}
MainMsgUI
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
public class MainMsgUI{
JFrame jf1 = new JFrame ();// 窗体对象唯一
JTextField nameJtf1;
JTextField scoreJtf1;
JTextField birthJtf;
String mainUserName;
public void showMainUI(User user,UserListener userListener){
// 清除组件
jf1.getContentPane ().removeAll ();
mainUserName = user.getName ();
// 创建一个窗体 并设置窗体的属性
jf1.setTitle (mainUserName + "信息界面");
jf1.setSize (360, 450);
jf1.setLocationRelativeTo (null);// 居中显示
jf1.setDefaultCloseOperation (JFrame.HIDE_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 ("生日: ");
nameJtf1 = new JTextField (user.getName ());//初始化文本框
scoreJtf1 = new JTextField ("10");
birthJtf = new JTextField (user.getBirth ());
Dimension dim = new Dimension (310, 35);
infoLabel.setPreferredSize (dim);// Preferred Size 首选尺寸
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);// 显示到屏幕上
saveBtn.addActionListener (userListener);
backButton.addActionListener (userListener);
}
}