用IDEA设计一个学生信息管理系统代码
时间: 2024-03-05 22:52:43 浏览: 91
学生信息管理系统的代码
以下是一个简单的学生信息管理系统的代码实现,使用Java Swing实现用户界面,使用文件存储学生信息:
1. 学生类
```java
public class Student {
private String name;
private String id;
private String gender;
private int age;
private String className;
public Student(String name, String id, String gender, int age, String className) {
this.name = name;
this.id = id;
this.gender = gender;
this.age = age;
this.className = className;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
```
2. 学生信息管理类
```java
import java.io.*;
import java.util.ArrayList;
public class StudentManager {
private ArrayList<Student> students;
private String fileName;
public StudentManager(String fileName) {
this.fileName = fileName;
students = new ArrayList<>();
loadFromFile();
}
public void addStudent(Student student) {
students.add(student);
saveToFile();
}
public void removeStudent(Student student) {
students.remove(student);
saveToFile();
}
public void updateStudent(Student oldStudent, Student newStudent) {
int index = students.indexOf(oldStudent);
if (index != -1) {
students.set(index, newStudent);
saveToFile();
}
}
public ArrayList<Student> searchStudent(String keyword) {
ArrayList<Student> result = new ArrayList<>();
for (Student student : students) {
if (student.getName().contains(keyword) || student.getGender().contains(keyword) ||
student.getId().contains(keyword) || student.getClassName().contains(keyword)) {
result.add(student);
}
}
return result;
}
public ArrayList<Student> getAllStudents() {
return students;
}
private void loadFromFile() {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) {
students = (ArrayList<Student>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
private void saveToFile() {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName))) {
out.writeObject(students);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 用户界面
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class StudentGUI extends JFrame {
private StudentManager studentManager;
private JTextField nameField;
private JTextField idField;
private JTextField genderField;
private JTextField ageField;
private JTextField classField;
private JTable table;
public StudentGUI(String fileName) {
studentManager = new StudentManager(fileName);
setTitle("学生信息管理系统");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(5, 2));
inputPanel.setBorder(BorderFactory.createTitledBorder("学生信息"));
inputPanel.add(new JLabel("姓名:"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("学号:"));
idField = new JTextField();
inputPanel.add(idField);
inputPanel.add(new JLabel("性别:"));
genderField = new JTextField();
inputPanel.add(genderField);
inputPanel.add(new JLabel("年龄:"));
ageField = new JTextField();
inputPanel.add(ageField);
inputPanel.add(new JLabel("班级:"));
classField = new JTextField();
inputPanel.add(classField);
panel.add(inputPanel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String id = idField.getText();
String gender = genderField.getText();
int age = Integer.parseInt(ageField.getText());
String className = classField.getText();
Student student = new Student(name, id, gender, age, className);
studentManager.addStudent(student);
updateTable();
clearInput();
}
});
buttonPanel.add(addButton);
JButton removeButton = new JButton("删除");
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row != -1) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
String id = (String) model.getValueAt(row, 1);
ArrayList<Student> students = studentManager.getAllStudents();
for (Student student : students) {
if (student.getId().equals(id)) {
studentManager.removeStudent(student);
break;
}
}
updateTable();
}
}
});
buttonPanel.add(removeButton);
JButton updateButton = new JButton("修改");
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row != -1) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
String id = (String) model.getValueAt(row, 1);
ArrayList<Student> students = studentManager.getAllStudents();
for (Student student : students) {
if (student.getId().equals(id)) {
String name = nameField.getText();
String gender = genderField.getText();
int age = Integer.parseInt(ageField.getText());
String className = classField.getText();
Student newStudent = new Student(name, id, gender, age, className);
studentManager.updateStudent(student, newStudent);
break;
}
}
updateTable();
}
}
});
buttonPanel.add(updateButton);
JButton searchButton = new JButton("搜索");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String keyword = JOptionPane.showInputDialog("请输入关键字");
ArrayList<Student> result = studentManager.searchStudent(keyword);
showSearchResult(result);
}
});
buttonPanel.add(searchButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
updateTable();
setContentPane(panel);
setVisible(true);
}
private void updateTable() {
ArrayList<Student> students = studentManager.getAllStudents();
DefaultTableModel model = new DefaultTableModel(new String[]{"姓名", "学号", "性别", "年龄", "班级"}, students.size());
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
model.setValueAt(student.getName(), i, 0);
model.setValueAt(student.getId(), i, 1);
model.setValueAt(student.getGender(), i, 2);
model.setValueAt(student.getAge(), i, 3);
model.setValueAt(student.getClassName(), i, 4);
}
table.setModel(model);
}
private void showSearchResult(ArrayList<Student> result) {
DefaultTableModel model = new DefaultTableModel(new String[]{"姓名", "学号", "性别", "年龄", "班级"}, result.size());
for (int i = 0; i < result.size(); i++) {
Student student = result.get(i);
model.setValueAt(student.getName(), i, 0);
model.setValueAt(student.getId(), i, 1);
model.setValueAt(student.getGender(), i, 2);
model.setValueAt(student.getAge(), i, 3);
model.setValueAt(student.getClassName(), i, 4);
}
table.setModel(model);
}
private void clearInput() {
nameField.setText("");
idField.setText("");
genderField.setText("");
ageField.setText("");
classField.setText("");
}
public static void main(String[] args) {
new StudentGUI("students.dat");
}
}
```
这是一个简单的实现,您可以根据自己的需要进行修改和扩展。
阅读全文