600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 【Swing 开发之图书管理系统】(五)管理员页面布局与用户信息页

【Swing 开发之图书管理系统】(五)管理员页面布局与用户信息页

时间:2020-05-24 03:45:23

相关推荐

【Swing 开发之图书管理系统】(五)管理员页面布局与用户信息页

Swing 开发之图书管理系统(五)管理员页面布局与用户信息页

Swing 开发之图书管理系统(五)管理员页面布局与用户信息页1.管理员页面布局2.用户信息页

Swing 开发之图书管理系统(五)管理员页面布局与用户信息页

系统:Win10

IDE:IntelliJ IDEA .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() {@Overrideprotected 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);// 设置logoUtils.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进入到窗口关闭事件的*/@Overrideprotected 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;}// 获取Button1public JLabel getButton1() {return button1;}// 获取Button2public JLabel getButton2() {return button2;}// 获取Button3public JLabel getButton3() {return button3;}// 获取Button4public JLabel getButton4() {return button4;}// 获取Button5public 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 stubthis.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;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。