用Java写一个学生成绩管理系统,学生信息包括有姓名,性别,年龄,学院,科目,成绩(并且使用swing组件)
时间: 2023-12-04 18:03:43 浏览: 64
好的,我可以为您提供一个基本的学生成绩管理系统的Java代码,使用swing组件实现图形化界面。以下是代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class StudentGradeManagementSystem extends JFrame implements ActionListener {
private JLabel nameLabel, genderLabel, ageLabel, collegeLabel, subjectLabel, gradeLabel;
private JTextField nameField, ageField, collegeField, subjectField, gradeField;
private JComboBox<String> genderBox;
private JButton addButton, clearButton, displayButton;
private JTextArea displayArea;
private ArrayList<Student> studentList;
public StudentGradeManagementSystem() {
setTitle("学生成绩管理系统");
setLayout(new GridLayout(8, 2, 5, 5));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nameLabel = new JLabel("姓名:");
add(nameLabel);
nameField = new JTextField(10);
add(nameField);
genderLabel = new JLabel("性别:");
add(genderLabel);
genderBox = new JComboBox<>(new String[] {"男", "女"});
add(genderBox);
ageLabel = new JLabel("年龄:");
add(ageLabel);
ageField = new JTextField(10);
add(ageField);
collegeLabel = new JLabel("学院:");
add(collegeLabel);
collegeField = new JTextField(10);
add(collegeField);
subjectLabel = new JLabel("科目:");
add(subjectLabel);
subjectField = new JTextField(10);
add(subjectField);
gradeLabel = new JLabel("成绩:");
add(gradeLabel);
gradeField = new JTextField(10);
add(gradeField);
addButton = new JButton("添加");
addButton.addActionListener(this);
add(addButton);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
add(clearButton);
displayButton = new JButton("显示");
displayButton.addActionListener(this);
add(displayButton);
displayArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(displayArea);
add(scrollPane);
studentList = new ArrayList<>();
setSize(500, 400);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
String gender = genderBox.getSelectedItem().toString();
int age = Integer.parseInt(ageField.getText());
String college = collegeField.getText();
String subject = subjectField.getText();
int grade = Integer.parseInt(gradeField.getText());
Student student = new Student(name, gender, age, college, subject, grade);
studentList.add(student);
JOptionPane.showMessageDialog(this, "添加成功!");
} else if (e.getSource() == clearButton) {
nameField.setText("");
genderBox.setSelectedIndex(0);
ageField.setText("");
collegeField.setText("");
subjectField.setText("");
gradeField.setText("");
} else if (e.getSource() == displayButton) {
displayArea.setText("");
for (Student student : studentList) {
displayArea.append(student.toString() + "\n");
}
}
}
public static void main(String[] args) {
new StudentGradeManagementSystem();
}
}
class Student {
private String name;
private String gender;
private int age;
private String college;
private String subject;
private int grade;
public Student(String name, String gender, int age, String college, String subject, int grade) {
this.name = name;
this.gender = gender;
this.age = age;
this.college = college;
this.subject = subject;
this.grade = grade;
}
public String toString() {
return "姓名:" + name + ",性别:" + gender + ",年龄:" + age + ",学院:" + college + ",科目:" + subject + ",成绩:" + grade;
}
}
```
在该程序中,使用了JFrame、JLabel、JTextField、JComboBox、JButton、JTextArea、JScrollPane等Swing组件。其中,JFrame作为主窗口,JLabel和JTextField用于输入学生信息,JComboBox提供性别选择,JButton实现添加、清空和显示功能,JTextArea和JScrollPane用于显示学生信息。
在程序中定义了Student类,用于保存学生信息,重写了toString()方法,用于在显示学生信息时方便输出。同时,使用ArrayList<Student>来保存学生列表,实现添加、清空和显示功能。
阅读全文