Swing 开发之图书管理系统(五)管理员页面布局与用户信息页
Swing 开发之图书管理系统(五)管理员页面布局与用户信息页
系统:Win10
IDE:IntelliJ IDEA 2017.3.7
JDK:1.8.0_121
数据库:MySQL 5.7.17
数据库工具:Navicat Premium 11.2.7
1.管理员页面布局
管理员页面主要由两部分组成
- 一部分是上面头部,由左边的头像、姓名、角色(管理员还是普通成员)和右边的退出按钮组成
- 一部分是由图书借阅、图书归还、用户管理、系统管理、图书管理五部分组成
主页面的代码如下:
/**
* 主界面
*/
@SuppressWarnings("serial")
public class MainView extends JFrame {
private User user;
private JPanel mainWin;
private JLabel status;
private JLabel userName;
private JLabel avatar;
private JLabel titleBack;
private JLabel button1;
private JLabel button2;
private JLabel button3;
private JLabel button4;
private JLabel button5;
private JLabel exit;
/**
* 主界面
* user 登录上来的用户
*/
public MainView(User user) {
this.user = user;
mainWin = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
// 加载背景图片
String bgPath = "images/MainView/background.png";
Image image = new ImageIcon(ClassLoader.getSystemResource(bgPath)/*图片的路径*/).getImage();
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
}
};
userName = new JLabel();
avatar = new JLabel();
titleBack = new JLabel();
button1 = new JLabel("借阅管理");
button2 = new JLabel("图书管理");
// 用户管理
button3 = new JLabel("用户管理");
button4 = new JLabel("系统管理");
button5 = new JLabel("图书管理");
status = new JLabel();
exit = new JLabel();
Init();
}
private void Init() {
// 设置标题
this.setTitle("图书管理系统");
// 设置窗口大小
this.setSize(1102, 679);
// 注册关闭事件
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 窗口居中
CenterView.CenterByWindow(this);
// 设置容器布局方式为空布局
mainWin.setLayout(null);
// 不允许用户调整窗口大小
this.setResizable(false);
// 设置logo
Utils.setLogo(this);
// 设置用户名参数
userName.setBounds(70, 0, 600, 30);
userName.setFont(new Font("微软雅黑", 1, 27));
status.setBounds(70, 40, 100, 30);
status.setFont(new Font("微软雅黑", 3, 17));
// 显示用户名称
userName.setText(user.getName());
// 显示是否管理员
if (user.getAdmin()) {
status.setText("管理员");
} else {
status.setText("普通用户");
button2.setVisible(false);
}
// 设置头像参数
setJLabelImageAndSize(5, 5, 60, 60, avatar, user.getAvatarSrc());
// 创建蚀刻式边框
avatar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
// 退出登录
setJLabelImageAndSize(910, 10, 170, 57, exit, "images/MainView/exit/exit1.png");
// 设置名片背景参数
setJLabelImageAndSize(0, 0, 1098, 80, titleBack, "images/MainView/titleBackground.png");
// 图书借阅
setJLabelImageAndSize(70, 120, 275, 210, button1, "images/MainView/1/default.png");
// 图书归还
setJLabelImageAndSize(70, 350, 275, 210, button2, "images/MainView/2/default.png");
// 用户管理
setJLabelImageAndSize(415, 120, 275, 210, button3, "images/MainView/3/default.png");
// 系统管理
setJLabelImageAndSize(415, 350, 275, 210, button4, "images/MainView/4/default.png");
// 图书管理
setJLabelImageAndSize(760, 120, 275, 440, button5, "images/MainView/5/default.png");
// 各个按钮添加监听
avatar.addMouseListener(new MainView_avatar_MouseListener(this, user));
exit.addMouseListener(new MainView_exit_MouseListener(this, exit));
button1.addMouseListener(new MainView_Button1_MouseListener(this));
button2.addMouseListener(new MainView_Button2_MouseListener(this));
button3.addMouseListener(new MainView_Button3_MouseListener(this));
button4.addMouseListener(new MainView_Button4_MouseListener(this));
button5.addMouseListener(new MainView_Button5_MouseListener(this));
mainWin.add(status);
mainWin.add(avatar);
mainWin.add(userName);
mainWin.add(button1);
mainWin.add(button2);
mainWin.add(button3);
mainWin.add(button4);
mainWin.add(button5);
mainWin.add(exit);
mainWin.add(titleBack);
this.add(mainWin);
//设置窗口可见
this.setVisible(true);
}
public void setJLabelImageAndSize(int x, int y, int width, int height, JLabel label, String imagePath) {
// 实例化ImageIcon对象
ImageIcon image = new ImageIcon(ClassLoader.getSystemResource(imagePath));
// 得到Image对象
Image img = image.getImage();
// 创建缩放版本
img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
// 替换为缩放版本
image.setImage(img);
// JLabel设置图像
label.setIcon(image);
// JLabel设置坐标和大小
label.setBounds(x, y, width, height);
}
/**
* 重写窗口的事件中转方法,程序是从这个方法processWindowEvent进入到窗口关闭事件的
*/
@Override
protected void processWindowEvent(WindowEvent e) {
// 这里需要对进来的WindowEvent进行判断,因为,不仅会有窗口关闭的WindowEvent进来,还可能有其他的WindowEvent进来
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
int option = JOptionPane.showConfirmDialog(null, "是否关闭系统?", "提示", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
// 用户选择关闭程序,以上代码提示后确认传给父类处理
super.processWindowEvent(e);
} else {
//用户选择不退出程序,这里把关闭事件截取
return;
}
} else {
// 如果是其他事件,交给父类处理
super.processWindowEvent(e);
}
}
// 获取头像
public JLabel getAvatar() {
return avatar;
}
// 获取Button1
public JLabel getButton1() {
return button1;
}
// 获取Button2
public JLabel getButton2() {
return button2;
}
// 获取Button3
public JLabel getButton3() {
return button3;
}
// 获取Button4
public JLabel getButton4() {
return button4;
}
// 获取Button5
public JLabel getButton5() {
return button5;
}
public String getPassword() {
return this.user.getPassword();
}
// 获取当前登录用户
public User getUser() {
return this.user;
}
}
2.用户信息页
点击用户头像会弹出一个个人信息的页面,上面是个人的账号和头像,下面的是姓名、性别、学院、电话等信息,然后下面有一个修改密码按钮,点击修改密码按钮会弹出一个修改密码的界面
个人信息页代码
/**
* 个人信息页面
*/
@SuppressWarnings("serial")
public class PersonalInfoView extends JDialog {
private JPanel PersonalWin; // 本窗口
private int WindowsHeight; // 父窗口高度
private int windowsWidth; // 父窗口宽度
private JLabel avatarLabel; // 头像框
private JButton changePwdBtn; // 点击更改密码
private JButton closeBtn; // 关闭
private JLabel nameLabel; // 姓名
private JTextField nameField; // 姓名输入框
private JLabel sexLabel; // 性别
private JPanel sexPanel; // 性别面板
private JRadioButton radioButton1, radioButton2, radioButton3;// 性别单选
private ButtonGroup buttonGroup; // 按钮组
private JLabel collegeLabel; // 学院
private JTextField collegeField; // 学院输入框
private JLabel phoneLabel; // 电话
private JTextField phoneField; // 电话输入框
User user;
MainView mv;
public PersonalInfoView(MainView mv, User user) {
super(mv, "个人信息", true);
this.user = user;
this.mv = mv;
PersonalWin = new JPanel() {
// 定义一张图片,新建一个ImageIcon对象并调用getImage方法获得一个Image对象
String path = "images/PersonalInfoView/background.png";
private Image image = new ImageIcon(ClassLoader.getSystemResource(path)/*图片的路径*/).getImage();
// 这里系统要调用这个paintComponent方法来画这张图片,这里系统传入了一个Graphics对象(画笔),
// 我们需要用这个对象来画背景图片
protected void paintComponent(Graphics g) {
// 调用画笔的drawImage方法,参数是要画的图片,初始坐标,结束坐标,和在哪里画
// this代表是LoginWin这个“画布”对象
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
}
};
WindowsHeight = mv.getHeight();
windowsWidth = mv.getWidth();
// 头像部分
avatarLabel = new JLabel();
// 姓名部分
nameLabel = new JLabel("姓名:");
nameField = new JTextField(user.getName());
nameField.setEnabled(false);
// 性别部分
sexLabel = new JLabel("性别:");
sexPanel = new JPanel();
sexPanel.setLayout(new GridLayout(1, 3));
radioButton1 = new JRadioButton("未知");
radioButton2 = new JRadioButton("男");
radioButton3 = new JRadioButton("女");
buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
buttonGroup.add(radioButton3);
if (user.getSex() == 0) {
radioButton1.setSelected(true);
} else if (user.getSex() == 1) {
radioButton2.setSelected(true);
} else if (user.getSex() == 2) {
radioButton3.setSelected(true);
}
radioButton1.setEnabled(false);
radioButton2.setEnabled(false);
radioButton3.setEnabled(false);
sexPanel.add(radioButton1);
sexPanel.add(radioButton2);
sexPanel.add(radioButton3);
// 学院部分
collegeLabel = new JLabel("学院:");
collegeField = new JTextField(user.getCollege());
collegeField.setEnabled(false);
// 电话部分
phoneLabel = new JLabel("电话:");
phoneField = new JTextField(user.getPhone());
phoneField.setEnabled(false);
// 按钮部分
closeBtn = new JButton("关闭");
changePwdBtn = new JButton("修改密码");
Init();
}
private void Init() {
// TODO Auto-generated method stub
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setSize(windowsWidth - 700, WindowsHeight - 200);
PersonalWin.setLayout(null);
PersonalWin.setFocusable(true);
//不允许用户调整窗口大小
this.setResizable(false);
CenterView.CenterByWindow(this);
// 头像
avatarLabel.setBounds(108, 15, 185, 200);
// 实例化ImageIcon 对象
ImageIcon image = new ImageIcon(ClassLoader.getSystemResource(user.getAvatarSrc()));
// 得到Image对象
Image img = image.getImage();
// 创建缩放版本
img = img.getScaledInstance(165, 165, Image.SCALE_DEFAULT);
// 替换为缩放版本
image.setImage(img);
// JLabel设置图像
avatarLabel.setIcon(image);
// 设置图像居中
avatarLabel.setHorizontalAlignment(JLabel.CENTER);
avatarLabel.setBorder(BorderFactory.createTitledBorder(user.getAccount()));
// 姓名
nameLabel.setFont(new Font("微软雅黑", 1, 17));
nameLabel.setBounds(70, 235, 60, 30);
nameField.setBounds(130, 235, 190, 30);
// 性别
sexLabel.setFont(new Font("微软雅黑", 1, 17));
sexLabel.setBounds(70, 275, 60, 30);
sexPanel.setBounds(130, 275, 190, 30);
// 姓名
collegeLabel.setFont(new Font("微软雅黑", 1, 17));
collegeLabel.setBounds(70, 315, 60, 30);
collegeField.setBounds(130, 315, 190, 30);
// 姓名
phoneLabel.setFont(new Font("微软雅黑", 1, 17));
phoneLabel.setBounds(70, 355, 60, 30);
phoneField.setBounds(130, 355, 190, 30);
// 修改密码按钮
changePwdBtn.setBounds(80, 405, 80, 30);
// 关闭按钮
closeBtn.setBounds(260, 405, 60, 30);
PersonalInfoView_ActionListener listen = new PersonalInfoView_ActionListener(this, mv, user);
changePwdBtn.addActionListener(listen);
closeBtn.addActionListener(listen);
// 添加头像
PersonalWin.add(avatarLabel);
// 添加个人信息
PersonalWin.add(nameLabel);
PersonalWin.add(nameField);
PersonalWin.add(sexLabel);
PersonalWin.add(sexPanel);
PersonalWin.add(collegeLabel);
PersonalWin.add(collegeField);
PersonalWin.add(phoneLabel);
PersonalWin.add(phoneField);
// 添加按钮
PersonalWin.add(changePwdBtn);
PersonalWin.add(closeBtn);
// 去掉按钮文字周围的焦点框
changePwdBtn.setFocusPainted(false);
closeBtn.setFocusPainted(false);
this.add(PersonalWin);
this.setVisible(true);
}
// 获取修改密码按钮
public JButton getChangePwdBtn() {
return changePwdBtn;
}
// 获取关闭按钮
public JButton getCloseBtn() {
return closeBtn;
}
}
弹出的修改密码界面如下,需要输入原密码和新密码,还需要确认新密码,如果两次输入新密码不一致会告警提示,如果旧密码输入错误也会告警提示
修改密码页面的代码
/**
* 个人详情页面的修改密码界面
* 修改自己密码时,needOldPwd为true
*/
@SuppressWarnings("serial")
public class ChangePwdView extends JDialog {
private JPanel changePwdViewPanel;
private JLabel originalPwdLabel; // 原密码
private JLabel newPwdLabel; // 新密码
private JLabel confirmPwdLabel; // 确认密码
private MyPasswordField originalPwd; // 原密码输入框
private MyPasswordField newPwd; // 新密码输入框
private MyPasswordField confirmPwd; // 确认密码输入框
User user;
// 保存按钮
private JButton savePwdBtn; // 保存
// 取消按钮
private JButton closeBtn;
// 是否需要输入旧密码 - 管理员重置其他用户密码不需要旧密码
boolean needOldPwd;
/**
* @param parentView 父级窗口
* @param user 原密码用户
* @param needOldPwd 是否需要旧密码
*/
public ChangePwdView(JDialog parentView, User user, boolean needOldPwd) {
super(parentView, true);
// 实例化一个面板组件
changePwdViewPanel = new JPanel();
this.user = user;
this.needOldPwd = needOldPwd;
originalPwdLabel = new JLabel("原密码:");
newPwdLabel = new JLabel("新密码:");
confirmPwdLabel = new JLabel("确认密码:");
savePwdBtn = new JButton("保存");
closeBtn = new JButton("取消");
originalPwd = new MyPasswordField();
originalPwd.setPlaceholder(" 请输入原密码");
newPwd = new MyPasswordField();
newPwd.setPlaceholder(" 请输入新密码");
confirmPwd = new MyPasswordField();
confirmPwd.setPlaceholder(" 请确认新密码");
// 需要输入旧密码
if (needOldPwd) {
// 旧密码标签与输入框
originalPwdLabel.setFont(new Font("微软雅黑", 0, 17));
originalPwdLabel.setBounds(60, 38, 100, 20);
originalPwd.setBounds(150, 35, 180, 30);
// 如果如要输入旧密码,则将该标签和输入框添加进来,否则不添加
changePwdViewPanel.add(originalPwdLabel);
changePwdViewPanel.add(originalPwd);
// 新密码标签与输入框
newPwdLabel.setFont(new Font("微软雅黑", 0, 17));
newPwdLabel.setBounds(60, 88, 100, 20);
newPwd.setBounds(150, 85, 180, 30);
// 确认新密码标签与输入框
confirmPwdLabel.setFont(new Font("微软雅黑", 0, 17));
confirmPwdLabel.setBounds(60, 138, 100, 20);
confirmPwd.setBounds(150, 135, 180, 30);
// 保存和取消按钮
savePwdBtn.setBounds(60, 200, 80, 30);
closeBtn.setBounds(250, 200, 80, 30);
// 设置大小
this.setSize(400, 300);
} else {
// 不需要输入旧密码
// 新密码标签与输入框
newPwdLabel.setFont(new Font("微软雅黑", 0, 17));
newPwdLabel.setBounds(60, 38, 100, 20);
newPwd.setBounds(150, 35, 180, 30);
// 确认新密码标签与输入框
confirmPwdLabel.setFont(new Font("微软雅黑", 0, 17));
confirmPwdLabel.setBounds(60, 88, 100, 20);
confirmPwd.setBounds(150, 85, 180, 30);
// 保存和取消按钮
savePwdBtn.setBounds(60, 150, 80, 30);
closeBtn.setBounds(250, 150, 80, 30);
// 设置大小
this.setSize(400, 250);
}
changePwdViewPanel.setLayout(null);
changePwdViewPanel.setFocusable(true);
// 添加监听
ChangePwdView_Listener listen = new ChangePwdView_Listener(this, user);
savePwdBtn.addActionListener(listen);
closeBtn.addActionListener(listen);
// 添加其他组件
changePwdViewPanel.add(newPwdLabel);
changePwdViewPanel.add(newPwd);
changePwdViewPanel.add(confirmPwdLabel);
changePwdViewPanel.add(confirmPwd);
changePwdViewPanel.add(savePwdBtn);
changePwdViewPanel.add(closeBtn);
// 去掉按钮文字周围的焦点框
savePwdBtn.setFocusPainted(false);
closeBtn.setFocusPainted(false);
this.add(changePwdViewPanel);
// 设置标题
this.setTitle("修改密码 ");
// 设置默认关闭操作
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// 设置居中显示
CenterView.CenterByWindow(this);
// 设置可见
this.setVisible(true);
}
// 获取旧密码的输入框
public MyPasswordField getOriginalPwd() {
return originalPwd;
}
// 获取新密码的输入框
public MyPasswordField getNewPwd() {
return newPwd;
}
// 获取确认新密码的输入框
public MyPasswordField getConfirmPwd() {
return confirmPwd;
}
// 获取是否需要输入旧密码
public boolean getNeedOldPwd() {
return needOldPwd;
}
// 在按钮监听中获取保存按钮
public JButton getSavePwdBtn() {
return savePwdBtn;
}
// 在按钮监听中获取取消按钮
public JButton getCloseBtn() {
return closeBtn;
}
}