Eighth Java training

Eighth Java training

Last time we created a window interface class, but did not complete it to the last step. Last time I wrote the query window by department

Create a CountStudentsBySexFrame window for counting people by gender

Insert picture description here
The detailed code is as follows:

package net.lyq.student.gui;

import net.lyq.student.service.StudentService;
import net.lyq.student.service.impl.StudentServiceImpl;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;

public class CountStudentsBySexFrame extends JFrame {
    private JPanel panel;
    private JPanel pnlSouth;
    private JPanel pnlCenter;

    private JButton btnPrint;
    private JButton btnExit;

    private JScrollPane scroller; // 滚动面板

    private Vector rows; // 记录行集
    private Vector<String> colHead; // 表格列标题
    private JTable table; // 表格
    private StudentService studentService; // 学生服务对象

    public CountStudentsBySexFrame(String title) {
        super(title);
        initGUI();
    }

    /**
     * 初始化用户界面
     */
    private void initGUI() {
        // 创建组件
        panel = (JPanel) getContentPane();
        pnlCenter = new JPanel();
        pnlSouth = new JPanel();

        rows = new Vector();
        colHead = new Vector();

        btnPrint = new JButton("打印[P]");
        btnPrint.setMnemonic(KeyEvent.VK_P);
        btnExit = new JButton("退出[X]");
        btnExit.setMnemonic(KeyEvent.VK_X);

        // 添加组件
        panel.add(pnlSouth, BorderLayout.SOUTH);
        panel.add(pnlCenter, BorderLayout.CENTER);

        pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
        pnlSouth.add(btnPrint);
        pnlSouth.add(btnExit);
        pnlCenter.setLayout(new BorderLayout());

        TitledBorder tb = new TitledBorder("统计结果");
        pnlCenter.setBorder(tb);

        // 创建学生服务对象
        studentService = new StudentServiceImpl();
        // 获取按性别统计结果记录行集
        rows = studentService.findRowsBySex();
        // 设置表头
        colHead.add("性别");
        colHead.add("人数");

        // 创建表格(参数1:记录集;参数2:表头)
        table = new JTable(rows, colHead);
        scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        pnlCenter.add(scroller, BorderLayout.CENTER);

        repaint(); // 重绘窗体

        if (rows.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
        }

        // 设置窗口大小
        setSize(300, 200);
        // 设置窗口不可调整大小
        setResizable(false);
        // 设置窗口屏幕居中
        setLocationRelativeTo(null);
        // 设置窗口标题
        setTitle("按性别统计学生人数");
        // 设置窗口可见
        setVisible(true);
        // 设置窗口默认关闭操作(卸载当前窗口)
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 【退出】按钮单击事件
        btnExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });

        // 【打印】按钮单击事件
        btnPrint.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    table.print();
                } catch (PrinterException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        new CountStudentsBySexFrame("");
    }
}

operation result:
Insert picture description here

Create CountStudentsByClassFrame

Insert picture description here

package net.lyq.student.gui;

import net.lyq.student.service.StudentService;
import net.lyq.student.service.impl.StudentServiceImpl;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;

public class CountStudentsByClassFrame extends JFrame {
    private JPanel panel;
    private JPanel pnlSouth;
    private JPanel pnlCenter;

    private JButton btnPrint;
    private JButton btnExit;

    private JScrollPane scroller; // 滚动面板

    private Vector rows; // 记录行集
    private Vector<String> colHead; // 表格列标题
    private JTable table; // 表格
    private StudentService studentService; // 学生服务对象·

    public CountStudentsByClassFrame(String title) {
        super(title);
        initGUI();
    }

