import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Student {
// 创建JFrame窗口
private JFrame frame = new JFrame("课堂随机点名系统");
// 创建菜单条
JMenuBar menuBar = new JMenuBar();
// 创建菜单组件
JMenu fileMenu = new JMenu("文件");
JMenu classroomMenu = new JMenu("班级管理");
JMenu studentMenu = new JMenu("学生管理");
JMenu groupMenu = new JMenu("小组管理");
JMenu classMenu = new JMenu("课堂管理");
// 菜单项组件
JMenuItem addGroup = new JMenuItem("新增小组");
JMenuItem groupOrganize = new JMenuItem("小组列表");
JMenuItem addStudent = new JMenuItem("新增学生");
JMenuItem studentOrganize = new JMenuItem("学生列表");
JMenuItem randomGroup = new JMenuItem("随机小组");
JMenuItem randomStudent = new JMenuItem("随机学生");
JMenuItem addClass = new JMenuItem("新增班级");
JMenuItem classOrganize = new JMenuItem("班级列表");
JMenuItem selectClass = new JMenuItem("选择班级");
// 当前选择的班级
private String currentClass = null;
public void init() {
// 添加菜单项到对应的菜单
groupMenu.add(addGroup);
groupMenu.add(groupOrganize);
studentMenu.add(addStudent);
studentMenu.add(studentOrganize);
classMenu.add(randomGroup);
classMenu.add(randomStudent);
classroomMenu.add(addClass);
classroomMenu.add(classOrganize);
classroomMenu.add(selectClass);
// 添加菜单到菜单条
menuBar.add(fileMenu); // 文件菜单暂时未添加任何项目
menuBar.add(classroomMenu);
menuBar.add(studentMenu);
menuBar.add(groupMenu);
menuBar.add(classMenu);
// 为每个菜单项添加监听器
addGroup.addActionListener(e -> checkIfClassIsSelected(() -> showAddGroupDialog()));
groupOrganize.addActionListener(e -> checkIfClassIsSelected(() -> showGroupOrganizeDialog()));
addStudent.addActionListener(e -> checkIfClassIsSelected(() -> showAddStudentDialog()));
studentOrganize.addActionListener(e -> checkIfClassIsSelected(() -> showStudentOrganizeDialog()));
randomGroup.addActionListener(e -> checkIfClassIsSelected(() -> showRandomGroupDialog()));
randomStudent.addActionListener(e -> checkIfClassIsSelected(() -> showRandomStudentDialog()));
addClass.addActionListener(e -> showAddClassDialog());
classOrganize.addActionListener(e -> showClassOrganizeDialog());
selectClass.addActionListener(e -> showSelectClassDialog());
// 把菜单条放入到JFrame中
frame.setJMenuBar(menuBar);
// 设置窗口关闭操作
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口最佳大小
frame.pack();
// 设置窗口可见性
frame.setVisible(true);
}
private void checkIfClassIsSelected(Runnable action) {
if (currentClass == null) {
JOptionPane.showMessageDialog(frame, "请选择班级!", "警告", JOptionPane.WARNING_MESSAGE);
} else {
action.run();
}
}
private void showAddGroupDialog() {
JFrame dialog = new JFrame("新增小组");
dialog.setSize(300, 200);
dialog.setLayout(new FlowLayout());
dialog.add(new JLabel("小组名称:"));
JTextField groupNameField = new JTextField(20);
dialog.add(groupNameField);
JButton okButton = new JButton("确定");
okButton.addActionListener(e -> {
String groupName = groupNameField.getText();
if (groupName.trim().isEmpty()) {
JOptionPane.showMessageDialog(dialog, "请输入有效的小组名称!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(dialog, "新增小组: " + groupName + " 成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
dialog.dispose();
}
});
dialog.add(okButton);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showGroupOrganizeDialog() {
JFrame dialog = new JFrame("小组列表");
dialog.setSize(300, 200);
dialog.setLayout(new BorderLayout());
JList<String> list = new JList<>(new String[]{"小组1", "小组2", "小组3"});
dialog.add(new JScrollPane(list), BorderLayout.CENTER);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showAddStudentDialog() {
JFrame dialog = new JFrame("新增学生");
dialog.setSize(300, 200);
dialog.setLayout(new FlowLayout());
dialog.add(new JLabel("学生姓名:"));
JTextField studentNameField = new JTextField(20);
dialog.add(studentNameField);
JButton okButton = new JButton("确定");
okButton.addActionListener(e -> {
String studentName = studentNameField.getText();
if (studentName.trim().isEmpty()) {
JOptionPane.showMessageDialog(dialog, "请输入有效的学生姓名!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(dialog, "新增学生: " + studentName + " 成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
dialog.dispose();
}
});
dialog.add(okButton);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showStudentOrganizeDialog() {
JFrame dialog = new JFrame("学生列表");
dialog.setSize(300, 200);
dialog.setLayout(new BorderLayout());
JList<String> list = new JList<>(new String[]{"张三", "李四", "王五"});
dialog.add(new JScrollPane(list), BorderLayout.CENTER);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showRandomGroupDialog() {
List<String> groups = new ArrayList<>();
groups.add("小组1");
groups.add("小组2");
groups.add("小组3");
JFrame dialog = new JFrame("随机小组");
dialog.setSize(300, 200);
dialog.setLayout(new FlowLayout());
JButton randomButton = new JButton("随机选择");
randomButton.addActionListener(e -> {
if (!groups.isEmpty()) {
Collections.shuffle(groups);
String selectedGroup = groups.get(0);
JOptionPane.showMessageDialog(dialog, "随机选中的小组是: " + selectedGroup, "随机小组", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(dialog, "小组列表为空", "错误", JOptionPane.ERROR_MESSAGE);
}
});
dialog.add(randomButton);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showRandomStudentDialog() {
List<String> students = new ArrayList<>();
students.add("张三");
students.add("李四");
students.add("王五");
students.add("赵六");
JFrame dialog = new JFrame("随机学生");
dialog.setSize(300, 200);
dialog.setLayout(new FlowLayout());
JButton randomButton = new JButton("随机选择");
randomButton.addActionListener(e -> {
if (!students.isEmpty()) {
Collections.shuffle(students);
String selectedStudent = students.get(0);
JOptionPane.showMessageDialog(dialog, "随机选中的学生是: " + selectedStudent, "随机学生", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(dialog, "学生列表为空", "错误", JOptionPane.ERROR_MESSAGE);
}
});
dialog.add(randomButton);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showAddClassDialog() {
JFrame dialog = new JFrame("新增班级");
dialog.setSize(300, 200);
dialog.setLayout(new FlowLayout());
dialog.add(new JLabel("班级名称:"));
JTextField classNameField = new JTextField(20);
dialog.add(classNameField);
JButton okButton = new JButton("确定");
okButton.addActionListener(e -> {
String className = classNameField.getText();
if (className.trim().isEmpty()) {
JOptionPane.showMessageDialog(dialog, "请输入有效的班级名称!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(dialog, "新增班级: " + className + " 成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
dialog.dispose(); // 关闭对话框
}
});
dialog.add(okButton);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showClassOrganizeDialog() {
JFrame dialog = new JFrame("班级列表");
dialog.setSize(300, 200);
dialog.setLayout(new BorderLayout());
JList<String> list = new JList<>(new String[]{"班级1", "班级2", "班级3"});
dialog.add(new JScrollPane(list), BorderLayout.CENTER);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
private void showSelectClassDialog() {
JFrame dialog = new JFrame("选择班级");
dialog.setSize(300, 200);
dialog.setLayout(new BorderLayout());
JList<String> classList = new JList<>(new String[]{"班级1", "班级2", "班级3"});
JButton okButton = new JButton("确定");
okButton.addActionListener(e -> {
String selectedClass = (String) classList.getSelectedValue();
if (selectedClass == null) {
JOptionPane.showMessageDialog(dialog, "请选择一个班级!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
currentClass = selectedClass;
JOptionPane.showMessageDialog(dialog, "已选择班级: " + selectedClass, "成功", JOptionPane.INFORMATION_MESSAGE);
dialog.dispose();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(okButton);
dialog.add(new JScrollPane(classList), BorderLayout.CENTER);
dialog.add(buttonPanel, BorderLayout.SOUTH);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Student().init());
}
}
标签:JFrame,Java,管理系统,JOptionPane,学生,add,dialog,JMenuItem,new
From: https://blog.csdn.net/2401_82456630/article/details/143750265