GUI 学生成绩管理系统
时间: 2023-12-22 09:28:08 浏览: 130
matlab GUI 学生成绩管理系统.doc
以下是一个基于Java Swing的GUI学生成绩管理系统的示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class GradeManagementSystem extends JFrame implements ActionListener {
private JTextField filePathField;
private JTextArea resultArea;
private JButton importButton;
private JButton analyzeButton;
private JButton sortButton;
private JButton exportButton;
private ArrayList<Integer> grades = new ArrayList<Integer>();
public GradeManagementSystem() {
super("学生成绩管理系统");
// 设置窗口大小和位置
setSize(600, 400);
setLocationRelativeTo(null);
// 设置窗口关闭时的操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建组件
JLabel filePathLabel = new JLabel("文件路径:");
filePathField = new JTextField(20);
importButton = new JButton("导入");
analyzeButton = new JButton("分析");
sortButton = new JButton("排序");
exportButton = new JButton("导出");
resultArea = new JTextArea(10, 40);
// 设置组件的事件监听器
importButton.addActionListener(this);
analyzeButton.addActionListener(this);
sortButton.addActionListener(this);
exportButton.addActionListener(this);
// 添加组件到窗口中
JPanel panel = new JPanel();
panel.add(filePathLabel);
panel.add(filePathField);
panel.add(importButton);
panel.add(analyzeButton);
panel.add(sortButton);
panel.add(exportButton);
add(panel, BorderLayout.NORTH);
add(new JScrollPane(resultArea), BorderLayout.CENTER);
// 显示窗口
setVisible(true);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == importButton) {
// 导入文件
String filePath = filePathField.getText();
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
grades.add(Integer.parseInt(line));
}
reader.close();
resultArea.setText("成功导入 " + grades.size() + " 条数据");
} catch (IOException ex) {
resultArea.setText("导入失败:" + ex.getMessage());
}
} else if (e.getSource() == analyzeButton) {
// 分析数据
if (grades.size() == 0) {
resultArea.setText("请先导入数据");
return;
}
int sum = 0;
int max = grades.get(0);
int min = grades.get(0);
for (int grade : grades) {
sum += grade;
if (grade > max) {
max = grade;
}
if (grade < min) {
min = grade;
}
}
double average = (double) sum / grades.size();
double variance = 0;
for (int grade : grades) {
variance += (grade - average) * (grade - average);
}
double standardDeviation = Math.sqrt(variance / grades.size());
resultArea.setText("最高分:" + max + "\n最低分:" + min + "\n平均分:" + average + "\n标准差:" + standardDeviation);
} else if (e.getSource() == sortButton) {
// 排序数据
if (grades.size() == 0) {
resultArea.setText("请先导入数据");
return;
}
Collections.sort(grades);
resultArea.setText("排序结果:\n");
for (int grade : grades) {
resultArea.append(grade + "\n");
}
} else if (e.getSource() == exportButton) {
// 导出数据
if (grades.size() == 0) {
resultArea.setText("请先导入数据");
return;
}
String filePath = filePathField.getText();
int dotIndex = filePath.lastIndexOf(".");
String outputFilePath = filePath.substring(0, dotIndex) + ".xls";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath));
for (int grade : grades) {
writer.write(grade + "\t");
}
writer.close();
resultArea.setText("成功导出数据到 " + outputFilePath);
} catch (IOException ex) {
resultArea.setText("导出失败:" + ex.getMessage());
}
}
}
public static void main(String[] args) {
new GradeManagementSystem();
}
}
```
阅读全文