声明StudentJFrame框架类,系与专业成员变量改用组合框编辑,储存输入元素,学号按系与专业自动编号,按学号查找和排序
时间: 2024-03-20 20:40:10 浏览: 76
好的,基于你的要求,我可以提供以下声明StudentJFrame框架类的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class StudentJFrame extends JFrame {
private JLabel idLabel, nameLabel, majorLabel;
private JTextField nameField;
private JComboBox<String> deptCombo, majorCombo;
private JButton addButton, searchButton, sortButton;
private JTextArea resultArea;
private ArrayList<Student> studentList;
public StudentJFrame() {
// Set up the GUI components
idLabel = new JLabel("学号:");
nameLabel = new JLabel("姓名:");
majorLabel = new JLabel("专业:");
nameField = new JTextField(20);
deptCombo = new JComboBox<>(new String[]{"计算机系", "电子系", "通信系"});
majorCombo = new JComboBox<>();
addButton = new JButton("添加");
searchButton = new JButton("查找");
sortButton = new JButton("排序");
resultArea = new JTextArea(10, 30);
// Add the components to the content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(idLabel);
contentPane.add(new JLabel("自动生成"));
contentPane.add(nameLabel);
contentPane.add(nameField);
contentPane.add(majorLabel);
contentPane.add(deptCombo);
contentPane.add(majorCombo);
contentPane.add(addButton);
contentPane.add(searchButton);
contentPane.add(sortButton);
contentPane.add(new JScrollPane(resultArea));
// Set up the event handlers
addButton.addActionListener(e -> addStudent());
searchButton.addActionListener(e -> searchStudent());
sortButton.addActionListener(e -> sortStudents());
// Set up the frame
setTitle("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
// Initialize the student list
studentList = new ArrayList<>();
}
private void addStudent() {
String name = nameField.getText();
String dept = (String) deptCombo.getSelectedItem();
String major = (String) majorCombo.getSelectedItem();
String id = generateId(dept, major);
Student student = new Student(id, name, dept, major);
studentList.add(student);
resultArea.append("添加成功:" + student + "\n");
}
private void searchStudent() {
String id = JOptionPane.showInputDialog("请输入要查找的学号:");
for (Student student : studentList) {
if (student.getId().equals(id)) {
resultArea.setText(student.toString());
return;
}
}
resultArea.setText("找不到该学生!");
}
private void sortStudents() {
Collections.sort(studentList);
resultArea.setText("排序成功!\n");
for (Student student : studentList) {
resultArea.append(student + "\n");
}
}
private String generateId(String dept, String major) {
int count = 0;
for (Student student : studentList) {
if (student.getDept().equals(dept) && student.getMajor().equals(major)) {
count++;
}
}
return String.format("%s%02d%02d", dept.substring(0, 2), major.hashCode() % 100, count + 1);
}
private void updateMajorList() {
String dept = (String) deptCombo.getSelectedItem();
switch (dept) {
case "计算机系":
majorCombo.setModel(new DefaultComboBoxModel<>(new String[]{"软件工程", "网络工程", "信息安全"}));
break;
case "电子系":
majorCombo.setModel(new DefaultComboBoxModel<>(new String[]{"电子信息科学与技术", "光电信息科学与工程", "电子商务"}));
break;
case "通信系":
majorCombo.setModel(new DefaultComboBoxModel<>(new String[]{"通信工程", "信息工程", "电信管理"}));
break;
}
}
private class Student implements Comparable<Student> {
private String id;
private String name;
private String dept;
private String major;
public Student(String id, String name, String dept, String major) {
this.id = id;
this.name = name;
this.dept = dept;
this.major = major;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDept() {
return dept;
}
public String getMajor() {
return major;
}
public String toString() {
return String.format("%s\t%s\t%s\t%s", id, name, dept, major);
}
public int compareTo(Student other) {
return id.compareTo(other.id);
}
}
}
```
这个声明包含了一个名为StudentJFrame的公共类,它扩展了JFrame类。该类包含了一些GUI组件,包括标签、文本框、组合框、按钮和文本区域。它还包含了一个ArrayList<Student>类型的成员变量studentList,用于存储所有添加的学生信息。在构造函数中,我们初始化了GUI组件、设置了事件处理程序,并初始化了studentList。在addStudent()方法中,我们使用输入的信息创建一个新的Student对象,并将其添加到studentList中。在searchStudent()方法中,我们使用JOptionPane.showInputDialog()方法获取用户输入的学号,并在studentList中查找该学生。在sortStudents()方法中,我们使用Collections.sort()方法对studentList进行排序,并在文本区域中显示所有学生的信息。在generateId()方法中,我们使用dept和major参数生成一个新的学号。在updateMajorList()方法中,我们根据deptCombo的选择更新majorCombo的选项。最后,我们定义了一个嵌套类Student,它实现了Comparable<Student>接口,并包含了一个compareTo()方法,用于比较两个学生的学号。
阅读全文