用Java写一个学生成绩管理系统项目,学生信息包括有姓名,性别,学号,学院,科目,成绩。界面有查询,添加,删除,清空按钮(并且使用swing组件)
时间: 2023-11-17 18:07:09 浏览: 79
好的,我可以为您提供一些参考代码,帮助您实现这个学生成绩管理系统项目。
首先,您需要创建一个Student类,来存储学生的信息:
```java
public class Student {
private String name;
private String gender;
private String id;
private String college;
private String subject;
private int score;
public Student(String name, String gender, String id, String college, String subject, int score) {
this.name = name;
this.gender = gender;
this.id = id;
this.college = college;
this.subject = subject;
this.score = score;
}
// 添加getter和setter方法
// ...
}
```
接下来,您可以创建一个学生成绩管理系统的主界面,使用swing组件来实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class ScoreManagementSystem extends JFrame implements ActionListener {
private JTextField textName, textGender, textId, textCollege, textSubject, textScore;
private JButton btnQuery, btnAdd, btnDelete, btnClear;
private JTextArea resultArea;
private ArrayList<Student> studentList;
public ScoreManagementSystem() {
// 初始化界面
setTitle("学生成绩管理系统");
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建组件
JLabel labelName = new JLabel("姓名:");
textName = new JTextField(10);
JLabel labelGender = new JLabel("性别:");
textGender = new JTextField(10);
JLabel labelId = new JLabel("学号:");
textId = new JTextField(10);
JLabel labelCollege = new JLabel("学院:");
textCollege = new JTextField(10);
JLabel labelSubject = new JLabel("科目:");
textSubject = new JTextField(10);
JLabel labelScore = new JLabel("成绩:");
textScore = new JTextField(10);
btnQuery = new JButton("查询");
btnAdd = new JButton("添加");
btnDelete = new JButton("删除");
btnClear = new JButton("清空");
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
// 创建布局
JPanel topPanel = new JPanel(new GridLayout(6, 2));
topPanel.add(labelName);
topPanel.add(textName);
topPanel.add(labelGender);
topPanel.add(textGender);
topPanel.add(labelId);
topPanel.add(textId);
topPanel.add(labelCollege);
topPanel.add(textCollege);
topPanel.add(labelSubject);
topPanel.add(textSubject);
topPanel.add(labelScore);
topPanel.add(textScore);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(btnQuery);
btnPanel.add(btnAdd);
btnPanel.add(btnDelete);
btnPanel.add(btnClear);
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.add(new JScrollPane(resultArea), BorderLayout.CENTER);
// 添加组件到主界面
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
add(btnPanel, BorderLayout.CENTER);
add(resultPanel, BorderLayout.SOUTH);
// 添加事件监听器
btnQuery.addActionListener(this);
btnAdd.addActionListener(this);
btnDelete.addActionListener(this);
btnClear.addActionListener(this);
// 初始化学生列表
studentList = new ArrayList<>();
studentList.add(new Student("张三", "男", "1001", "计算机学院", "Java程序设计", 90));
studentList.add(new Student("李四", "女", "1002", "计算机学院", "数据结构", 85));
studentList.add(new Student("王五", "男", "1003", "信息学院", "数字信号处理", 92));
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnQuery) {
// 查询学生信息
String id = textId.getText();
for (Student student : studentList) {
if (student.getId().equals(id)) {
resultArea.setText(student.toString());
return;
}
}
resultArea.setText("学号为" + id + "的学生不存在!");
} else if (e.getSource() == btnAdd) {
// 添加学生信息
String name = textName.getText();
String gender = textGender.getText();
String id = textId.getText();
String college = textCollege.getText();
String subject = textSubject.getText();
int score = Integer.parseInt(textScore.getText());
Student student = new Student(name, gender, id, college, subject, score);
studentList.add(student);
resultArea.setText("添加成功!");
} else if (e.getSource() == btnDelete) {
// 删除学生信息
String id = textId.getText();
for (Student student : studentList) {
if (student.getId().equals(id)) {
studentList.remove(student);
resultArea.setText("删除成功!");
return;
}
}
resultArea.setText("学号为" + id + "的学生不存在!");
} else if (e.getSource() == btnClear) {
// 清空文本框
textName.setText("");
textGender.setText("");
textId.setText("");
textCollege.setText("");
textSubject.setText("");
textScore.setText("");
resultArea.setText("");
}
}
public static void main(String[] args) {
ScoreManagementSystem system = new ScoreManagementSystem();
system.setVisible(true);
}
}
```
在这个界面中,我们创建了文本框和按钮,用于查询、添加、删除和清空学生信息。我们还创建了一个ArrayList,用于存储所有学生的信息。在事件监听器中,我们通过判断事件来源来执行不同的操作。例如,如果点击了查询按钮,我们就会从学生列表中查询对应的学生信息,并在文本框中显示出来。如果点击了添加按钮,我们就会从文本框中读取学生信息,创建一个新的Student对象,并将其添加到学生列表中。
希望这些代码能够帮助到您,如果您有任何问题或需要进一步的帮助,请随时告诉我。
阅读全文
相关推荐


















