包含studentjpanel对象信息面板,系与专业成员变量改用组合框编辑,增加两者关联,存储输入元素,学号按系与专业自动编号,按学号查找和排序
时间: 2024-03-19 11:46:03 浏览: 59
好的,根据您的需求,我可以为您提供一个简单的示例代码,来实现学生对象信息面板的管理,并且包含系与专业成员变量的组合框编辑,以及学号按照系和专业自动编号,支持按照学号进行查找和排序的功能。
首先,我们需要定义一个`Student`类,该类包含姓名、学号、年龄、性别、系别、专业和成绩等属性,如下所示:
```
public class Student {
private String name;
private String id;
private int age;
private String gender;
private String department;
private String major;
private double score;
// 构造方法
public Student(String name, int age, String gender, String department, String major, double score) {
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.major = major;
this.score = score;
this.id = generateId(department, major);
}
// 自动生成学号方法
private String generateId(String department, String major) {
// 假设学号格式为:系别代码(2位)+专业代码(2位)+4位数字编号
String idPrefix = department.substring(0, 2) + major.substring(0, 2);
// 获取当前系和专业的学生人数
int count = StudentDB.getStudentCount(department, major);
return idPrefix + String.format("%04d", count + 1);
}
// 封装属性的getter和setter方法
// ...
}
```
接下来,我们需要定义一个`StudentDB`类,用于存储学生对象信息,并且提供按照学号查找和排序的方法,如下所示:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StudentDB {
private static List<Student> students = new ArrayList<>();
// 添加学生
public static void addStudent(Student student) {
students.add(student);
}
// 按照学号查找学生
public static Student findStudentById(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
// 按照学号排序
public static void sortByStudentId() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getId().compareTo(s2.getId());
}
});
}
// 获取某个系和专业的学生人数
public static int getStudentCount(String department, String major) {
int count = 0;
for (Student student : students) {
if (student.getDepartment().equals(department) && student.getMajor().equals(major)) {
count++;
}
}
return count;
}
}
```
最后,我们需要定义一个`StudentPanel`类,用于管理学生对象信息面板,并且包含系与专业成员变量的组合框编辑,如下所示:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentPanel extends JPanel {
private JTextField nameField;
private JComboBox<String> departmentBox;
private JComboBox<String> majorBox;
private JTextField ageField;
private JComboBox<String> genderBox;
private JTextField scoreField;
private JButton addButton;
private JButton searchButton;
private JButton sortButton;
public StudentPanel() {
// 初始化面板布局
setLayout(new GridLayout(6, 2, 5, 5));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// 初始化面板元素
nameField = new JTextField();
departmentBox = new JComboBox<>(new String[]{"计算机科学与技术", "软件工程", "信息安全"});
majorBox = new JComboBox<>(new String[]{"计算机科学与技术", "软件工程", "信息安全"});
ageField = new JTextField();
genderBox = new JComboBox<>(new String[]{"男", "女"});
scoreField = new JTextField();
addButton = new JButton("添加");
searchButton = new JButton("查找");
sortButton = new JButton("排序");
// 添加面板元素
add(new JLabel("姓名:"));
add(nameField);
add(new JLabel("系别:"));
add(departmentBox);
add(new JLabel("专业:"));
add(majorBox);
add(new JLabel("年龄:"));
add(ageField);
add(new JLabel("性别:"));
add(genderBox);
add(new JLabel("成绩:"));
add(scoreField);
add(addButton);
add(searchButton);
add(sortButton);
// 添加按钮点击事件
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = (String) genderBox.getSelectedItem();
String department = (String) departmentBox.getSelectedItem();
String major = (String) majorBox.getSelectedItem();
double score = Double.parseDouble(scoreField.getText());
Student student = new Student(name, age, gender, department, major, score);
StudentDB.addStudent(student);
JOptionPane.showMessageDialog(StudentPanel.this, "添加成功");
}
});
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = JOptionPane.showInputDialog(StudentPanel.this, "请输入学号:");
Student student = StudentDB.findStudentById(id);
if (student != null) {
JOptionPane.showMessageDialog(StudentPanel.this,
"姓名:" + student.getName() + "\n" +
"学号:" + student.getId() + "\n" +
"年龄:" + student.getAge() + "\n" +
"性别:" + student.getGender() + "\n" +
"系别:" + student.getDepartment() + "\n" +
"专业:" + student.getMajor() + "\n" +
"成绩:" + student.getScore());
} else {
JOptionPane.showMessageDialog(StudentPanel.this, "没有找到该学生");
}
}
});
sortButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StudentDB.sortByStudentId();
JOptionPane.showMessageDialog(StudentPanel.this, "排序成功");
}
});
}
}
```
这样,我们就完成了一个包含学生对象信息面板,可以进行系与专业成员变量改用组合框编辑,增加两者关联,存储输入元素,学号按系与专业自动编号,按学号查找和排序的Java程序开发。您可以根据需要进行修改和调整。
阅读全文