用java语言写一个学生成绩管理系统适配eclipse编译系统 需遵守一下要求 1.带有图形界面2.登录时输入用户密码,且进入时打开新的界面,且自定义学生成绩的初始条件3能够录入查询 修改删除课程4能够录入 查询 修改 删除学生某个课程的成绩5能够持久化
时间: 2024-01-24 10:16:54 浏览: 24
好的,以下是一个简单的学生成绩管理系统的Java代码,适配Eclipse编译系统,满足您提出的要求:
```
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class StudentGradeManagementSystem {
private JFrame frame;
private JTextField textFieldUsername;
private JPasswordField passwordField;
private JTable table;
private DefaultTableModel model;
private List<Student> students;
private List<Course> courses;
private File file;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentGradeManagementSystem window = new StudentGradeManagementSystem();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public StudentGradeManagementSystem() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
// Load data from file
loadData();
frame = new JFrame();
frame.setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
// Login Panel
JPanel panelLogin = new JPanel();
panelLogin.setBounds(10, 10, 564, 343);
panelLogin.setLayout(null);
frame.getContentPane().add(panelLogin);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(140, 100, 80, 20);
panelLogin.add(lblUsername);
textFieldUsername = new JTextField();
textFieldUsername.setBounds(230, 100, 120, 20);
panelLogin.add(textFieldUsername);
textFieldUsername.setColumns(10);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(140, 140, 80, 20);
panelLogin.add(lblPassword);
passwordField = new JPasswordField();
passwordField.setBounds(230, 140, 120, 20);
panelLogin.add(passwordField);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = textFieldUsername.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("admin")) {
openMainPanel();
} else {
JOptionPane.showMessageDialog(frame, "Invalid username or password!");
}
}
});
btnLogin.setBounds(230, 180, 80, 20);
panelLogin.add(btnLogin);
// Main Panel
JPanel panelMain = new JPanel();
panelMain.setBounds(10, 10, 564, 343);
panelMain.setLayout(null);
frame.getContentPane().add(panelMain);
panelMain.setVisible(false);
JLabel lblTitle = new JLabel("Student Grade Management System");
lblTitle.setBounds(180, 10, 250, 20);
panelMain.add(lblTitle);
JButton btnAddCourse = new JButton("Add Course");
btnAddCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String courseName = JOptionPane.showInputDialog(frame, "Enter course name:");
if (courseName != null && !courseName.isEmpty()) {
courses.add(new Course(courseName));
saveData();
updateTable();
}
}
});
btnAddCourse.setBounds(20, 50, 100, 20);
panelMain.add(btnAddCourse);
JButton btnEditCourse = new JButton("Edit Course");
btnEditCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectedIndex = table.getSelectedRow();
if (selectedIndex >= 0) {
Course course = courses.get(selectedIndex);
String courseName = JOptionPane.showInputDialog(frame, "Enter course name:", course.getName());
if (courseName != null && !courseName.isEmpty()) {
course.setName(courseName);
saveData();
updateTable();
}
} else {
JOptionPane.showMessageDialog(frame, "Please select a course!");
}
}
});
btnEditCourse.setBounds(130, 50, 100, 20);
panelMain.add(btnEditCourse);
JButton btnDeleteCourse = new JButton("Delete Course");
btnDeleteCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectedIndex = table.getSelectedRow();
if (selectedIndex >= 0) {
courses.remove(selectedIndex);
saveData();
updateTable();
} else {
JOptionPane.showMessageDialog(frame, "Please select a course!");
}
}
});
btnDeleteCourse.setBounds(240, 50, 100, 20);
panelMain.add(btnDeleteCourse);
JButton btnAddStudent = new JButton("Add Student");
btnAddStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String studentName = JOptionPane.showInputDialog(frame, "Enter student name:");
if (studentName != null && !studentName.isEmpty()) {
Student student = new Student(studentName);
for (Course course : courses) {
String grade = JOptionPane.showInputDialog(frame, "Enter grade for " + course.getName() + ":");
if (grade != null && !grade.isEmpty()) {
student.addGrade(course, Double.parseDouble(grade));
}
}
students.add(student);
saveData();
updateTable();
}
}
});
btnAddStudent.setBounds(20, 80, 100, 20);
panelMain.add(btnAddStudent);
JButton btnEditStudent = new JButton("Edit Student");
btnEditStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectedIndex = table.getSelectedRow();
if (selectedIndex >= 0) {
Student student = students.get(selectedIndex);
String studentName = JOptionPane.showInputDialog(frame, "Enter student name:", student.getName());
if (studentName != null && !studentName.isEmpty()) {
student.setName(studentName);
for (Course course : courses) {
String grade = JOptionPane.showInputDialog(frame, "Enter grade for " + course.getName() + ":", student.getGrade(course));
if (grade != null && !grade.isEmpty()) {
student.setGrade(course, Double.parseDouble(grade));
}
}
saveData();
updateTable();
}
} else {
JOptionPane.showMessageDialog(frame, "Please select a student!");
}
}
});
btnEditStudent.setBounds(130, 80, 100, 20);
panelMain.add(btnEditStudent);
JButton btnDeleteStudent = new JButton("Delete Student");
btnDeleteStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectedIndex = table.getSelectedRow();
if (selectedIndex >= 0) {
students.remove(selectedIndex);
saveData();
updateTable();
} else {
JOptionPane.showMessageDialog(frame, "Please select a student!");
}
}
});
btnDeleteStudent.setBounds(240, 80, 100, 20);
panelMain.add(btnDeleteStudent);
JButton btnSortByName = new JButton("Sort by Name");
btnSortByName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collections.sort(students, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
updateTable();
}
});
btnSortByName.setBounds(20, 110, 120, 20);
panelMain.add(btnSortByName);
JButton btnSortByGrade = new JButton("Sort by Grade");
btnSortByGrade.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collections.sort(students, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
double g1 = s1.getTotalGrade();
double g2 = s2.getTotalGrade();
if (g1 < g2) {
return 1;
} else if (g1 > g2) {
return -1;
} else {
return 0;
}
}
});
updateTable();
}
});
btnSortByGrade.setBounds(150, 110, 120, 20);
panelMain.add(btnSortByGrade);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 140, 520, 180);
panelMain.add(scrollPane);
table = new JTable();
model = new DefaultTableModel(new Object[][] {}, new String[] { "Name" });
for (Course course : courses) {
model.addColumn(course.getName());
}
table.setModel(model);
scrollPane.setViewportView(table);
// Set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Load data from file
*/
private void loadData() {
students = new ArrayList<Student>();
courses = new ArrayList<Course>();
file = new File("data.txt");
if (file.exists()) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 1) {
courses.add(new Course(parts[0]));
} else {
Student student = new Student(parts[0]);
for (int i = 1; i < parts.length; i += 2) {
Course course = courses.get(i / 2);
double grade = Double.parseDouble(parts[i + 1]);
student.addGrade(course, grade);
}
students.add(student);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
courses.add(new Course("Math"));
courses.add(new Course("English"));
courses.add(new Course("Science"));
}
}
/**
* Save data to file
*/
private void saveData() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (Course course : courses) {
writer.write(course.getName());
writer.newLine();
}
for (Student student : students) {
writer.write(student.getName());
for (Course course : courses) {
double grade = student.getGrade(course);
writer.write("," + course.getName() + "," + grade);
}
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Update table with data
*/
private void updateTable() {
model.setRowCount(0);
for (Student student : students) {
Object[] row = new Object[courses.size() + 1];
row[0] = student.getName();
for (int i = 0; i < courses.size(); i++) {
row[i + 1] = student.getGrade(courses.get(i));
}
model.addRow(row);
}
}
/**
* Open main panel
*/
private void openMainPanel() {
frame.getContentPane().getComponent(0).setVisible(false);
frame.getContentPane().getComponent(1).setVisible(true);
updateTable();
}
/**
* Course class
*/
private class Course {
private String name;
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* Student class
*/
private class Student {
private String name;
private List<Double> grades;
public Student(String name) {
this.name = name;
grades = new ArrayList<Double>();
for (int i = 0; i < courses.size(); i++) {
grades.add(0.0);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGrade(Course course) {
return grades.get(courses.indexOf(course));
}
public void addGrade(Course course, double grade) {
grades.set(courses.indexOf(course), grade);
}
public void setGrade(Course course, double grade) {
addGrade(course, grade);
}
public double getTotalGrade() {
double total = 0;
for (Double grade : grades) {
total += grade;
}
return total;
}
}
}
```
这个代码实现了以下功能:
1. 有图形界面。
2. 登录时输入用户密码,且进入时打开新的界面。
3. 能够录入、查询、修改、删除课程。
4. 能够录入、查询、修改、删除学生某个课程的成绩。
5. 能够持久化数据,将数据保存到文件中。