[Java] Student performance management system (graphical interface to implement related functions)

Foreword:


The entire production process from function code implementation to interface display is all implemented in Java language.

1. Environment setup 


        1. Create a project file in the idea, create a model module under the project file, download and create three packages in the model module to store the (BackEndCode) back-end code package, (MainExe) main program package, and (WebCode) interface. Implement the code package and create the files as shown below in the three packages

        2. Container selection: Because this time I am using the Student class I created and the amount of information is uncertain, I choose a collection as the container of the Student class.

2. Function realization


1. Implementation of student information class (BackEndCode package)

        In order to prevent attributes from being arbitrarily accessed by external classes, private is used to modify the attributes in the student class and create set and get methods to facilitate calling the attributes, and then create a print data method to facilitate data printing. The code is as follows:

package BackEndCode;

import java.io.Serializable;

public class Student implements Serializable {

    private String StuName;
    private int id;
    private int ChineseScores;
    private int MathScores;
    private int EnglishScores;

    public Student() {
    }

    public Student(String stuName, int id, int chineseScores, int mathScores, int englishScores) {
        StuName = stuName;
        this.id = id;
        ChineseScores = chineseScores;
        MathScores = mathScores;
        EnglishScores = englishScores;
    }

    public String getStuName() {
        return StuName;
    }

    public int getId() {
        return id;
    }

    public int getChineseScores() {
        return ChineseScores;
    }

    public int getMathScores() {
        return MathScores;
    }

    public int getEnglishScores() {
        return EnglishScores;
    }
    
    public int getSum() {
        return ChineseScores + MathScores + EnglishScores;
    }

    @Override
    public String toString() {
        return id + " " + StuName + " " + ChineseScores + " " +
                MathScores + " " + EnglishScores + " " + getSum();
    }
}

 2. Student information writing and reading function class (BackEndCode package)

        Here, object serialization flow and object deserialization flow are used to realize the reading and writing functions of writing student information into text files and reading data from text files. The code is as follows:

package BackEndCode;

import java.io.*;
import java.util.ArrayList;

public class StuTest {
    //序列化流
    public static void Output(ArrayList<Student> arr){
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Students.txt"));
            oos.writeObject(arr);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //反序列化
    @SuppressWarnings("unchecked")
    public static ArrayList<Student> Input(){
        File file = new File("Students.txt");
        ArrayList<Student> arr = new ArrayList<>();
        if (!file.exists()) {
            return arr;
        }
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            Object obj = ois.readObject();
            arr = (ArrayList<Student>) obj;
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException c) {
            c.getException();
        }
        return arr;
    }
}

3. Student performance sorting class (BackEndCode package)

Users can sort in ascending and descending order by selecting Chinese scores, math scores, English scores, or total scores. The code is as follows:

package BackEndCode;

import java.util.ArrayList;
import java.util.Comparator;

public class ComparatorScores {

    //  按语文成绩降序
    public static void ComparatorChineseScoresD(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getChineseScores() - o1.getChineseScores();
            }
        };
        arr.sort(comparator);
    }


    //  按数学成绩降序
    public static void ComparatorMathScoresD(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getMathScores() - o1.getMathScores();
            }
        };
        arr.sort(comparator);
    }


    //  按英语成绩降序
    public static void ComparatorEnglishScoresD(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getEnglishScores() - o1.getEnglishScores();
            }
        };
        arr.sort(comparator);
    }

    //  按总分降序
    public static void ComparatorSumScoresD(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getSum() - o1.getSum();
            }
        };
        arr.sort(comparator);
    }


    //  按语文成绩升序
    public static void ComparatorChineseScoresR(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getChineseScores() - o2.getChineseScores();
            }
        };
        arr.sort(comparator);
    }


    //  按数学成绩升序
    public static void ComparatorMathScoresR(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getMathScores() - o2.getMathScores();
            }
        };
        arr.sort(comparator);
    }


    //  按英语成绩升序
    public static void ComparatorEnglishScoresR(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getEnglishScores() - o2.getEnglishScores();
            }
        };
        arr.sort(comparator);
    }

    //  按总分升序
    public static void ComparatorSumScoresR(ArrayList<Student> arr) {
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getSum() - o2.getSum();
            }
        };
        arr.sort(comparator);
    }

}

