用java写一个学生成绩管理系统要求可视化窗口
时间: 2023-12-17 09:05:04 浏览: 73
好的,我可以为你提供一个简单的学生成绩管理系统的Java代码,并使用Java Swing库实现可视化窗口。
首先,我们需要定义一个学生类,用于存储学生的信息和成绩。代码实现如下:
```java
public class Student {
private String name;
private int mathScore;
private int englishScore;
private int programmingScore;
public Student(String name, int mathScore, int englishScore, int programmingScore) {
this.name = name;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.programmingScore = programmingScore;
}
public String getName() {
return name;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getProgrammingScore() {
return programmingScore;
}
}
```
接下来,我们需要创建一个可视化窗口,用于展示学生信息和成绩。代码实现如下:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
public class StudentGradeManagementSystem extends JFrame {
private ArrayList<Student> students;
private JTable table;
private DefaultTableModel model;
private JTextField nameField;
private JTextField mathField;
private JTextField englishField;
private JTextField programmingField;
public StudentGradeManagementSystem() {
students = new ArrayList<>();
// 设置窗口标题和大小
setTitle("学生成绩管理系统");
setSize(600, 400);
// 创建表格和表格模型
table = new JTable();
model = new DefaultTableModel(new Object[][]{}, new String[]{"姓名", "数学成绩", "英语成绩", "编程成绩"});
table.setModel(model);
// 创建添加学生信息的面板
JPanel addPanel = new JPanel(new GridLayout(2, 4));
JLabel nameLabel = new JLabel("姓名");
JLabel mathLabel = new JLabel("数学成绩");
JLabel englishLabel = new JLabel("英语成绩");
JLabel programmingLabel = new JLabel("编程成绩");
nameField = new JTextField();
mathField = new JTextField();
englishField = new JTextField();
programmingField = new JTextField();
JButton addButton = new JButton("添加");
addButton.addActionListener(e -> addStudent());
addPanel.add(nameLabel);
addPanel.add(mathLabel);
addPanel.add(englishLabel);
addPanel.add(programmingLabel);
addPanel.add(nameField);
addPanel.add(mathField);
addPanel.add(englishField);
addPanel.add(programmingField);
addPanel.add(addButton);
// 将表格和添加学生信息的面板添加到窗口中
add(new JScrollPane(table), BorderLayout.CENTER);
add(addPanel, BorderLayout.SOUTH);
// 显示窗口
setVisible(true);
}
private void addStudent() {
String name = nameField.getText();
int mathScore = Integer.parseInt(mathField.getText());
int englishScore = Integer.parseInt(englishField.getText());
int programmingScore = Integer.parseInt(programmingField.getText());
Student student = new Student(name, mathScore, englishScore, programmingScore);
students.add(student);
model.addRow(new Object[]{name, mathScore, englishScore, programmingScore});
nameField.setText("");
mathField.setText("");
englishField.setText("");
programmingField.setText("");
}
}
```
最后,在main方法中创建StudentGradeManagementSystem对象,即可启动程序。
```java
public class Main {
public static void main(String[] args) {
new StudentGradeManagementSystem();
}
}
```
运行程序后,你将看到一个可视化的窗口,可以通过输入学生信息和成绩来添加学生,同时可以在表格中查看已添加的学生信息和成绩。
阅读全文