用java写一个Student对象信息,声明StudentJFrame框架类和StudentJPanel对象信息面板,要求①增加查找组合框数据项,提供Person类按省份、城市等多种查找依据提供按姓氏、出生年份等进行模糊查询。② 使用一种日期组件 输入 生日期。③设置省份和城市组合框为可编辑的,将输入的省份和城市字符串添加到各组合框数据项。要求需要用到JTable组件,用java写出
时间: 2024-03-18 08:42:10 浏览: 138
java 语言输入学生资料
好的,以下是用Java写的一个实现。
首先是Student类,包括姓名、性别、生日、省份、城市等信息:
```
public class Student {
private String name;
private String gender;
private LocalDate birthday;
private String province;
private String city;
public Student(String name, String gender, LocalDate birthday, String province, String city) {
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.province = province;
this.city = city;
}
// getter and setter methods
}
```
然后是StudentJPanel类,包括一个组合框、一个文本框、一个日期组件和两个可编辑的省份和城市组合框:
```
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class StudentJPanel extends JPanel {
private JComboBox<String> searchComboBox;
private JTextField searchTextField;
private JTable studentTable;
private DefaultTableModel tableModel;
private JTextField nameTextField;
private JComboBox<String> genderComboBox;
private JSpinner birthdaySpinner;
private JComboBox<String> provinceComboBox;
private JComboBox<String> cityComboBox;
private List<Student> studentList = new ArrayList<>();
public StudentJPanel() {
setLayout(new BorderLayout());
// search panel
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
searchComboBox = new JComboBox<>(new String[]{"省份", "城市", "姓氏", "出生年份"});
searchTextField = new JTextField(20);
JButton searchButton = new JButton("查询");
searchButton.addActionListener(e -> search());
searchPanel.add(searchComboBox);
searchPanel.add(searchTextField);
searchPanel.add(searchButton);
add(searchPanel, BorderLayout.NORTH);
// student table
tableModel = new DefaultTableModel(new String[]{"姓名", "性别", "生日", "省份", "城市"}, 0);
studentTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(studentTable);
add(scrollPane, BorderLayout.CENTER);
// add panel
JPanel addPanel = new JPanel(new GridLayout(0, 2));
nameTextField = new JTextField();
genderComboBox = new JComboBox<>(new String[]{"男", "女"});
birthdaySpinner = new JSpinner(new SpinnerDateModel());
JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(birthdaySpinner, "yyyy-MM-dd");
birthdaySpinner.setEditor(dateEditor);
provinceComboBox = new JComboBox<>();
cityComboBox = new JComboBox<>();
provinceComboBox.setEditable(true);
cityComboBox.setEditable(true);
JButton addButton = new JButton("添加");
addButton.addActionListener(e -> addStudent());
addPanel.add(new JLabel("姓名"));
addPanel.add(nameTextField);
addPanel.add(new JLabel("性别"));
addPanel.add(genderComboBox);
addPanel.add(new JLabel("生日"));
addPanel.add(birthdaySpinner);
addPanel.add(new JLabel("省份"));
addPanel.add(provinceComboBox);
addPanel.add(new JLabel("城市"));
addPanel.add(cityComboBox);
addPanel.add(new JLabel());
addPanel.add(addButton);
add(addPanel, BorderLayout.SOUTH);
}
private void search() {
String searchBy = (String) searchComboBox.getSelectedItem();
String keyword = searchTextField.getText().trim();
tableModel.setRowCount(0);
for (Student student : studentList) {
boolean match = false;
switch (searchBy) {
case "省份":
if (student.getProvince().equals(keyword)) {
match = true;
}
break;
case "城市":
if (student.getCity().equals(keyword)) {
match = true;
}
break;
case "姓氏":
if (student.getName().startsWith(keyword)) {
match = true;
}
break;
case "出生年份":
if (String.valueOf(student.getBirthday().getYear()).startsWith(keyword)) {
match = true;
}
break;
}
if (match) {
tableModel.addRow(new Object[]{student.getName(), student.getGender(), student.getBirthday(),
student.getProvince(), student.getCity()});
}
}
}
private void addStudent() {
String name = nameTextField.getText().trim();
String gender = (String) genderComboBox.getSelectedItem();
LocalDate birthday = ((SpinnerDateModel) birthdaySpinner.getModel()).getDate().toInstant()
.atZone(java.time.ZoneId.systemDefault()).toLocalDate();
String province = (String) provinceComboBox.getSelectedItem();
String city = (String) cityComboBox.getSelectedItem();
Student student = new Student(name, gender, birthday, province, city);
studentList.add(student);
tableModel.addRow(new Object[]{student.getName(), student.getGender(), student.getBirthday(),
student.getProvince(), student.getCity()});
provinceComboBox.addItem(province);
cityComboBox.addItem(city);
}
}
```
最后是StudentJFrame类,包括一个StudentJPanel对象信息面板:
```
import javax.swing.*;
public class StudentJFrame extends JFrame {
public StudentJFrame() {
setTitle("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
StudentJPanel studentJPanel = new StudentJPanel();
setContentPane(studentJPanel);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem saveMenuItem = new JMenuItem("保存");
saveMenuItem.addActionListener(e -> save());
JMenuItem exitMenuItem = new JMenuItem("退出");
exitMenuItem.addActionListener(e -> System.exit(0));
fileMenu.add(saveMenuItem);
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
private void save() {
// save student information to file
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new StudentJFrame().setVisible(true));
}
}
```
可以看到,实现中使用了JTable组件来展示学生信息,使用了一个组合框和一个文本框来进行查询操作,使用了一个日期组件来输入学生的生日信息,使用了两个可编辑的省份和城市组合框来输入学生的省份和城市信息。同时,添加学生信息的操作也很简单,只需要填写相应的信息即可。
希望这个实现可以对你有所帮助!
阅读全文