4. Main interface (WebCode package) 

The rendering of the main interface is as follows: (You can pull down to select the sorting method of results)

 

 

 

The actual code of the main interface is as follows (composed of multiple functions, slightly more code): 

package WebCode;

import BackEndCode.ComparatorScores;
import BackEndCode.StuTest;
import BackEndCode.Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

public class MainInterface extends JFrame implements ActionListener {
    public static void main(String[] args) throws IOException {
        new MainInterface();
    }

    //面板控件
    private JLabel queryLab;
    private JTextField queryTxt;
    private JButton queryBtn;
    private JButton allBtn;
    private JComboBox<String> CompareSel;
    private JPanel top;

    private JTable resultTb;
    private JScrollPane jsp;
    DefaultTableModel tableModel;

    private JButton addBtn;
    private JButton deleteBtn;
    private JButton updateBtn;
    private JButton saveBtn;
    private JPanel bottom;

    ArrayList<Student> arr;

    //构造函数
    public MainInterface() throws IOException {
        super("学生成绩管理系统");

        arr = StuTest.Input();
        //顶部查询栏
        queryLab = new JLabel("请输入学号");
        queryTxt = new JTextField(10);
        queryBtn = new JButton("查询");
        allBtn = new JButton("全部");
        CompareSel = new JComboBox<>();
        CompareSel.addItem("语文成绩升序");
        CompareSel.addItem("语文成绩降序");
        CompareSel.addItem("数学成绩升序");
        CompareSel.addItem("数学成绩降序");
        CompareSel.addItem("英语成绩升序");
        CompareSel.addItem("英语成绩降序");
        CompareSel.addItem("总分升序");
        CompareSel.addItem("总分降序");
        top = new JPanel();
        top.add(queryLab);
        top.add(queryTxt);
        top.add(queryBtn);
        top.add(allBtn);
        top.add(CompareSel);

        //底部增删改栏
        addBtn = new JButton("增加");
        deleteBtn = new JButton("删除");
        updateBtn = new JButton("修改");
        saveBtn = new JButton("保存");
        bottom = new JPanel();
        bottom.add(addBtn);
        bottom.add(deleteBtn);
        bottom.add(updateBtn);
        bottom.add(saveBtn);


        //顶部按钮添加监听
        queryBtn.addActionListener(this);
        queryBtn.setActionCommand("query");
        allBtn.addActionListener(this);
        allBtn.setActionCommand("all");

        //底部按钮添加监听
        addBtn.addActionListener(this);
        addBtn.setActionCommand("add");
        deleteBtn.addActionListener(this);
        deleteBtn.setActionCommand("delete");
        updateBtn.addActionListener(this);
        updateBtn.setActionCommand("update");
        saveBtn.addActionListener(this);
        saveBtn.setActionCommand("save");

        //中间Table
        resultTb = new JTable();
        tableModel = (DefaultTableModel) resultTb.getModel();
        tableModel.addColumn("学号");
        tableModel.addColumn("学生姓名");
        tableModel.addColumn("语文成绩");
        tableModel.addColumn("数学成绩");
        tableModel.addColumn("英语成绩");
        tableModel.addColumn("总分");

        jsp = new JScrollPane(resultTb);


        //构建整体布局
        this.add(top, BorderLayout.NORTH);
        this.add(jsp, BorderLayout.CENTER);
        this.add(bottom, BorderLayout.SOUTH);

        //设置窗口属性
        this.setBounds(500, 250, 500, 300);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        switch (s) {
            case "query":
                //查询
                String text = queryTxt.getText();
                int id;
                try {
                    id = Integer.parseInt(text);
                } catch (NumberFormatException ignore) {
                    JOptionPane.showMessageDialog(this, "请输入正确的学号!!!");
                    return;
                }
                Student inquireStu = getStu(id);
                if (inquireStu == null) {
                    JOptionPane.showMessageDialog(this, "未查询到该学生");
                    return;
                } else {
                    Object[] a = inquireStu.toString().split(" ");
                    tableModel.getDataVector().clear();
                    tableModel.addRow(a);
                }
                break;
            case "all":
                tableModel.getDataVector().clear();
                tableModel.fireTableDataChanged();
                //显示全部
                if (arr.size() == 0) {
                    JOptionPane.showMessageDialog(this, "未查询到信息");
                    return;
                }

                String selectedItem = (String) CompareSel.getSelectedItem();
                SetSort(arr,selectedItem);

                for (Student allStu : arr) {
                    Object[] a = allStu.toString().split(" ");
                    tableModel.addRow(a);
                }
                break;
            case "add":
                //添加
                AddStuInterface addStuInterface = new AddStuInterface(this, "添加学生信息", true, arr);
                break;
            case "delete":
                //删除
                new DeleteStuInterface(this, "删除学生信息", true, arr);
                break;
            case "update":
                //修改
                new UpdateStuInterface(this, "修改学生信息", true, arr);
                break;
            case "save":
                StuTest.Output(arr);
                JOptionPane.showMessageDialog(this, "保存成功");
        }
    }

