package com.itheima.ui;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class StudentManager {
private JFrame frame;
private JTextField searchField;
private JButton searchButton;
private JButton addButton;
private JTable studentTable;
private DefaultTableModel tableModel;
private List<Student> students = new ArrayList<>();
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
StudentManager window = new StudentManager();
window.createAndShowGUI();
} catch (Exception e) {
e.printStackTrace();
}
});
}
private void createAndShowGUI() {
frame = new JFrame("学生信息管理");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLayout(new BorderLayout());
frame.setLocationRelativeTo(null);
// 创建顶部面板
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
searchField = new JTextField(20);
searchButton = new JButton("查询");
addButton = new JButton("添加");
topPanel.add(searchField);
topPanel.add(searchButton);
topPanel.add(addButton);
frame.add(topPanel, BorderLayout.NORTH);
// 创建表格模型
String[] columns = {"ID", "姓名", "年龄", "性别"};
tableModel = new DefaultTableModel(columns, 0);
studentTable = new JTable(tableModel);
studentTable.setFillsViewportHeight(true);
studentTable.setAutoCreateRowSorter(true);
// 添加右键菜单
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem modifyItem = new JMenuItem("修改");
JMenuItem deleteItem = new JMenuItem("删除");
popupMenu.add(modifyItem);
popupMenu.add(deleteItem);
studentTable.setComponentPopupMenu(popupMenu);
// 添加监听器
modifyItem.addActionListener(e -> showModifyDialog());
deleteItem.addActionListener(e -> showDeleteDialog());
// 创建表格滚动面板
JScrollPane scrollPane = new JScrollPane(studentTable);
frame.add(scrollPane, BorderLayout.CENTER);
// 初始化学生数据
initStudents();
// 添加查询按钮监听器
searchButton.addActionListener(e -> searchStudent());
// 添加添加按钮监听器
addButton.addActionListener(e -> showAddDialog());
// 显示窗口
frame.setVisible(true);
}
private void initStudents() {
students.add(new Student(1, "张三", 20, "男"));
students.add(new Student(2, "李四", 22, "女"));
students.add(new Student(3, "王五", 19, "男"));
for (Student student : students) {
tableModel.addRow(new Object[]{student.getId(), student.getName(), student.getAge(), student.getSex()});
}
}
private void searchStudent() {
String name = searchField.getText().trim();
if (name.isEmpty()) {
JOptionPane.showMessageDialog(frame, "请输入学生姓名!");
return;
}
int row = -1;
for (int i = 0; i < tableModel.getRowCount(); i++) {
if (tableModel.getValueAt(i, 1).equals(name)) {
row = i;
break;
}
}
if (row == -1) {
JOptionPane.showMessageDialog(frame, "未找到该学生!");
} else {
studentTable.setRowSelectionInterval(row, row);
}
}
private void showAddDialog() {
AddDialog addDialog = new AddDialog(frame);
addDialog.setVisible(true);
}
private void showModifyDialog() {
ModifyDialog modifyDialog = new ModifyDialog(frame, getSelectedStudent());
modifyDialog.setVisible(true);
}
private void showDeleteDialog() {
Student selectedStudent = getSelectedStudent();
if (selectedStudent != null) {
int result = JOptionPane.showConfirmDialog(frame, "确认删除该学生吗?", "确认删除", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
tableModel.removeRow(studentTable.getSelectedRow());
students.remove(selectedStudent);
}
}
}
private Student getSelectedStudent() {
int row = studentTable.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(frame, "请选择一行进行操作!");
return null;
}
int id = (int) tableModel.getValueAt(row, 0);
String name = (String) tableModel.getValueAt(row, 1);
int age = (int) tableModel.getValueAt(row, 2);
String sex = (String) tableModel.getValueAt(row, 3);
return new Student(id, name, age, sex);
}
private class AddDialog extends JDialog {
private JTextField nameField;
private JTextField ageField;
private JComboBox<String> sexComboBox;
public AddDialog(JFrame parent) {
super(parent, "添加学生信息", true);
setLayout(new GridLayout(4, 2));
setSize(300, 200);
setLocationRelativeTo(parent);
add(new JLabel("姓名:"));
nameField = new JTextField(20);
add(nameField);
add(new JLabel("年龄:"));
ageField = new JTextField(20);
add(ageField);
add(new JLabel("性别:"));
sexComboBox = new JComboBox<>(new String[]{"男", "女"});
add(sexComboBox);
JButton addButton = new JButton("添加");
addButton.addActionListener(e -> {
String name = nameField.getText().trim();
String ageStr = ageField.getText().trim();
String sex = (String) sexComboBox.getSelectedItem();
if (name.isEmpty() || ageStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入完整信息!");
return;
}
int age = Integer.parseInt(ageStr);
Student newStudent = new Student(students.size() + 1, name, age, sex);
students.add(newStudent);
tableModel.addRow(new Object[]{newStudent.getId(), newStudent.getName(), newStudent.getAge(), newStudent.getSex()});
dispose();
});
add(addButton);
}
}
private class ModifyDialog extends JDialog {
private JTextField nameField;
private JTextField ageField;
private JComboBox<String> sexComboBox;
private Student student;
public ModifyDialog(JFrame parent, Student student) {
super(parent, "修改学生信息", true);
this.student = student;
setLayout(new GridLayout(4, 2));
setSize(300, 200);
setLocationRelativeTo(parent);
add(new JLabel("姓名:"));
nameField = new JTextField(student.getName(), 20);
add(nameField);
add(new JLabel("年龄:"));
ageField = new JTextField(String.valueOf(student.getAge()), 20);
add(ageField);
add(new JLabel("性别:"));
sexComboBox = new JComboBox<>(new String[]{"男", "女"});
sexComboBox.setSelectedItem(student.getSex());
add(sexComboBox);
JButton modifyButton = new JButton("修改");
modifyButton.addActionListener(e -> {
String name = nameField.getText().trim();
String ageStr = ageField.getText().trim();
String sex = (String) sexComboBox.getSelectedItem();
if (name.isEmpty() || ageStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入完整信息!");
return;
}
int age = Integer.parseInt(ageStr);
student.setName(name);
student.setAge(age);
student.setSex(sex);
int row = studentTable.getSelectedRow();
tableModel.setValueAt(name, row, 1);
tableModel.setValueAt(age, row, 2);
tableModel.setValueAt(sex, row, 3);
dispose();
});
add(modifyButton);
}
}
private static class Student {
private int id;
private String name;
private int age;
private String sex;
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
}
java学生信息的管理界面,展示全部学生信息,提供一个根据名称查询某个学生信息提示,添加学生信息,删除学生信息,修改学生信息。输入框和搜索按钮、添加按钮占第一行并且居中,中间展示表格,每行数据右键绑定
最新推荐文章于 2024-10-16 08:01:18 发布