运用Java 实现学生管理系统,该系统具备添加元素、删除元素、修改元素、查询元素的功能。 要求进行界面设计,通过输入数字,调用方法实现对应功能。
时间: 2023-06-11 11:06:50 浏览: 102
好的,那我来为您介绍一下实现学生管理系统的基本思路:
1. 定义学生类,包括学生ID、姓名、年龄、性别、成绩等属性;
2. 定义学生管理类,包括添加学生、删除学生、修改学生、查询学生等方法;
3. 利用Java Swing组件实现学生管理系统的界面设计,包括添加学生的文本框、按钮、表格等界面元素;
4. 在界面中添加事件监听器,根据用户输入的数字调用对应的方法实现相应的功能。
下面是一个简单的示例代码,您可以参考一下:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JLabel titleLabel;
private JLabel idLabel;
private JLabel nameLabel;
private JLabel ageLabel;
private JLabel genderLabel;
private JLabel gradeLabel;
private JTextField idField;
private JTextField nameField;
private JTextField ageField;
private JTextField genderField;
private JTextField gradeField;
private JButton addButton;
private JButton deleteButton;
private JButton updateButton;
private JButton searchButton;
private JTable table;
private DefaultTableModel model;
public StudentManagementSystem() {
super("学生管理系统");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 标题
titleLabel = new JLabel("学生管理系统", JLabel.CENTER);
add(titleLabel, BorderLayout.NORTH);
// 表格
model = new DefaultTableModel();
model.addColumn("学号");
model.addColumn("姓名");
model.addColumn("年龄");
model.addColumn("性别");
model.addColumn("成绩");
table = new JTable(model);
add(new JScrollPane(table), BorderLayout.CENTER);
// 添加学生
JPanel addPanel = new JPanel(new GridLayout(6, 2));
idLabel = new JLabel("学号:");
idField = new JTextField();
nameLabel = new JLabel("姓名:");
nameField = new JTextField();
ageLabel = new JLabel("年龄:");
ageField = new JTextField();
genderLabel = new JLabel("性别:");
genderField = new JTextField();
gradeLabel = new JLabel("成绩:");
gradeField = new JTextField();
addButton = new JButton("添加");
addButton.addActionListener(this);
addPanel.add(idLabel);
addPanel.add(idField);
addPanel.add(nameLabel);
addPanel.add(nameField);
addPanel.add(ageLabel);
addPanel.add(ageField);
addPanel.add(genderLabel);
addPanel.add(genderField);
addPanel.add(gradeLabel);
addPanel.add(gradeField);
addPanel.add(new JPanel());
addPanel.add(addButton);
add(addPanel, BorderLayout.SOUTH);
// 删除学生
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
add(deleteButton, BorderLayout.WEST);
// 修改学生
updateButton = new JButton("修改");
updateButton.addActionListener(this);
add(updateButton, BorderLayout.EAST);
// 查询学生
searchButton = new JButton("查询");
searchButton.addActionListener(this);
add(searchButton, BorderLayout.SOUTH);
setVisible(true);
}
/**
* 添加学生
*/
private void addStudent() {
String id = idField.getText();
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = genderField.getText();
double grade = Double.parseDouble(gradeField.getText());
Object[] row = { id, name, age, gender, grade };
model.addRow(row);
}
/**
* 删除学生
*/
private void deleteStudent() {
int row = table.getSelectedRow();
if (row >= 0) {
model.removeRow(row);
} else {
JOptionPane.showMessageDialog(this, "请选择要删除的学生");
}
}
/**
* 修改学生
*/
private void updateStudent() {
int row = table.getSelectedRow();
if (row >= 0) {
String id = idField.getText();
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = genderField.getText();
double grade = Double.parseDouble(gradeField.getText());
table.setValueAt(id, row, 0);
table.setValueAt(name, row, 1);
table.setValueAt(age, row, 2);
table.setValueAt(gender, row, 3);
table.setValueAt(grade, row, 4);
} else {
JOptionPane.showMessageDialog(this, "请选择要修改的学生");
}
}
/**
* 查询学生
*/
private void searchStudent() {
String keyword = JOptionPane.showInputDialog(this, "请输入要查询的关键字");
if (keyword != null && !keyword.trim().equals("")) {
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
String value = model.getValueAt(i, j).toString();
if (value.contains(keyword)) {
table.setRowSelectionInterval(i, i);
return;
}
}
}
JOptionPane.showMessageDialog(this, "没有找到符合条件的学生");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addStudent();
} else if (e.getSource() == deleteButton) {
deleteStudent();
} else if (e.getSource() == updateButton) {
updateStudent();
} else if (e.getSource() == searchButton) {
searchStudent();
}
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
```
以上代码仅供参考,您可以根据需求进行修改和完善。
阅读全文