    //遍历集合
    private Student getStu(int id) {
        for (Student s : arr) {
            if (s.getId() == id) {
                return s;
            }
        }
        return null;
    }

    //排序
    private void SetSort(ArrayList<Student> arr, String s) {
        switch (s) {
            case "语文成绩升序":
                ComparatorScores.ComparatorChineseScoresR(arr);
                break;
            case "语文成绩降序":
                ComparatorScores.ComparatorChineseScoresD(arr);
                break;
            case "数学成绩升序":
                ComparatorScores.ComparatorMathScoresR(arr);
                break;
            case "数学成绩降序":
                ComparatorScores.ComparatorMathScoresD(arr);
                break;
            case "英语成绩升序":
                ComparatorScores.ComparatorEnglishScoresR(arr);
                break;
            case "英语成绩降序":
                ComparatorScores.ComparatorEnglishScoresD(arr);
                break;
            case "总分升序":
                ComparatorScores.ComparatorSumScoresR(arr);
                break;
            case "总分降序":
                ComparatorScores.ComparatorSumScoresD(arr);
                break;
        }
    }
}

 

5. Add interface (WebCode package)

The rendering of the added interface is as follows:

 

The interface implementation code is as follows: 

package WebCode;

import BackEndCode.Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class AddStuInterface extends JDialog implements ActionListener {

    //面板控件
    //左边标题栏
    private JLabel idLab,nameLab,CSLab,MSLab,ESLab;

    //右边信息选择填写栏
    private JTextField idTxt,nameTxt,CSTxt,MSTxt,ESTxt;

    //添加和取消按键
    private JButton addBtn, cancelBtn;

    //布局控件
    private JPanel left,center, bottom;

    ArrayList<Student> narr;

    //构造函数
    public AddStuInterface(Frame owner, String title, boolean modal, ArrayList<Student> arr) {
        super(owner, title, modal);
        narr = arr;
        //左边标签栏
        idLab = new JLabel("学号:");
        nameLab = new JLabel("姓名:");
        CSLab = new JLabel("语文成绩:");
        MSLab = new JLabel("数学成绩:");
        ESLab = new JLabel("英语成绩:");
        //右边信息填写栏
        idTxt = new JTextField();
        nameTxt = new JTextField();
        CSTxt = new JTextField();
        MSTxt = new JTextField();
        ESTxt = new JTextField();
        //添加和取消按键
        addBtn = new JButton("添加");
        cancelBtn = new JButton("取消");

        //设置监听
        addBtn.addActionListener(this);
        addBtn.setActionCommand("add");
        cancelBtn.addActionListener(this);
        cancelBtn.setActionCommand("cancel");
        //创建布局
        //创建左边栏
        left = new JPanel();
        left.setLayout(new GridLayout(5, 1));
        left.add(idLab);
        left.add(nameLab);
        left.add(CSLab);
        left.add(MSLab);
        left.add(ESLab);
        //创建右边栏
        center = new JPanel();
        center.setLayout(new GridLayout(5, 1));
        center.add(idTxt);
        center.add(nameTxt);
        center.add(CSTxt);
        center.add(MSTxt);
        center.add(ESTxt);
        //底部添加和取消按键
        bottom = new JPanel();
        bottom.add(addBtn);
        bottom.add(cancelBtn);
        //整体布局
        this.add(left, BorderLayout.WEST);
        this.add(center, BorderLayout.CENTER);
        this.add(bottom, BorderLayout.SOUTH);


        //设置窗口属性
        this.setSize(300, 200);
        this.setResizable(false);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if (s.equals("cancel")) {
            this.setVisible(false);
        } else if (s.equals("add")) {
            int id;
            try {
                id = Integer.parseInt(idTxt.getText());
            } catch (NumberFormatException ignored) {
                JOptionPane.showMessageDialog(this, "请输入正确的学号!!!");
                return;
            }
            for (Student student : narr) {
                if (id == student.getId()) {
                    JOptionPane.showMessageDialog(this, "该学号已存在!!!");
                    return;
                }
            }
            String name = nameTxt.getText();
            if (name.equals("")) {
                JOptionPane.showMessageDialog(this, "姓名不能为空!!!");
                return;
            }
            int CS = InputScores(CSTxt.getText());
            int MS = InputScores(MSTxt.getText());
            int ES = InputScores(ESTxt.getText());
            Student addStudent = new Student(name, id, CS, MS, ES);
            narr.add(addStudent);
            JOptionPane.showMessageDialog(this, "添加成功");
            this.setVisible(false);
        }
    }

    //成绩输入
    public int InputScores(String s) {
        int score = 0;
        try {
            score = Integer.parseInt(s);
        } catch (NumberFormatException ignored) {}
        return score;
    }
}

 6. Delete interface (WebCode package)