    private void initGUI() {
        // 创建组件
        panel = (JPanel) getContentPane();
        pnlCenter = new JPanel();
        pnlSouth = new JPanel();

        rows = new Vector();
        colHead = new Vector();

        btnPrint = new JButton("打印");
        btnPrint.setMnemonic(KeyEvent.VK_P);
        btnExit = new JButton("退出");
        btnExit.setMnemonic(KeyEvent.VK_X);

        // 添加组件
        panel.add(pnlSouth, BorderLayout.SOUTH);
        panel.add(pnlCenter, BorderLayout.CENTER);

        pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
        pnlSouth.add(btnPrint);
        pnlSouth.add(btnExit);
        pnlCenter.setLayout(new BorderLayout());

        TitledBorder tb = new TitledBorder("统计结果:");
        pnlCenter.setBorder(tb);

        // 创建学生服务对象
        studentService = new StudentServiceImpl();
        // 获取按性别统计记录行集
        rows = studentService.findRowsByClass();
        // 设置表头
        colHead.add("班级:");
        colHead.add("人数:");
        // 创建表格
        table = new JTable(rows, colHead);
        scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        pnlCenter.add(scroller, BorderLayout.CENTER);

        repaint();

        if (rows.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有记录", "错误提示", JOptionPane.WARNING_MESSAGE);
        }

        setSize(300, 200);

        setResizable(false);

        setLocationRelativeTo(null);

        setTitle("按班级统计人数");

        setVisible(true);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        btnExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });


        btnPrint.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    table.print();
                } catch (PrinterException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        new CountStudentsByClassFrame("");
    }
}





Count the number of people by department CountStudentsByDepartmentFrame

Insert picture description here

package net.lyq.student.gui;

import net.lyq.student.service.StudentService;
import net.lyq.student.service.impl.StudentServiceImpl;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;

/**
 * 功能:按系部统计学生人数
 */
public class CountStudentsByDepartmentFrame extends JFrame {
    private JPanel panel;
    private JPanel pnlSouth;
    private JPanel pnlCenter;

    private JButton btnPrint;
    private JButton btnExit;

    private JScrollPane scroller; // 滚动面板

    private Vector rows; // 记录行集
    private Vector<String> colHead; // 表格列标题
    private JTable table; // 表格
    private StudentService studentService; // 学生服务对象

    public CountStudentsByDepartmentFrame(String title) {
        super(title);
        initGUI();
    }

    /**
     * 初始化用户界面
     */
    private void initGUI() {
        // 创建组件
        panel = (JPanel) getContentPane();
        pnlCenter = new JPanel();
        pnlSouth = new JPanel();

        rows = new Vector();
        colHead = new Vector();

        btnPrint = new JButton("打印[P]");
        btnPrint.setMnemonic(KeyEvent.VK_P);
        btnExit = new JButton("退出[X]");
        btnExit.setMnemonic(KeyEvent.VK_X);

        // 添加组件
        panel.add(pnlSouth, BorderLayout.SOUTH);
        panel.add(pnlCenter, BorderLayout.CENTER);

        pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
        pnlSouth.add(btnPrint);
        pnlSouth.add(btnExit);
        pnlCenter.setLayout(new BorderLayout());

        TitledBorder tb = new TitledBorder("统计结果");
        pnlCenter.setBorder(tb);

        // 创建学生服务对象
        studentService = new StudentServiceImpl();
        // 获取按性别统计结果记录行集
        rows = studentService.findRowsByDepartment();
        // 设置表头
        colHead.add("系部");
        colHead.add("人数");

        // 创建表格(参数1:记录集;参数2:表头)
        table = new JTable(rows, colHead);
        scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        pnlCenter.add(scroller, BorderLayout.CENTER);

        repaint(); // 重绘窗体

        if (rows.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
        }

        // 设置窗口大小
        setSize(300, 200);
        // 设置窗口不可调整大小
        setResizable(false);
        // 设置窗口屏幕居中
        setLocationRelativeTo(null);
        // 设置窗口标题
        setTitle("按系部统计学生人数");
        // 设置窗口可见
        setVisible(true);
        // 设置窗口默认关闭操作(卸载当前窗口)
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 【退出】按钮单击事件
        btnExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });

        // 【打印】按钮单击事件
        btnPrint.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    table.print();
                } catch (PrinterException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        new CountStudentsByDepartmentFrame("");
    }
}

