编写一个图形界面,分别输入学生的学号,姓名,班级和四门功课的成绩(程序设计,高等数学,英语,面向对象),能够对一个班的学生成绩进行统计(排序,最高成绩,最低成绩,平均成绩)并输出相应结果。 3、开发环境:JDK+Myeclipse.
时间: 2023-10-20 13:07:15 浏览: 212
以下是一个简单的Java Swing GUI程序,用于输入学生信息和成绩,并对班级成绩进行统计和排序:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class StudentGradeGUI extends JFrame implements ActionListener {
private JLabel labelID, labelName, labelClass, labelProgram, labelMath, labelEnglish, labelOOP, labelResult;
private JTextField fieldID, fieldName, fieldClass, fieldProgram, fieldMath, fieldEnglish, fieldOOP;
private JButton buttonSubmit, buttonSort;
private JTextArea textArea;
private ArrayList<Student> students;
public StudentGradeGUI() {
super("Student Grade");
labelID = new JLabel("ID:");
labelName = new JLabel("Name:");
labelClass = new JLabel("Class:");
labelProgram = new JLabel("Program:");
labelMath = new JLabel("Math:");
labelEnglish = new JLabel("English:");
labelOOP = new JLabel("OOP:");
labelResult = new JLabel();
fieldID = new JTextField(10);
fieldName = new JTextField(10);
fieldClass = new JTextField(10);
fieldProgram = new JTextField(10);
fieldMath = new JTextField(10);
fieldEnglish = new JTextField(10);
fieldOOP = new JTextField(10);
buttonSubmit = new JButton("Submit");
buttonSubmit.addActionListener(this);
buttonSort = new JButton("Sort");
buttonSort.addActionListener(this);
textArea = new JTextArea(10, 30);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
JPanel panelInput = new JPanel(new GridLayout(7, 2));
panelInput.add(labelID);
panelInput.add(fieldID);
panelInput.add(labelName);
panelInput.add(fieldName);
panelInput.add(labelClass);
panelInput.add(fieldClass);
panelInput.add(labelProgram);
panelInput.add(fieldProgram);
panelInput.add(labelMath);
panelInput.add(fieldMath);
panelInput.add(labelEnglish);
panelInput.add(fieldEnglish);
panelInput.add(labelOOP);
panelInput.add(fieldOOP);
JPanel panelButton = new JPanel(new FlowLayout());
panelButton.add(buttonSubmit);
panelButton.add(buttonSort);
JPanel panelResult = new JPanel(new FlowLayout());
panelResult.add(labelResult);
JPanel panelOutput = new JPanel(new BorderLayout());
panelOutput.add(BorderLayout.NORTH, panelResult);
panelOutput.add(BorderLayout.CENTER, scrollPane);
JPanel panelMain = new JPanel(new BorderLayout());
panelMain.add(BorderLayout.NORTH, panelInput);
panelMain.add(BorderLayout.CENTER, panelOutput);
panelMain.add(BorderLayout.SOUTH, panelButton);
add(panelMain);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setVisible(true);
students = new ArrayList<>();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonSubmit) {
Student student = new Student(fieldID.getText(), fieldName.getText(), fieldClass.getText(),
Double.parseDouble(fieldProgram.getText()), Double.parseDouble(fieldMath.getText()),
Double.parseDouble(fieldEnglish.getText()), Double.parseDouble(fieldOOP.getText()));
students.add(student);
labelResult.setText("Submitted " + students.size() + " students.");
fieldID.setText("");
fieldName.setText("");
fieldClass.setText("");
fieldProgram.setText("");
fieldMath.setText("");
fieldEnglish.setText("");
fieldOOP.setText("");
} else if (e.getSource() == buttonSort) {
Collections.sort(students);
StringBuilder sb = new StringBuilder();
double sumProgram = 0, sumMath = 0, sumEnglish = 0, sumOOP = 0;
double maxProgram = 0, maxMath = 0, maxEnglish = 0, maxOOP = 0;
double minProgram = Double.MAX_VALUE, minMath = Double.MAX_VALUE, minEnglish = Double.MAX_VALUE, minOOP = Double.MAX_VALUE;
for (Student student : students) {
sb.append(student.toString() + "\n");
sumProgram += student.getProgram();
sumMath += student.getMath();
sumEnglish += student.getEnglish();
sumOOP += student.getOOP();
if (student.getProgram() > maxProgram) maxProgram = student.getProgram();
if (student.getMath() > maxMath) maxMath = student.getMath();
if (student.getEnglish() > maxEnglish) maxEnglish = student.getEnglish();
if (student.getOOP() > maxOOP) maxOOP = student.getOOP();
if (student.getProgram() < minProgram) minProgram = student.getProgram();
if (student.getMath() < minMath) minMath = student.getMath();
if (student.getEnglish() < minEnglish) minEnglish = student.getEnglish();
if (student.getOOP() < minOOP) minOOP = student.getOOP();
}
double avgProgram = sumProgram / students.size();
double avgMath = sumMath / students.size();
double avgEnglish = sumEnglish / students.size();
double avgOOP = sumOOP / students.size();
sb.append("Average Program: " + avgProgram + "\n");
sb.append("Average Math: " + avgMath + "\n");
sb.append("Average English: " + avgEnglish + "\n");
sb.append("Average OOP: " + avgOOP + "\n");
sb.append("Max Program: " + maxProgram + "\n");
sb.append("Max Math: " + maxMath + "\n");
sb.append("Max English: " + maxEnglish + "\n");
sb.append("Max OOP: " + maxOOP + "\n");
sb.append("Min Program: " + minProgram + "\n");
sb.append("Min Math: " + minMath + "\n");
sb.append("Min English: " + minEnglish + "\n");
sb.append("Min OOP: " + minOOP + "\n");
textArea.setText(sb.toString());
}
}
public static void main(String[] args) {
new StudentGradeGUI();
}
}
class Student implements Comparable<Student> {
private String id, name, clazz;
private double program, math, english, oop;
public Student(String id, String name, String clazz, double program, double math, double english, double oop) {
this.id = id;
this.name = name;
this.clazz = clazz;
this.program = program;
this.math = math;
this.english = english;
this.oop = oop;
}
public String getId() { return id; }
public String getName() { return name; }
public String getClazz() { return clazz; }
public double getProgram() { return program; }
public double getMath() { return math; }
public double getEnglish() { return english; }
public double getOOP() { return oop; }
public double getAverage() {
return (program + math + english + oop) / 4;
}
public String toString() {
return id + "\t" + name + "\t" + clazz + "\t" + program + "\t" + math + "\t" + english + "\t" + oop + "\t" + getAverage();
}
public int compareTo(Student other) {
return Double.compare(other.getAverage(), getAverage());
}
}
```
运行上述程序后,会弹出一个窗口,可以输入学生信息和成绩,并提交或排序。排序后,会在文本框中显示学生信息和班级成绩统计结果。
阅读全文