The deletion interface rendering is as follows:

 

The deletion interface code is implemented as follows: 

package WebCode;

import BackEndCode.Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class DeleteStuInterface extends JDialog implements ActionListener {

    //面板控件
    //顶部提示标签
    private JLabel topLab;
    //左边标签栏
    private JLabel idLab;
    //右边填写栏
    private JTextField idTxt;
    //底部删除和取消按键
    private JButton deleteBtn;
    private JButton cancelBtn;


    private JPanel top;
    private JPanel left;
    private JPanel center;
    private JPanel bottom;

    ArrayList<Student> narr;

    //构造函数

    public DeleteStuInterface(Frame owner, String title, boolean modal, ArrayList<Student> arr) {
        super(owner, title, modal);
        narr = arr;

        //顶部提示标签
        topLab = new JLabel("请输入要删除学生的学号");
        top = new JPanel();
        top.add(topLab);

        //中间信息栏
        idLab = new JLabel("学号:");
        idTxt = new JTextField();
        left = new JPanel();
        left.setLayout(new GridLayout(1, 1));
        center = new JPanel();
        center.setLayout(new GridLayout(1, 1));
        left.add(idLab);
        center.add(idTxt);

        //底部删除键和取消键
        deleteBtn = new JButton("删除");
        cancelBtn = new JButton("取消");
        //按键设置监听
        deleteBtn.addActionListener(this);
        deleteBtn.setActionCommand("删除");
        cancelBtn.addActionListener(this);
        cancelBtn.setActionCommand("取消");

        bottom = new JPanel();
        bottom.add(deleteBtn);
        bottom.add(cancelBtn);

        //整体布局
        this.add(top, BorderLayout.NORTH);
        this.add(left, BorderLayout.WEST);
        this.add(center, BorderLayout.CENTER);
        this.add(bottom, BorderLayout.SOUTH);

        //设置窗口属性
        this.setSize(300, 130);
        this.setResizable(false);
        this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if (s.equals("删除")) {
            int i;
            try {
                i = Integer.parseInt(idTxt.getText());
            } catch (NumberFormatException ignored) {
                JOptionPane.showMessageDialog(this, "请输入正确的学号!!!");
                return;
            }
            for (int j = 0; j < narr.size(); j++) {
                if (i == narr.get(j).getId()) {
                    narr.remove(j);
                    JOptionPane.showMessageDialog(this, "删除成功!!!");
                    this.setVisible(false);
                    return;
                }
            }
            JOptionPane.showMessageDialog(this, "该学号不存在!!!");
        } else if (s.equals("取消")) {
            this.setVisible(false);
        }
    }
}

 7. Modify the interface (WebCode package)

The modified interface rendering is as follows:

 

 

Modify the interface code as follows: 

package WebCode;

