From 9bc8ace6cd0e8e62006ccbf3c4c4fce614aff73d Mon Sep 17 00:00:00 2001 From: pus7f45rn <2668148347@qq.com> Date: Sun, 12 Oct 2025 17:28:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'src/view/MainMenuView.jav?= =?UTF-8?q?a'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/view/MainMenuView.java | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/view/MainMenuView.java diff --git a/src/view/MainMenuView.java b/src/view/MainMenuView.java new file mode 100644 index 0000000..40c0cf8 --- /dev/null +++ b/src/view/MainMenuView.java @@ -0,0 +1,72 @@ +package view; + +import model.UserType; +import javax.swing.*; +import java.awt.*; + +public class MainMenuView extends JPanel { + private final JLabel welcomeLabel = new JLabel("", SwingConstants.CENTER); + private final JComboBox typeComboBox = new JComboBox<>(); + private final JSpinner countSpinner = new JSpinner(new SpinnerNumberModel(10, 10, 30, 1)); + private final JButton startExamButton = new JButton("开始答题"); + private final JButton changePasswordButton = new JButton("修改密码"); + private final JButton logoutButton = new JButton("退出登录"); + + public MainMenuView() { + setLayout(new BorderLayout(20, 20)); + setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40)); + + welcomeLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 28)); + add(welcomeLabel, BorderLayout.NORTH); + + JPanel centerPanel = new JPanel(new GridBagLayout()); + GridBagConstraints gbc = new GridBagConstraints(); + gbc.insets = new Insets(15, 10, 15, 10); + + gbc.gridx = 0; gbc.gridy = 0; addLabel(centerPanel, "选择题目难度:", gbc); + gbc.gridx = 1; gbc.gridy = 0; addComponent(centerPanel, typeComboBox, gbc); + + gbc.gridx = 0; gbc.gridy = 1; addLabel(centerPanel, "选择题目数量:", gbc); + gbc.gridx = 1; gbc.gridy = 1; addComponent(centerPanel, countSpinner, gbc); + + startExamButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20)); + + startExamButton.putClientProperty("JButton.buttonType", "roundRect"); + gbc.gridy = 2; gbc.gridwidth = 2; gbc.insets = new Insets(30, 10, 15, 10); + addComponent(centerPanel, startExamButton, gbc); + add(centerPanel, BorderLayout.CENTER); + + JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 0)); + southPanel.add(changePasswordButton); + southPanel.add(logoutButton); + add(southPanel, BorderLayout.SOUTH); + + for (UserType type : UserType.values()) { + typeComboBox.addItem(type.getDisplayName()); + } + } + + private void addLabel(JPanel panel, String text, GridBagConstraints gbc) { + JLabel label = new JLabel(text); + label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16)); + panel.add(label, gbc); + } + + private void addComponent(JPanel panel, JComponent comp, GridBagConstraints gbc) { + comp.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16)); + panel.add(comp, gbc); + } + + public void setWelcomeMessage(String message) { welcomeLabel.setText(message); } + public UserType getSelectedType() { + String displayName = (String) typeComboBox.getSelectedItem(); + for (UserType type : UserType.values()) { + if (type.getDisplayName().equals(displayName)) return type; + } + return UserType.PRIMARY; + } + public int getQuestionCount() { return (int) countSpinner.getValue(); } + public JButton getStartExamButton() { return startExamButton; } + public JButton getChangePasswordButton() { return changePasswordButton; } + public JButton getLogoutButton() { return logoutButton; } +} \ No newline at end of file