【Java】学生成绩管理系统(图形化界面实现相关功能)
IT啊熊 2024-07-11 13:35:04 阅读 63
前言:
从功能的代码实现到界面的展示整个制作过程全部用Java语言实现。
1、环境搭建
1、在idea创建一个工程文件,在工程文件下创建一个model模块,在model模块下载创建三个package包分别用来存放(BackEndCode)后端代码包、(MainExe)主程序包、(WebCode)界面实现代码包,再在三个包中创建如下图所示文件
2、容器选择:因为本次使用的是自己创建的Student类并且信息数量不确定,所以选择集合作为Student类的容器
2、功能实现
1、学生信息类的实现(BackEndCode包)
为了防止属性不能被外类随意访问,因此采用private对学生类中的属性进行修饰并且进行创建set、get方法以便于调用属性,再创建打印数据方法,方便数据打印,代码如下:
<code>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、学生信息写入读出功能类(BackEndCode包)
这里采用对象序列化流和对象反序列化流实现学生信息写入文本文件和从文本文件读出数据的读写功能,代码如下:
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、学生成绩排序类(BackEndCode包)
用户可以通过选择语文成绩、数学成绩、英语成绩、或者总分进行升降序排序,代码如下:
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、主界面(WebCode包)
主界面效果图如下:(可下拉选择成绩排序方式)
主界面现实代码如下(多个功能集合而成,代码稍多):
<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、添加界面(WebCode包)
添加界面效果图如下:
界面实现代码如下:
<code>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、删除界面(WebCode包)
删除界面效果图如下:
删除界面代码实现如下:
<code>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、修改界面(WebCode包)
修改界面效果图如下:
修改界面代码实现如下:
<code>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、主程序的调用及相关细节展示
1、查询时输入学号非法警告
2、查询全部信息时,信息为空提示
3、 添加界面学号为空或非法格式,姓名为空发出警告(成绩为空默认缺考)
4、删除界面学号非法或不存在发出警告
5、 修改界面学号非法或不存在发出警告
6、保存成功提示
总结:
以上就是我实现学生成绩管理系统的方案,本文仅仅介绍了实现方案及制作流程,仅供参考,若有问题请帮忙留言指出,欢迎交流学习。
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。