用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. 能够持久化数据,将数据保存到文件中。

相关推荐

最新推荐

recommend-type

混合云管理平台的研究与实践.docx

混合云管理平台聚焦于异构云资源管理、自动化运维管理、可定制工作流的平台。最终为用户提供一体化的资源管理,自动化资源交付,并为用户提供了方便获取资源的途径。用户可以通过租户自服务门户获取资源并在资源上...
recommend-type

高新兴物联GM800模组Linux系统下ECM&Gobinet功能指导_V1.2-20200806.pdf

5G模组GM800 Linux拨号方式说明,包括ECM拨号,Gobinet拨号,最简示例,编译方式。适合任何Linux系统,Ubuntu、Centos等系统操作都可以正常使用
recommend-type

银河麒麟服务器操作系统 V4 hadoop 软件适配手册

银河麒麟服务器操作系统 V4 hadoop 软件适配手册 包含Hadoop的环境配置,以及文件的配置。
recommend-type

年终工作总结汇报PPTqytp.pptx

年终工作总结汇报PPTqytp.pptx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依