写一个学生管理系统的图形化界面部分,用java语言实现
时间: 2024-03-24 07:41:12 浏览: 63
好的,我可以为您提供一些关于学生管理系统图形化界面部分的Java代码示例。以下是一个简单的学生信息管理系统的示例代码。请注意,这只是一个基础的示例,您可以根据您的需求进行修改和扩展。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JTextField idField, nameField, ageField, genderField, majorField;
private JButton addButton, deleteButton, updateButton, searchButton;
private JTextArea displayArea;
public StudentManagementSystem() {
setTitle("学生信息管理系统");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建面板,用于显示学生信息
JPanel infoPanel = new JPanel(new GridLayout(6, 2));
infoPanel.add(new JLabel("学号:"));
idField = new JTextField(10);
infoPanel.add(idField);
infoPanel.add(new JLabel("姓名:"));
nameField = new JTextField(10);
infoPanel.add(nameField);
infoPanel.add(new JLabel("年龄:"));
ageField = new JTextField(10);
infoPanel.add(ageField);
infoPanel.add(new JLabel("性别:"));
genderField = new JTextField(10);
infoPanel.add(genderField);
infoPanel.add(new JLabel("专业:"));
majorField = new JTextField(10);
infoPanel.add(majorField);
// 创建按钮面板
JPanel buttonPanel = new JPanel(new FlowLayout());
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);
searchButton = new JButton("查询");
searchButton.addActionListener(this);
buttonPanel.add(searchButton);
// 创建文本区域,用于显示学生信息
displayArea = new JTextArea(10, 40);
displayArea.setEditable(false);
// 将面板和文本区域添加到窗口中
Container container = getContentPane();
container.add(infoPanel, BorderLayout.NORTH);
container.add(buttonPanel, BorderLayout.CENTER);
container.add(new JScrollPane(displayArea), BorderLayout.SOUTH);
}
// 按钮事件处理方法
public void actionPerformed(ActionEvent event) {
if (event.getSource() == addButton) {
// 添加学生信息
String id = idField.getText();
String name = nameField.getText();
String age = ageField.getText();
String gender = genderField.getText();
String major = majorField.getText();
String info = "学号:" + id + "\n姓名:" + name + "\n年龄:" + age + "\n性别:" + gender + "\n专业:" + major + "\n";
displayArea.append(info);
} else if (event.getSource() == deleteButton) {
// 删除学生信息
idField.setText("");
nameField.setText("");
ageField.setText("");
genderField.setText("");
majorField.setText("");
displayArea.setText("");
} else if (event.getSource() == updateButton) {
// 修改学生信息
String id = idField.getText();
String name = nameField.getText();
String age = ageField.getText();
String gender = genderField.getText();
String major = majorField.getText();
String info = "学号:" + id + "\n姓名:" + name + "\n年龄:" + age + "\n性别:" + gender + "\n专业:" + major + "\n";
displayArea.setText(info);
} else if (event.getSource() == searchButton) {
// 查询学生信息
String id = idField.getText();
String name = nameField.getText();
String age = ageField.getText();
String gender = genderField.getText();
String major = majorField.getText();
String info = "学号:" + id + "\n姓名:" + name + "\n年龄:" + age + "\n性别:" + gender + "\n专业:" + major + "\n";
displayArea.setText(info);
}
}
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
sms.setVisible(true);
}
}
```
以上代码实现了一个简单的学生信息管理系统的图形化界面,包括添加、删除、修改和查询学生信息的功能。您可以根据您的需求进行扩展和修改。
阅读全文