Create a setting status bar information window:

Insert picture description here

package net.lyq.student.gui;

import net.lyq.student.app.Application;
import net.lyq.student.bean.Status;
import net.lyq.student.service.StatusService;
import net.lyq.student.service.impl.StatusServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

/**
 * 功能:设置状态栏信息
 * 作者:李娅侨
 */
public class SetStatusBarFrame extends JFrame {
    private JPanel panel;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;

    private JLabel lblCollege;
    private JLabel lblVersion;
    private JLabel lblAuthor;
    private JLabel lblTelephone;
    private JLabel lblAddress;
    private JLabel lblEmail;

    private JTextField txtCollege;
    private JTextField txtVersion;
    private JTextField txtAuthor;
    private JTextField txtTelephone;
    private JTextField txtAddress;
    private JTextField txtEmail;

    private JButton btnSave;
    private JButton btnExit;

    private StatusService statusService; // 状态服务对象

    public SetStatusBarFrame(String title) {
        super(title);
        initGUI();
    }

    /**
     * 初始化用户界面
     */
    private void initGUI() {
        // 创建组件
        panel = (JPanel) getContentPane();
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
        panel5 = new JPanel();

        lblCollege = new JLabel("校名:");
        lblVersion = new JLabel("版本:");
        lblAuthor = new JLabel("作者:");
        lblTelephone = new JLabel("电话:");
        lblAddress = new JLabel("地址:");
        lblEmail = new JLabel("邮件:");

        txtCollege = new JTextField(12);
        txtVersion = new JTextField(12);
        txtAuthor = new JTextField(12);
        txtTelephone = new JTextField(12);
        txtAddress = new JTextField(29);
        txtEmail = new JTextField(29);

        btnSave = new JButton("保存[S]");
        btnSave.setMnemonic(KeyEvent.VK_S);
        btnExit = new JButton("退出[X]");
        btnExit.setMnemonic(KeyEvent.VK_X);

        // 添加组件
        panel.setLayout(new GridLayout(5, 1));
        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        panel.add(panel4);
        panel.add(panel5);

        panel1.add(lblCollege);
        panel1.add(txtCollege);
        panel1.add(lblVersion);
        panel1.add(txtVersion);

        panel2.add(lblAuthor);
        panel2.add(txtAuthor);
        panel2.add(lblTelephone);
        panel2.add(txtTelephone);

        panel3.add(lblAddress);
        panel3.add(txtAddress);
        panel4.add(lblEmail);
        panel4.add(txtEmail);

        panel5.add(btnSave);
        panel5.add(btnExit);

        // 创建状态服务对象
        statusService = new StatusServiceImpl();
        // 按标识符获取状态对象
        Status status = statusService.findStatusById(1);
        if (status != null) {
            txtCollege.setText(status.getCollege());
            txtVersion.setText(status.getVersion());
            txtAuthor.setText(status.getAuthor());
            txtAddress.setText(status.getAuthor());
            txtTelephone.setText(status.getTelephone());
            txtAddress.setText(status.getAddress());
            txtEmail.setText(status.getEmail());
        }

        // 设置窗口大小
        setSize(450, 350);
        // 设置窗口屏幕居中
        setLocationRelativeTo(null);
        // 设置窗口不可调整大小
        setResizable(false);
        // 设置窗口刚好容纳组件
        pack();
        // 设置窗口标题
        setTitle("设置状态栏信息");
        // 设置窗口可见
        setVisible(true);
        // 设置窗口默认关闭操作(卸载当前窗口)
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 【关闭】按钮事件处理
        btnExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });

        // 【保存】按钮事件处理
        btnSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取状态信息对象
                Status status = statusService.findStatusById(1);
                // 修改属性值
                status.setCollege(txtCollege.getText());
                status.setVersion(txtVersion.getText());
                status.setAuthor(txtAuthor.getText());
                status.setTelephone(txtTelephone.getText());
                status.setAddress(txtAddress.getText());
                status.setEmail(txtEmail.getText());
                // 更新状态记录
                int count = statusService.updateStatus(status);
                // 判断是否更新成功
                if (count > 0) {
                    Application.mainFrame.setStatusBar();
                    Application.mainFrame.setTitle("学生信息管理系统" + status.getVersion());
                }
            }
        });
    }
}

