Java编写简易学生信息管理系统具有界面
时间: 2024-01-03 16:04:53 浏览: 115
用java写的简单的学生信息管理系统
5星 · 资源好评率100%
好的,我可以为您提供一些帮助。首先,您需要选择一个Java GUI框架,例如Swing或JavaFX。然后,您可以编写代码来创建应用程序窗口和各种组件,例如按钮、文本框和表格。接下来,您需要实现数据存储和管理,可以使用文件或数据库来存储学生信息。最后,您需要编写代码来处理用户输入并更新数据。以下是一个简单的示例代码,可以帮助您入门:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JTextField nameField, idField, majorField;
private JButton addButton, deleteButton, updateButton;
private JTable table;
private StudentTableModel model;
public StudentManagementSystem() {
super("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("姓名"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("学号"));
idField = new JTextField();
inputPanel.add(idField);
inputPanel.add(new JLabel("专业"));
majorField = new JTextField();
inputPanel.add(majorField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
addButton = new JButton("添加");
addButton.addActionListener(this);
buttonPanel.add(addButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
buttonPanel.add(deleteButton);
updateButton = new JButton("更新");
updateButton.addActionListener(this);
buttonPanel.add(updateButton);
model = new StudentTableModel();
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
add(scrollPane, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
String id = idField.getText();
String major = majorField.getText();
model.addStudent(new Student(name, id, major));
} else if (e.getSource() == deleteButton) {
int row = table.getSelectedRow();
if (row >= 0) {
model.deleteStudent(row);
}
} else if (e.getSource() == updateButton) {
int row = table.getSelectedRow();
if (row >= 0) {
String name = nameField.getText();
String id = idField.getText();
String major = majorField.getText();
model.updateStudent(row, new Student(name, id, major));
}
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
}
}
class Student {
private String name;
private String id;
private String major;
public Student(String name, String id, String major) {
this.name = name;
this.id = id;
this.major = major;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getMajor() {
return major;
}
}
class StudentTableModel extends AbstractTableModel {
private ArrayList<Student> students;
public StudentTableModel() {
students = new ArrayList<Student>();
students.add(new Student("张三", "1001", "计算机科学"));
students.add(new Student("李四", "1002", "软件工程"));
students.add(new Student("王五", "1003", "信息安全"));
}
public void addStudent(Student student) {
students.add(student);
fireTableDataChanged();
}
public void deleteStudent(int row) {
students.remove(row);
fireTableDataChanged();
}
public void updateStudent(int row, Student student) {
students.set(row, student);
fireTableDataChanged();
}
public int getRowCount() {
return students.size();
}
public int getColumnCount() {
return 3;
}
public Object getValueAt(int rowIndex, int columnIndex) {
Student student = students.get(rowIndex);
switch (columnIndex) {
case 0:
return student.getName();
case 1:
return student.getId();
case 2:
return student.getMajor();
default:
return null;
}
}
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0:
return "姓名";
case 1:
return "学号";
case 2:
return "专业";
default:
return null;
}
}
}
```
这是一个简单的学生信息管理系统,包括添加、删除和更新学生信息的功能,并使用表格显示学生信息。您可以根据自己的需要进行修改和扩展。
阅读全文