java 用户界面设计 Student对象信息管理代码
时间: 2023-11-25 17:05:50 浏览: 79
java 学生管理系统 学生管理系统界面设计
以下是一个简单的Java用户界面设计示例,用于管理Student对象的信息:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StudentGUI extends JFrame implements ActionListener {
private JTextField nameField, idField, majorField;
private JButton addButton, removeButton, clearButton;
private JTextArea displayArea;
private StudentList studentList;
public StudentGUI() {
super("Student Information System");
// 创建界面组件
nameField = new JTextField(20);
idField = new JTextField(20);
majorField = new JTextField(20);
addButton = new JButton("Add");
removeButton = new JButton("Remove");
clearButton = new JButton("Clear");
displayArea = new JTextArea(10, 30);
JScrollPane scrollPane = new JScrollPane(displayArea);
// 将组件添加到界面
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("Name:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("ID:"));
inputPanel.add(idField);
inputPanel.add(new JLabel("Major:"));
inputPanel.add(majorField);
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(clearButton);
JPanel displayPanel = new JPanel();
displayPanel.add(scrollPane);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(displayPanel, BorderLayout.SOUTH);
// 添加事件监听器
addButton.addActionListener(this);
removeButton.addActionListener(this);
clearButton.addActionListener(this);
// 初始化StudentList对象
studentList = new StudentList();
// 设置界面属性
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// 处理按钮事件
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String id = idField.getText();
String major = majorField.getText();
if (e.getSource() == addButton) {
// 添加学生信息
Student student = new Student(name, id, major);
studentList.add(student);
displayArea.append("Added: " + student + "\n");
} else if (e.getSource() == removeButton) {
// 移除学生信息
Student student = new Student(name, id, major);
if (studentList.remove(student)) {
displayArea.append("Removed: " + student + "\n");
} else {
displayArea.append("Not found: " + student + "\n");
}
} else if (e.getSource() == clearButton) {
// 清空显示区域
displayArea.setText("");
}
}
// Student类
private 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 toString() {
return name + " (" + id + ", " + major + ")";
}
}
// StudentList类
private class StudentList {
private java.util.List<Student> students;
public StudentList() {
students = new java.util.ArrayList<Student>();
}
public void add(Student student) {
students.add(student);
}
public boolean remove(Student student) {
return students.remove(student);
}
}
// 测试代码
public static void main(String[] args) {
new StudentGUI();
}
}
```
该程序包括一个StudentGUI类,该类继承自JFrame,并实现了ActionListener接口。它包括一个学生信息的输入面板,三个按钮(添加、删除和清空),以及一个学生信息的显示区域。在添加和删除学生信息时,程序将使用StudentList类来管理学生列表。
阅读全文