Modify the main interface window:

Insert picture description here

package net.lyq.student.gui;

import net.lyq.student.app.Application;
import net.lyq.student.bean.Status;
import net.lyq.student.service.StatusService;
import net.lyq.student.service.impl.StatusServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * 功能:主界面窗口
 *      通过菜单系统调用各功能模块
 * 作者:李娅侨
 * 日期:2020年07月05日
 */
public class MainFrame extends JFrame {
    private JMenuBar mnbMain; // 主菜单栏

    private JMenu mnuSet; // 设置菜单
    private JMenuItem mniSetCollegeInfo;
    private JMenuItem mniSetStatusBar;
    private JMenuItem mniChangePassword;
    private JMenuItem mniExit;

    private JMenu mnuOperate; // 操作菜单
    private JMenuItem mniAddStudent;
    private JMenuItem mniBrowseStudent;
    private JMenuItem mniEditStudent;

    private JMenu mnuDelStu; // 删除菜单
    private JMenuItem mniDelStudentById;
    private JMenuItem mniDelStudentsByClass;
    private JMenuItem mniDelStudentsByDepartment;

    private JMenu mnuFind; // 查询菜单
    private JMenuItem mniFindStudentById;
    private JMenuItem mniFindStudentsByName;
    private JMenuItem mniFindStudentsByClass;
    private JMenuItem mniFindStudentsByDepartment;

    private JMenu mnuCount; // 统计菜单
    private JMenuItem mniCountStudentsBySex;
    private JMenuItem mniCountStudentsByClass;
    private JMenuItem mniCountStudentsByDepartment;

    private JMenu mnuHelp; // 帮助菜单
    private JMenuItem mniHelp;
    private JMenuItem mniAbout;

    private JPanel panel; // 面板
    private JPanel  pnlCenter;
    private JPanel  pnlSouth;

    private JLabel lblStatusBar; // 状态栏标签
    private JLabel lblBackground; // 背景标签

    private ImageIcon imgCollege;
    private ImageIcon imgExit;
    private ImageIcon imgPassword;
    private ImageIcon imgQuery;
    private ImageIcon imgBrowse;
    private ImageIcon imgCount;
    private ImageIcon imgBackground;

    private JToolBar toolbar; // 工具栏

    private JButton btnSetCollege;
    private JButton btnChangePassword;
    private JButton btnFindStudentById;
    private JButton btnExit;
    private JButton btnBrowseStudent;
    private JButton btnCountByDepartment;

    private Status status; // 状态对象
    private StatusService statusService; // 状态服务对象

    public MainFrame(String title) {
        super(title);
        initGUI();
    }

