package frontend; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import backend.User; public class QuestionCountFrame extends JFrame { private JTextField countField; private JButton startButton, backButton; private User currentUser; private MainFrame mainFrame; public QuestionCountFrame(MainFrame mainFrame, User user) { this.mainFrame = mainFrame; this.currentUser = user; initializeUI(); } private void initializeUI() { setTitle("题目数量设置 - " + currentUser.getUserType()); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setSize(400, 250); setLocationRelativeTo(mainFrame); setResizable(false); // 创建主面板 JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 标题 JLabel titleLabel = new JLabel("请输入题目数量", JLabel.CENTER); titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16)); titleLabel.setForeground(new Color(0, 120, 215)); // 信息标签 JLabel infoLabel = new JLabel("当前选择: " + currentUser.getUserType(), JLabel.CENTER); infoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 输入面板 JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); JLabel countLabel = new JLabel("题目数量:"); countLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14)); countField = new JTextField(10); countField.setFont(new Font("微软雅黑", Font.PLAIN, 14)); inputPanel.add(countLabel); inputPanel.add(countField); // 按钮面板 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); startButton = new JButton("开始答题"); backButton = new JButton("返回"); setupButtonStyle(startButton); setupButtonStyle(backButton); buttonPanel.add(startButton); buttonPanel.add(backButton); // 添加组件到主面板 mainPanel.add(titleLabel, BorderLayout.NORTH); mainPanel.add(infoLabel, BorderLayout.CENTER); mainPanel.add(inputPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); add(mainPanel); // 添加事件监听器 setupEventListeners(); } private void setupButtonStyle(JButton button) { button.setFont(new Font("微软雅黑", Font.BOLD, 14)); button.setBackground(new Color(0, 120, 215)); button.setForeground(Color.BLACK); button.setFocusPainted(false); button.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); } private void setupEventListeners() { startButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { handleStartQuiz(); } }); backButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dispose(); } }); // 回车键开始答题 countField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { handleStartQuiz(); } }); } private void handleStartQuiz() { String countText = countField.getText().trim(); if (countText.isEmpty()) { JOptionPane.showMessageDialog(this, "请输入题目数量", "输入错误", JOptionPane.WARNING_MESSAGE); return; } try { int count = Integer.parseInt(countText); if (count <= 0) { JOptionPane.showMessageDialog(this, "题目数量必须大于0", "输入错误", JOptionPane.WARNING_MESSAGE); return; } if (count > 50) { JOptionPane.showMessageDialog(this, "题目数量不能超过50", "输入错误", JOptionPane.WARNING_MESSAGE); return; } // 创建答题界面 dispose(); new QuizFrame(mainFrame, currentUser, count).setVisible(true); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "请输入有效的数字", "输入错误", JOptionPane.WARNING_MESSAGE); } } }