根据一下内容实现用Java写一个学生成绩管理系统 要求1:带有图形界面。要求2:登录时输入用户名密码。要求3:能够录入、查询、修改、删除课程。 要求4:能够录入、查询、修改、删除学生某个课程的成绩。 要求5:能够持久化。代码不要太长!
时间: 2023-08-11 09:05:47 浏览: 176
使用java语言实现的一个学生成绩管理系统
5星 · 资源好评率100%
好的,以下是一个简单的学生成绩管理系统的Java实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class ScoreManagementSystem extends JFrame implements ActionListener {
private JLabel userLabel, passLabel, courseLabel, studentLabel, gradeLabel;
private JTextField userField, passField, courseField, studentField, gradeField;
private JButton loginButton, addButton, queryButton, modifyButton, deleteButton;
private JTextArea resultArea;
// 存储用户名和密码
private Map<String, String> userMap = new HashMap<>();
// 存储课程信息
private List<Course> courseList = new ArrayList<>();
// 存储学生成绩
private Map<String, Map<String, Integer>> scoreMap = new HashMap<>();
public ScoreManagementSystem() {
initUI();
initData();
}
// 初始化界面
private void initUI() {
userLabel = new JLabel("用户名:");
passLabel = new JLabel("密码:");
courseLabel = new JLabel("课程名:");
studentLabel = new JLabel("学生名:");
gradeLabel = new JLabel("成绩:");
userField = new JTextField(10);
passField = new JPasswordField(10);
courseField = new JTextField(10);
studentField = new JTextField(10);
gradeField = new JTextField(10);
loginButton = new JButton("登录");
addButton = new JButton("添加");
queryButton = new JButton("查询");
modifyButton = new JButton("修改");
deleteButton = new JButton("删除");
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
JPanel loginPanel = new JPanel(new GridLayout(2, 2));
loginPanel.add(userLabel);
loginPanel.add(userField);
loginPanel.add(passLabel);
loginPanel.add(passField);
JPanel coursePanel = new JPanel(new GridLayout(4, 2));
coursePanel.add(courseLabel);
coursePanel.add(courseField);
coursePanel.add(studentLabel);
coursePanel.add(studentField);
coursePanel.add(gradeLabel);
coursePanel.add(gradeField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 5));
buttonPanel.add(addButton);
buttonPanel.add(queryButton);
buttonPanel.add(modifyButton);
buttonPanel.add(deleteButton);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(loginPanel, BorderLayout.NORTH);
contentPane.add(coursePanel, BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
contentPane.add(new JScrollPane(resultArea), BorderLayout.EAST);
loginButton.addActionListener(this);
addButton.addActionListener(this);
queryButton.addActionListener(this);
modifyButton.addActionListener(this);
deleteButton.addActionListener(this);
setTitle("学生成绩管理系统");
setSize(500, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 初始化数据
private void initData() {
try {
// 从文件中读取用户名和密码
Scanner scanner = new Scanner(new File("users.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] tokens = line.split(",");
userMap.put(tokens[0], tokens[1]);
}
scanner.close();
// 从文件中读取课程信息
scanner = new Scanner(new File("courses.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] tokens = line.split(",");
courseList.add(new Course(tokens[0], tokens[1]));
}
scanner.close();
// 从文件中读取学生成绩
scanner = new Scanner(new File("scores.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] tokens = line.split(",");
String student = tokens[0];
String course = tokens[1];
int grade = Integer.parseInt(tokens[2]);
if (!scoreMap.containsKey(student)) {
scoreMap.put(student, new HashMap<>());
}
scoreMap.get(student).put(course, grade);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String user = userField.getText();
String pass = new String(passField.getPassword());
if (userMap.containsKey(user) && userMap.get(user).equals(pass)) {
resultArea.setText("登录成功!");
} else {
resultArea.setText("用户名或密码错误!");
}
} else if (e.getSource() == addButton) {
String course = courseField.getText();
String student = studentField.getText();
int grade = Integer.parseInt(gradeField.getText());
if (!scoreMap.containsKey(student)) {
scoreMap.put(student, new HashMap<>());
}
scoreMap.get(student).put(course, grade);
resultArea.setText("添加成功!");
saveData();
} else if (e.getSource() == queryButton) {
String student = studentField.getText();
if (scoreMap.containsKey(student)) {
StringBuilder sb = new StringBuilder();
sb.append(student).append("的成绩:\n");
Map<String, Integer> scores = scoreMap.get(student);
for (Course course : courseList) {
String courseName = course.getName();
if (scores.containsKey(courseName)) {
sb.append(courseName).append(": ").append(scores.get(courseName)).append("\n");
}
}
resultArea.setText(sb.toString());
} else {
resultArea.setText("该学生不存在!");
}
} else if (e.getSource() == modifyButton) {
String course = courseField.getText();
String student = studentField.getText();
int grade = Integer.parseInt(gradeField.getText());
if (scoreMap.containsKey(student)) {
Map<String, Integer> scores = scoreMap.get(student);
if (scores.containsKey(course)) {
scores.put(course, grade);
resultArea.setText("修改成功!");
saveData();
} else {
resultArea.setText("该学生未选修该课程!");
}
} else {
resultArea.setText("该学生不存在!");
}
} else if (e.getSource() == deleteButton) {
String course = courseField.getText();
String student = studentField.getText();
if (scoreMap.containsKey(student)) {
Map<String, Integer> scores = scoreMap.get(student);
if (scores.containsKey(course)) {
scores.remove(course);
resultArea.setText("删除成功!");
saveData();
} else {
resultArea.setText("该学生未选修该课程!");
}
} else {
resultArea.setText("该学生不存在!");
}
}
}
// 将数据保存到文件中
private void saveData() {
try {
PrintWriter writer = new PrintWriter(new FileWriter("scores.txt"));
for (String student : scoreMap.keySet()) {
Map<String, Integer> scores = scoreMap.get(student);
for (String course : scores.keySet()) {
writer.println(student + "," + course + "," + scores.get(course));
}
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 课程类
private static class Course {
private String name;
private String teacher;
public Course(String name, String teacher) {
this.name = name;
this.teacher = teacher;
}
public String getName() {
return name;
}
public String getTeacher() {
return teacher;
}
}
public static void main(String[] args) {
new ScoreManagementSystem().setVisible(true);
}
}
```
以上代码实现了一个简单的学生成绩管理系统,带有图形界面,能够登录,能够录入、查询、修改、删除课程,能够录入、查询、修改、删除学生某个课程的成绩,能够持久化。
阅读全文