    /**
     * 初始化图形用户界面
     */
    private void initGUI() {
        // 创建主菜单
        mnbMain = new JMenuBar();

        // 创建【设置】菜单及其菜单项
        mnuSet = new JMenu("系统设置[S]");
        mnuSet.setMnemonic(KeyEvent.VK_S);
        mniSetCollegeInfo = new JMenuItem("学校信息");
        mniSetStatusBar = new JMenuItem("状态栏信息");
        mniChangePassword = new JMenuItem("修改密码");
        mniExit = new JMenuItem("退出系统");

        // 创建【操作】菜单及其菜单项
        mnuOperate = new JMenu("数据操作[O]");
        mnuOperate.setMnemonic(KeyEvent.VK_O);
        mniAddStudent = new JMenuItem("增加学生表记录");
        mnuDelStu = new JMenu("删除学生表记录");
        mniEditStudent = new JMenuItem("编辑学生表记录");
        mniBrowseStudent = new JMenuItem("浏览学生表记录");

        // 创建【删除学生表记录】的子菜单
        mniDelStudentById = new JMenuItem("按学号删除");
        mniDelStudentsByClass = new JMenuItem("按班级删除");
        mniDelStudentsByDepartment = new JMenuItem("按系部删除");

        // 创建【查询】菜单及其菜单项
        mnuFind = new JMenu("查询学生[Q]");
        mnuFind.setMnemonic(KeyEvent.VK_Q);
        mniFindStudentById = new JMenuItem("按学号查询");
        mniFindStudentsByName = new JMenuItem("按姓名查询");
        mniFindStudentsByClass = new JMenuItem("按班级查询");
        mniFindStudentsByDepartment = new JMenuItem("按系部查询");

        // 创建【统计】菜单及其菜单项
        mnuCount = new JMenu("人数统计[C]");
        mnuCount.setMnemonic(KeyEvent.VK_C);
        mniCountStudentsBySex = new JMenuItem("按性别统计");
        mniCountStudentsByClass = new JMenuItem("按班级统计");
        mniCountStudentsByDepartment = new JMenuItem("按系部统计");

        // 创建【帮助】菜单及其菜单项
        mnuHelp = new JMenu("帮助[H]");
        mnuHelp.setMnemonic(KeyEvent.VK_H);
        mniHelp = new JMenuItem("帮助");
        mniAbout = new JMenuItem("关于");

        // 创建图标对象
        imgCollege = new ImageIcon("image/college.png");
        imgPassword = new ImageIcon("image/password.png");
        imgQuery = new ImageIcon("image/query.png");
        imgBrowse = new ImageIcon("image/browse.png");
        imgCount = new ImageIcon("image/count.png");
        imgExit = new ImageIcon("image/exit.png");

        // 创建工具栏
        toolbar = new JToolBar();
        btnSetCollege = new JButton("设置学校", imgCollege);
        btnSetCollege.setToolTipText("设置学校信息");
        btnSetCollege.setVerticalTextPosition(AbstractButton.BOTTOM);
        btnSetCollege.setHorizontalTextPosition(AbstractButton.CENTER);
        btnChangePassword = new JButton("修改密码", imgPassword);
        btnChangePassword.setToolTipText("修改用户密码");
        btnChangePassword.setVerticalTextPosition(AbstractButton.BOTTOM);
        btnChangePassword.setHorizontalTextPosition(AbstractButton.CENTER);
        btnBrowseStudent = new JButton("浏览学生", imgBrowse);
        btnBrowseStudent.setToolTipText("浏览学生记录");
        btnBrowseStudent.setVerticalTextPosition(AbstractButton.BOTTOM);
        btnBrowseStudent.setHorizontalTextPosition(AbstractButton.CENTER);
        btnFindStudentById = new JButton("查询学生", imgQuery);
        btnFindStudentById.setToolTipText("按学号查询学生记录");
        btnFindStudentById.setVerticalTextPosition(AbstractButton.BOTTOM);
        btnFindStudentById.setHorizontalTextPosition(AbstractButton.CENTER);
        btnCountByDepartment = new JButton("统计人数", imgCount);
        btnCountByDepartment.setToolTipText("按系部统计学生人数");
        btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
        btnCountByDepartment.setHorizontalTextPosition(AbstractButton.CENTER);
        btnExit = new JButton("退出系统", imgExit);
        btnExit.setToolTipText("退出系统");
        btnExit.setVerticalTextPosition(AbstractButton.BOTTOM);
        btnExit.setHorizontalTextPosition(AbstractButton.CENTER);
        toolbar.add(btnSetCollege);
        toolbar.add(btnChangePassword);
        toolbar.add(btnBrowseStudent);
        toolbar.add(btnFindStudentById);
        toolbar.add(btnCountByDepartment);
        toolbar.add(btnExit);

        // 创建面板
        panel = (JPanel) getContentPane();
        pnlCenter = new JPanel();
        pnlSouth = new JPanel();
        pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));

        // 创建背景图片
        imgBackground = new ImageIcon("images/background.jpg");
        // 创建背景标签
        lblBackground = new JLabel(imgBackground);
        // 创建状态栏标签
        lblStatusBar = new JLabel();

        // 设置菜单栏
        setJMenuBar(mnbMain);
        // 添加【设置】菜单
        mnbMain.add(mnuSet);
        mnuSet.add(mniSetCollegeInfo);
        mnuSet.add(mniSetStatusBar);
        mnuSet.add(mniChangePassword);
        mnuSet.addSeparator();
        mnuSet.add(mniExit);

        // 添加【删除学生表记录】菜单
        mnuDelStu.add(mniDelStudentById);
        mnuDelStu.add(mniDelStudentsByClass);
        mnuDelStu.add(mniDelStudentsByDepartment);

        // 添加【操作】菜单
        mnbMain.add(mnuOperate);
        mnuOperate.add(mniAddStudent);
        mnuOperate.add(mniEditStudent);
        mnuOperate.add(mnuDelStu);
        mnuOperate.add(mniBrowseStudent);

        // 添加【查询】菜单
        mnbMain.add(mnuFind);
        mnuFind.add(mniFindStudentById);
        mnuFind.add(mniFindStudentsByName);
        mnuFind.add(mniFindStudentsByClass);
        mnuFind.add(mniFindStudentsByDepartment);

        // 添加【统计】菜单
        mnbMain.add(mnuCount);
        mnuCount.add(mniCountStudentsBySex);
        mnuCount.add(mniCountStudentsByClass);
        mnuCount.add(mniCountStudentsByDepartment);

        // 添加【帮助】菜单
        mnbMain.add(mnuHelp);
        mnuHelp.add(mniHelp);
        mnuHelp.add(mniAbout);

        // 添加面板
        panel.setLayout(new BorderLayout());
        panel.add(toolbar, "North");
        panel.add(pnlCenter, "Center");
        panel.add(pnlSouth, "South");
        pnlCenter.add(lblBackground);
        pnlSouth.add(lblStatusBar);

        // 非管理员不能设置状态栏
        if (!Application.username.equals("admin")) {
            mniSetStatusBar.setEnabled(false);
            mniAddStudent.setEnabled(false);
            mnuDelStu.setEnabled(false);
            mniEditStudent.setEnabled(false);
        }

        // 设置状态栏信息
        setStatusBar();

        // 创建状态服务对象
        statusService = new StatusServiceImpl();
        // 获取状态对象
        status = statusService.findStatusById(1);

        // 设置窗口尺寸
        setSize(800, 640);
        // 设置窗口可见
        setVisible(true);
        // 设置窗口屏幕居中
        setLocationRelativeTo(null);
        // 设置窗口标题
        setTitle("学生信息管理系统" + status.getVersion());

        // 注册窗口监听器,继承窗口适配器,编写事件处理方法
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                exitSystem();
            }
        });

        // 设置菜单
        // 【设置学校信息】菜单项单击事件
        mniSetCollegeInfo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new SetCollegeInfoFrame("");
            }
        });

        // 【设置状态栏信息】菜单项单击事件
        mniSetStatusBar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new SetStatusBarFrame("");
            }
        });

        // 【修改密码】菜单项单击事件
        mniChangePassword.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ChangePasswordFrame("");
            }
        });

        // 【退出系统】菜单项单击事件
        mniExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exitSystem();
            }
        });

        // 查询菜单
        // 【按学号查询】菜单项单击事件
        mniFindStudentById.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FindStudentByIdFrame("");
            }
        });

        // 【按姓名查询】菜单项单击事件
        mniFindStudentsByName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FindStudentsByNameFrame("");
            }
        });

        // 【按班级查询】菜单项单击事件
        mniFindStudentsByClass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FindStudentsByClassFrame("");
            }
        });

        // 【按系部查询】菜单项单击事件
        mniFindStudentsByDepartment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FindStudentsByDepartmentFrame("");
            }
        });

        // 统计菜单
        // 【按性别统计人数】菜单项单击事件
        mniCountStudentsBySex.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new CountStudentsBySexFrame("");
            }
        });

        // 【按班级统计人数】菜单项单击事件
        mniCountStudentsByClass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new CountStudentsByClassFrame("");
            }
        });

        // 【按系部统计人数】菜单项单击事件
        mniCountStudentsByDepartment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new CountStudentsByDepartmentFrame("");
            }
        });

        // 【增加学生记录】菜单项单击事件
        mniAddStudent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new AddStudentFrame("");
            }
        });

        // 【按学号删除学生记录】菜单项单击事件
        mniDelStudentById.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new DeleteStudentByIdFrame("");
            }
        });

        // 【按班级删除学生记录】菜单项单击事件
        mniDelStudentsByClass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new DeleteStudentsByClassFrame("");
            }
        });

        // 【按系部删除学生记录】菜单项单击事件
        mniDelStudentsByDepartment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new DeleteStudentsByDepartmentFrame("");
            }
        });

        // 【编辑学生记录】菜单项单击事件
        mniEditStudent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new net.lyq.student.gui.EditStudentFrame("");
            }
        });

        // 【浏览学生记录】菜单项单击事件
        mniBrowseStudent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new BrowseStudentsFrame("");
            }
        });

        // 【帮助】菜单单击事件
        mniHelp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Runtime.getRuntime().exec("cmd /c start help/帮助文档.chm");
                } catch (IOException e1) {
                    JOptionPane.showMessageDialog(null, e1.getMessage(), "学生信息管理系统", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 【关于】菜单单击事件
        mniAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "开发人员:" + status.getCollege() + "_" + status.getAuthor() + "\n联系电话:" + status.getTelephone()
                                + "\n电子邮箱:" + status.getEmail(),
                        "学生信息管理系统" + status.getVersion(), JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 工具栏按钮单击事件
        // 【设置学校信息】按钮
        btnSetCollege.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new SetCollegeInfoFrame("");
            }
        });

        // 【修改密码】按钮
        btnChangePassword.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ChangePasswordFrame("");
            }
        });

        // 【浏览】按钮
        btnBrowseStudent.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new BrowseStudentsFrame("");
            }
        });

        // 【查询】按钮
        btnFindStudentById.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new FindStudentByIdFrame("");
            }
        });

        // 【统计】按钮
        btnCountByDepartment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new CountStudentsByDepartmentFrame("");
            }
        });

        // 【退出】按钮
        btnExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exitSystem();
            }
        });
    }

    /**
     * 退出系统(询问用户是否要退出)
     */
    private void exitSystem() {
        int choice = JOptionPane.showConfirmDialog(this,
                "您是否要退出系统?","学生信息管理系统", JOptionPane.YES_NO_OPTION);
        if (choice == JOptionPane.YES_OPTION) {
            System.exit(0);
        } else {
            // 卸载当前窗口
            dispose();
            // 重新显示主窗口
            Application.mainFrame = new MainFrame("学生信息管理系统" + status.getVersion());
        }
    }

    /**
     * 设置状态栏信息
     */
    public void setStatusBar() {
        // 创建状态服务对象
        statusService = new StatusServiceImpl();
        // 获取状态栏对象
        status = statusService.findStatusById(1);
        // 设置状态栏标签
        lblStatusBar.setText(
                status.getCollege() + "学生信息管理系统" + status.getVersion() + "      作者:" + status.getAuthor() + "      地址:"
                        + status.getAddress() + "      电话:" + status.getTelephone() + "      邮箱:" + status.getEmail());
    }

    public static void main(String[] args) {
        Application.mainFrame = new MainFrame("");
    }
}

After modifying the code, run the login interface to complete all operations.

Guess you like

Origin blog.csdn.net/weixin_46705517/article/details/107310112