用Java写一个成绩管理系统,有图形化界面,能实现文件输入输出流
时间: 2024-03-27 21:36:55 浏览: 66
以下是一个简单的成绩管理系统的Java代码,使用了Swing实现图形化界面,并且能够使用文件输入输出流进行数据的读写操作。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class ScoreManagementSystem extends JFrame {
private JLabel nameLabel, scoreLabel;
private JTextField nameField, scoreField;
private JButton addButton, showButton, saveButton, loadButton;
private JTextArea infoArea;
public ScoreManagementSystem() {
super("成绩管理系统");
// 创建组件
nameLabel = new JLabel("姓名:");
scoreLabel = new JLabel("成绩:");
nameField = new JTextField(10);
scoreField = new JTextField(10);
addButton = new JButton("添加");
showButton = new JButton("显示");
saveButton = new JButton("保存");
loadButton = new JButton("读取");
infoArea = new JTextArea(10, 30);
// 添加组件
JPanel panel1 = new JPanel();
panel1.add(nameLabel);
panel1.add(nameField);
panel1.add(scoreLabel);
panel1.add(scoreField);
panel1.add(addButton);
panel1.add(showButton);
panel1.add(saveButton);
panel1.add(loadButton);
add(panel1, BorderLayout.NORTH);
add(infoArea, BorderLayout.CENTER);
// 绑定事件
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String scoreStr = scoreField.getText();
if (name.length() == 0 || scoreStr.length() == 0) {
JOptionPane.showMessageDialog(ScoreManagementSystem.this, "请输入姓名和成绩", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
try {
double score = Double.parseDouble(scoreStr);
addScore(name, score);
infoArea.setText(getAllScores());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(ScoreManagementSystem.this, "请输入正确的成绩", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
infoArea.setText(getAllScores());
}
});
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileNameExtensionFilter("文本文件", "txt"));
int result = fc.showSaveDialog(ScoreManagementSystem.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
saveScores(file);
JOptionPane.showMessageDialog(ScoreManagementSystem.this, "保存成功", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(ScoreManagementSystem.this, "保存失败:" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
});
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileNameExtensionFilter("文本文件", "txt"));
int result = fc.showOpenDialog(ScoreManagementSystem.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
loadScores(file);
infoArea.setText(getAllScores());
JOptionPane.showMessageDialog(ScoreManagementSystem.this, "读取成功", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(ScoreManagementSystem.this, "读取失败:" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
});
// 设置窗口属性
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
}
// 添加一条成绩
private void addScore(String name, double score) {
try {
FileWriter fw = new FileWriter("scores.txt", true);
fw.write(name + " " + score + "\n");
fw.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "写入文件失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
// 获取所有成绩
private String getAllScores() {
StringBuilder sb = new StringBuilder();
try {
FileReader fr = new FileReader("scores.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
fr.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "读取文件失败:" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
return sb.toString();
}
// 保存成绩到文件
private void saveScores(File file) throws IOException {
FileWriter fw = new FileWriter(file);
FileReader fr = new FileReader("scores.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
fw.write(line + "\n");
}
br.close();
fr.close();
fw.close();
}
// 从文件读取成绩
private void loadScores(File file) throws IOException {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
FileWriter fw = new FileWriter("scores.txt");
while ((line = br.readLine()) != null) {
fw.write(line + "\n");
}
br.close();
fr.close();
fw.close();
}
public static void main(String[] args) {
new ScoreManagementSystem();
}
}
```
这个成绩管理系统的界面包括姓名、成绩两个输入框,以及添加、显示、保存、读取四个按钮。当用户点击添加按钮时,程序将读取输入框中的姓名和成绩,然后将它们写入到一个文件中。当用户点击显示按钮时,程序将读取文件中的所有成绩,并将它们显示在界面的文本区域中。当用户点击保存按钮时,程序将弹出一个文件选择对话框,用户可以选择一个文件,然后程序将把当前所有成绩写入到该文件中。当用户点击读取按钮时,程序将弹出一个文件选择对话框,用户可以选择一个文件,然后程序将从该文件中读取成绩,并将它们写入到原来的文件中。
阅读全文