用java语言写一个学生成绩管理系统适配eclipse编译系统 需遵守一下要求 1.带有图形界面2.登录时输入用户密码,且进入时打开新的界面,且能在该页面上直接增添删除3能够录入查询 修改删除课程4能够录入 查询 修改 删除学生某个课程的成绩5能够持久化6 系统中的注释语言改成中文
时间: 2024-01-24 22:17:17 浏览: 113
很好,我可以给您提供一个简单的学生成绩管理系统代码,代码如下:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StudentGradeSystem {
private JFrame frame;
private JTextField usernameField;
private JTextField passwordField;
private JPanel loginPanel;
private JPanel mainPanel;
private ArrayList<Student> students;
private ArrayList<Course> courses;
private File studentFile;
private File courseFile;
private boolean loggedIn;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentGradeSystem window = new StudentGradeSystem();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public StudentGradeSystem() {
initialize();
loadFiles();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginPanel = new JPanel();
frame.getContentPane().add(loginPanel, BorderLayout.CENTER);
loginPanel.setLayout(null);
JLabel lblUsername = new JLabel("用户名:");
lblUsername.setBounds(76, 75, 61, 16);
loginPanel.add(lblUsername);
JLabel lblPassword = new JLabel("密码:");
lblPassword.setBounds(76, 103, 61, 16);
loginPanel.add(lblPassword);
usernameField = new JTextField();
usernameField.setBounds(149, 70, 130, 26);
loginPanel.add(usernameField);
usernameField.setColumns(10);
passwordField = new JTextField();
passwordField.setColumns(10);
passwordField.setBounds(149, 98, 130, 26);
loginPanel.add(passwordField);
JButton btnLogin = new JButton("登录");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
if (username.equals("admin") && password.equals("admin")) {
loggedIn = true;
loginPanel.setVisible(false);
mainPanel.setVisible(true);
} else {
JOptionPane.showMessageDialog(frame, "用户名或密码不正确!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
btnLogin.setBounds(149, 136, 117, 29);
loginPanel.add(btnLogin);
mainPanel = new JPanel();
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(null);
JButton btnAddStudent = new JButton("添加学生");
btnAddStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog(frame, "请输入学生姓名:");
if (name != null && !name.isEmpty()) {
Student student = new Student(name);
students.add(student);
saveStudents();
}
}
});
btnAddStudent.setBounds(6, 6, 117, 29);
mainPanel.add(btnAddStudent);
JButton btnDeleteStudent = new JButton("删除学生");
btnDeleteStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog(frame, "请输入学生姓名:");
if (name != null && !name.isEmpty()) {
Student student = findStudent(name);
if (student != null) {
students.remove(student);
saveStudents();
}
}
}
});
btnDeleteStudent.setBounds(135, 6, 117, 29);
mainPanel.add(btnDeleteStudent);
JButton btnAddCourse = new JButton("添加课程");
btnAddCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog(frame, "请输入课程名称:");
if (name != null && !name.isEmpty()) {
Course course = new Course(name);
courses.add(course);
saveCourses();
}
}
});
btnAddCourse.setBounds(264, 6, 117, 29);
mainPanel.add(btnAddCourse);
JButton btnDeleteCourse = new JButton("删除课程");
btnDeleteCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog(frame, "请输入课程名称:");
if (name != null && !name.isEmpty()) {
Course course = findCourse(name);
if (course != null) {
courses.remove(course);
saveCourses();
}
}
}
});
btnDeleteCourse.setBounds(6, 47, 117, 29);
mainPanel.add(btnDeleteCourse);
JButton btnUpdateGrade = new JButton("更新成绩");
btnUpdateGrade.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String studentName = JOptionPane.showInputDialog(frame, "请输入学生姓名:");
if (studentName != null && !studentName.isEmpty()) {
String courseName = JOptionPane.showInputDialog(frame, "请输入课程名称:");
if (courseName != null && !courseName.isEmpty()) {
double grade = Double.parseDouble(JOptionPane.showInputDialog(frame, "请输入成绩:"));
Student student = findStudent(studentName);
Course course = findCourse(courseName);
if (student != null && course != null) {
student.setGrade(course, grade);
saveStudents();
}
}
}
}
});
btnUpdateGrade.setBounds(135, 47, 117, 29);
mainPanel.add(btnUpdateGrade);
JButton btnSearchGrade = new JButton("查询成绩");
btnSearchGrade.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String studentName = JOptionPane.showInputDialog(frame, "请输入学生姓名:");
if (studentName != null && !studentName.isEmpty()) {
String courseName = JOptionPane.showInputDialog(frame, "请输入课程名称:");
if (courseName != null && !courseName.isEmpty()) {
Student student = findStudent(studentName);
Course course = findCourse(courseName);
if (student != null && course != null) {
double grade = student.getGrade(course);
JOptionPane.showMessageDialog(frame, studentName + "在" + courseName + "的成绩为:" + grade);
}
}
}
}
});
btnSearchGrade.setBounds(264, 47, 117, 29);
mainPanel.add(btnSearchGrade);
mainPanel.setVisible(false);
}
private void loadFiles() {
studentFile = new File("students.txt");
if (studentFile.exists()) {
students = readStudents(studentFile);
} else {
students = new ArrayList<Student>();
}
courseFile = new File("courses.txt");
if (courseFile.exists()) {
courses = readCourses(courseFile);
} else {
courses = new ArrayList<Course>();
}
loggedIn = false;
}
private ArrayList<Student> readStudents(File file) {
ArrayList<Student> list = new ArrayList<Student>();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
String name = parts[0];
Student student = new Student(name);
for (int i = 1; i < parts.length; i += 2) {
String courseName = parts[i];
double grade = Double.parseDouble(parts[i + 1]);
Course course = findCourse(courseName);
if (course != null) {
student.setGrade(course, grade);
}
}
list.add(student);
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private ArrayList<Course> readCourses(File file) {
ArrayList<Course> list = new ArrayList<Course>();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Course course = new Course(line);
list.add(course);
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private Student findStudent(String name) {
for (Student student : students) {
if (student.getName().equals(name)) {
return student;
}
}
return null;
}
private Course findCourse(String name) {
for (Course course : courses) {
if (course.getName().equals(name)) {
return course;
}
}
return null;
}
private void saveStudents() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(studentFile));
for (Student student : students) {
writer.write(student.getName());
for (Course course : courses) {
Double grade = student.getGrade(course);
if (grade != null) {
writer.write("," + course.getName() + "," + grade);
}
}
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveCourses() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(courseFile));
for (Course course : courses) {
writer.write(course.getName());
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Student {
private String name;
private ArrayList<Course> courses;
private ArrayList<Double> grades;
public Student(String name) {
this.name = name;
courses = new ArrayList<Course>();
grades = new ArrayList<Double>();
}
public String getName() {
return name;
}
public Double getGrade(Course course) {
int index = courses.indexOf(course);
if (index != -1) {
return grades.get(index);
} else {
return null;
}
}
public void setGrade(Course course, double grade) {
int index = courses.indexOf(course);
if (index != -1) {
grades.set(index, grade);
} else {
courses.add(course);
grades.add(grade);
}
}
}
class Course {
private String name;
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
```
这是一个简单的学生成绩管理系统,具有图形界面,登录时需要输入用户名和密码,能够录入、查询、修改、删除学生某个课程的成绩等功能,同时能够持久化保存数据。您可以在 Eclipse 中编译和运行这个程序。
阅读全文