import BackEndCode.Student;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class UpdateStuInterface extends JDialog implements ActionListener {
    //面板控件
    //左边标题栏
    private JLabel idLab, nameLab, CSLab, MSLab, ESLab;
    //右边信息选择填写栏
    private JTextField idTxt, nameTxt, CSTxt, MSTxt, ESTxt;
    //添加和取消按键
    private JButton updateBtn, cancelBtn;
    //布局控件
    private JPanel left, center, bottom;

    ArrayList<Student> narr;

    //构造函数
    public UpdateStuInterface(Frame owner, String title, boolean modal, ArrayList<Student> arr) {
        super(owner, title, modal);
        narr = arr;
        //左边标签栏
        idLab = new JLabel("学号:");
        nameLab = new JLabel("姓名:");
        CSLab = new JLabel("语文成绩:");
        MSLab = new JLabel("数学成绩:");
        ESLab = new JLabel("英语成绩:");
        //右侧信息填写栏
        idTxt = new JTextField();
        nameTxt = new JTextField();
        CSTxt = new JTextField();
        MSTxt = new JTextField();
        ESTxt = new JTextField();
        //添加和取消按键
        updateBtn = new JButton("修改");
        cancelBtn = new JButton("取消");

        //设置监听
        updateBtn.addActionListener(this);
        updateBtn.setActionCommand("修改");
        cancelBtn.addActionListener(this);
        cancelBtn.setActionCommand("取消");

        //创建布局
        //创建左边栏
        left = new JPanel();
        left.setLayout(new GridLayout(5, 1));
        left.add(idLab);
        left.add(nameLab);
        left.add(CSLab);
        left.add(MSLab);
        left.add(ESLab);
        //创建右边栏
        center = new JPanel();
        center.setLayout(new GridLayout(5, 1));
        center.add(idTxt);
        center.add(nameTxt);
        center.add(CSTxt);
        center.add(MSTxt);
        center.add(ESTxt);
        //底部修改和取消键
        bottom = new JPanel();
        bottom.add(updateBtn);
        bottom.add(cancelBtn);
        //整体布局
        this.add(left, BorderLayout.WEST);
        this.add(center, BorderLayout.CENTER);
        this.add(bottom, BorderLayout.SOUTH);

        //设置窗口属性
        this.setSize(300, 250);
        this.setResizable(false);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();

        if (s.equals("修改")) {
            int i;
            try {
                i = Integer.parseInt(idTxt.getText());
            } catch (NumberFormatException ignored) {
                JOptionPane.showMessageDialog(this, "请输入正确的学号!!!");
                return;
            }

            for (int j = 0; j < narr.size(); j++) {
                if (i == narr.get(j).getId()) {
                    int id = Integer.parseInt(idTxt.getText());
                    String name = nameTxt.getText();
                    if (name.equals("")) {
                        JOptionPane.showMessageDialog(this, "请输入姓名!!!");
                        return;
                    }
                    int CS = InputScores(CSTxt.getText());
                    int MS = InputScores(MSTxt.getText());
                    int ES = InputScores(ESTxt.getText());
                    Student updateStudent = new Student(name, id, CS, MS, ES);
                    narr.set(j, updateStudent);
                    JOptionPane.showMessageDialog(this, "修改成功");
                    this.setVisible(false);
                    return;
                }
            }
            JOptionPane.showMessageDialog(this, "该学号不存在");
        } else if (s.equals("取消")) {
            this.setVisible(false);
        }
    }

    //成绩输入
    public int InputScores(String s) {
        int score = 0;
        try {
            score = Integer.parseInt(s);
        } catch (NumberFormatException ignored) {}
        return score;
    }
}

3. Main program call and related details display 


 1. Illegal warning when entering student number when querying

 

 2. When querying all information, the information is empty prompt

 

3. If the student number in the added interface is empty or in an illegal format, a warning will be issued if the name is empty (the result is empty and the default is to miss the exam)

 

 

4. If the student ID on the deletion interface is illegal or does not exist, a warning will be issued.

 

 

5. If the student ID in the modified interface is illegal or does not exist, a warning will be issued.

6. Save successfully prompt 

 

Summarize:


        The above is my plan to implement the student performance management system. This article only introduces the implementation plan and production process, for reference only. If you have any questions, please leave a message. Welcome to exchange and learn. 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_73540794/article/details/133929978