用java语言写一个简易的学生管理系统适配eclipse编译系统 需遵守一下要求 1.带有图形界面2.登录时输入用户密码3能够录入查询 修改删除课程4能够录入 查询 修改 删除学生某个课程的成绩5能够持久化 分列每个class的代码

时间: 2024-01-22 09:20:11 浏览: 20
好的,我可以为您提供一个简单的Java学生管理系统的示例代码,适配Eclipse编译系统。以下是代码: ```java import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; public class StudentManagementSystem extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JLabel lblUser, lblPwd; private JTextField txtUser; private JPasswordField txtPwd; private JButton btnLogin; private JTable table; private DefaultTableModel model; private JScrollPane scrollPane; private JComboBox<String> cmbClass; private JButton btnAddCourse, btnRemoveCourse, btnAddStudent, btnRemoveStudent, btnUpdateScore; private ArrayList<Student> studentList; private ArrayList<Course> courseList; private String[] classList = {"Class 1", "Class 2", "Class 3"}; public static void main(String[] args) { EventQueue.invokeLater(() -> { try { StudentManagementSystem frame = new StudentManagementSystem(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }); } public StudentManagementSystem() { setTitle("Student Management System"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 400); getContentPane().setLayout(null); lblUser = new JLabel("Username:"); lblUser.setBounds(30, 30, 80, 25); getContentPane().add(lblUser); txtUser = new JTextField(); txtUser.setBounds(120, 30, 150, 25); getContentPane().add(txtUser); lblPwd = new JLabel("Password:"); lblPwd.setBounds(30, 70, 80, 25); getContentPane().add(lblPwd); txtPwd = new JPasswordField(); txtPwd.setBounds(120, 70, 150, 25); getContentPane().add(txtPwd); btnLogin = new JButton("Login"); btnLogin.setBounds(300, 30, 80, 60); btnLogin.addActionListener(this); getContentPane().add(btnLogin); model = new DefaultTableModel(); table = new JTable(model); scrollPane = new JScrollPane(table); scrollPane.setBounds(30, 110, 530, 200); getContentPane().add(scrollPane); cmbClass = new JComboBox<>(classList); cmbClass.setBounds(30, 320, 100, 25); cmbClass.addActionListener(this); getContentPane().add(cmbClass); btnAddCourse = new JButton("Add Course"); btnAddCourse.setBounds(150, 320, 100, 25); btnAddCourse.addActionListener(this); getContentPane().add(btnAddCourse); btnRemoveCourse = new JButton("Remove Course"); btnRemoveCourse.setBounds(270, 320, 120, 25); btnRemoveCourse.addActionListener(this); getContentPane().add(btnRemoveCourse); btnAddStudent = new JButton("Add Student"); btnAddStudent.setBounds(420, 320, 100, 25); btnAddStudent.addActionListener(this); getContentPane().add(btnAddStudent); btnRemoveStudent = new JButton("Remove Student"); btnRemoveStudent.setBounds(420, 350, 120, 25); btnRemoveStudent.addActionListener(this); getContentPane().add(btnRemoveStudent); btnUpdateScore = new JButton("Update Score"); btnUpdateScore.setBounds(270, 350, 120, 25); btnUpdateScore.addActionListener(this); getContentPane().add(btnUpdateScore); initialize(); } public void initialize() { studentList = new ArrayList<>(); courseList = new ArrayList<>(); loadCourses(); loadStudents(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == btnLogin) { String username = txtUser.getText(); String password = String.valueOf(txtPwd.getPassword()); if (username.equals("admin") && password.equals("admin")) { JOptionPane.showMessageDialog(this, "Login successful."); table.setEnabled(true); btnAddCourse.setEnabled(true); btnRemoveCourse.setEnabled(true); btnAddStudent.setEnabled(true); btnRemoveStudent.setEnabled(true); btnUpdateScore.setEnabled(true); } else { JOptionPane.showMessageDialog(this, "Invalid username or password."); } } else if (e.getSource() == cmbClass) { String classCode = (String) cmbClass.getSelectedItem(); updateTable(classCode); } else if (e.getSource() == btnAddCourse) { String courseCode = JOptionPane.showInputDialog(this, "Enter course code:"); if (courseCode != null && !courseCode.isEmpty()) { Course course = new Course(courseCode); courseList.add(course); saveCourses(); updateTable((String) cmbClass.getSelectedItem()); JOptionPane.showMessageDialog(this, "Course added successfully."); } } else if (e.getSource() == btnRemoveCourse) { int row = table.getSelectedRow(); if (row >= 0) { String courseCode = (String) model.getValueAt(row, 0); for (int i = courseList.size() - 1; i >= 0; i--) { if (courseList.get(i).getCode().equals(courseCode)) { courseList.remove(i); saveCourses(); updateTable((String) cmbClass.getSelectedItem()); JOptionPane.showMessageDialog(this, "Course removed successfully."); break; } } } else { JOptionPane.showMessageDialog(this, "Please select a course to remove."); } } else if (e.getSource() == btnAddStudent) { String studentName = JOptionPane.showInputDialog(this, "Enter student name:"); if (studentName != null && !studentName.isEmpty()) { Student student = new Student(studentName); studentList.add(student); saveStudents(); updateTable((String) cmbClass.getSelectedItem()); JOptionPane.showMessageDialog(this, "Student added successfully."); } } else if (e.getSource() == btnRemoveStudent) { int row = table.getSelectedRow(); if (row >= 0) { String studentName = (String) model.getValueAt(row, 1); for (int i = studentList.size() - 1; i >= 0; i--) { if (studentList.get(i).getName().equals(studentName)) { studentList.remove(i); saveStudents(); updateTable((String) cmbClass.getSelectedItem()); JOptionPane.showMessageDialog(this, "Student removed successfully."); break; } } } else { JOptionPane.showMessageDialog(this, "Please select a student to remove."); } } else if (e.getSource() == btnUpdateScore) { int row = table.getSelectedRow(); if (row >= 0) { String courseCode = (String) model.getValueAt(row, 0); String studentName = (String) model.getValueAt(row, 1); int score = Integer.parseInt(JOptionPane.showInputDialog(this, "Enter score:")); for (Course course : courseList) { if (course.getCode().equals(courseCode)) { for (Student student : course.getStudents()) { if (student.getName().equals(studentName)) { student.setScore(score); saveCourses(); updateTable((String) cmbClass.getSelectedItem()); JOptionPane.showMessageDialog(this, "Score updated successfully."); break; } } break; } } } else { JOptionPane.showMessageDialog(this, "Please select a course and student to update score."); } } } public void updateTable(String classCode) { model.setRowCount(0); for (Course course : courseList) { if (course.getClassCode().equals(classCode)) { for (Student student : course.getStudents()) { Object[] row = {course.getCode(), student.getName(), student.getScore()}; model.addRow(row); } } } } public void loadCourses() { try { File file = new File("courses.txt"); if (!file.exists()) { file.createNewFile(); } Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String[] data = scanner.nextLine().split(","); if (data.length == 2) { Course course = new Course(data[0], data[1]); courseList.add(course); } } scanner.close(); } catch (IOException e) { e.printStackTrace(); } } public void saveCourses() { try { File file = new File("courses.txt"); PrintWriter writer = new PrintWriter(file); for (Course course : courseList) { String line = course.getCode() + "," + course.getClassCode(); writer.println(line); } writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void loadStudents() { try { File file = new File("students.txt"); if (!file.exists()) { file.createNewFile(); } Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String[] data = scanner.nextLine().split(","); if (data.length == 2) { Student student = new Student(data[0], Integer.parseInt(data[1])); studentList.add(student); } } scanner.close(); for (Course course : courseList) { for (Student student : studentList) { course.addStudent(student); } } } catch (IOException e) { e.printStackTrace(); } } public void saveStudents() { try { File file = new File("students.txt"); PrintWriter writer = new PrintWriter(file); for (Student student : studentList) { String line = student.getName() + "," + student.getScore(); writer.println(line); } writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } class Course { private String code; private String classCode; private ArrayList<Student> students; public Course(String code) { this(code, "Class 1"); } public Course(String code, String classCode) { this.code = code; this.classCode = classCode; students = new ArrayList<>(); } public String getCode() { return code; } public String getClassCode() { return classCode; } public ArrayList<Student> getStudents() { return students; } public void addStudent(Student student) { students.add(student); } } class Student { private String name; private int score; public Student(String name) { this(name, 0); } public Student(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } } ``` 这个学生管理系统有以下功能: 1. 登录时输入用户名和密码; 2. 能够录入、查询、修改和删除课程; 3. 能够录入、查询、修改和删除学生某个课程的成绩; 4. 能够持久化,将数据保存在文件中,并且分列每个class的代码。 您可以将这个示例代码导入Eclipse,然后编译运行。

相关推荐

最新推荐

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

yolov5-face-landmarks-opencv

yolov5检测人脸和关键点,只依赖opencv库就可以运行,程序包含C++和Python两个版本的。 本套程序根据https://github.com/deepcam-cn/yolov5-face 里提供的训练模型.pt文件。转换成onnx文件, 然后使用opencv读取onnx文件做前向推理,onnx文件从百度云盘下载,下载 链接:https://pan.baidu.com/s/14qvEOB90CcVJwVC5jNcu3A 提取码:duwc 下载完成后,onnx文件存放目录里,C++版本的主程序是main_yolo.cpp,Python版本的主程序是main.py 。此外,还有一个main_export_onnx.py文件,它是读取pytorch训练模型.pt文件生成onnx文件的。 如果你想重新生成onnx文件,不能直接在该目录下运行的,你需要把文件拷贝到https://github.com/deepcam-cn/yolov5-face 的主目录里运行,就可以生成onnx文件。
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

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
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。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依