You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Partner_Project/src/ui/GradeSelectionFrame.java

234 lines
7.4 KiB

package ui;
import model.Grade;
import model.UserManager;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
/**
* 年级选择和题目数量输入界面
* 用户可以选择年级、设置题目数量、用户设置或退出登录
*/
public class GradeSelectionFrame extends JFrame {
private static final long serialVersionUID = 1L;
private final String email;
private final String username;
private Grade currentGrade;
private final JLabel gradeLabel;
private final JComboBox<String> gradeComboBox;
private final JSpinner questionSpinner;
private final JButton startButton;
private final JButton userSettingsButton;
private final JButton logoutButton;
/**
* 构造年级选择界面(使用邮箱和年级)
*
* @param email 用户邮箱
* @param initialGrade 初始年级
*/
public GradeSelectionFrame(String email, Grade initialGrade) {
this(email, initialGrade, email); // 默认显示邮箱作为用户名
}
/**
* 构造年级选择界面(使用 UserInfo 对象)
*
* @param userInfo 用户信息对象
*/
public GradeSelectionFrame(UserManager.UserInfo userInfo) {
this(userInfo.getEmail(), userInfo.getGrade(), userInfo.getUsername());
}
/**
* 构造年级选择界面(完整构造函数)
*
* @param email 用户邮箱
* @param initialGrade 初始年级
* @param username 用户名
*/
private GradeSelectionFrame(String email, Grade initialGrade, String username) {
this.email = email;
this.username = username;
this.currentGrade = initialGrade;
setTitle("数学学习软件 - 选择年级");
setSize(700, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// --- Top Panel ---
JPanel topPanel = new JPanel(new BorderLayout());
// 欢迎信息(显示用户名)
JLabel welcomeLabel = new JLabel("欢迎, " + username, JLabel.LEFT);
welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
topPanel.add(welcomeLabel, BorderLayout.WEST);
// 用户设置按钮(右上角)
userSettingsButton = new JButton("用户设置");
JPanel topRightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
topRightPanel.add(userSettingsButton);
topPanel.add(topRightPanel, BorderLayout.EAST);
// 当前年级显示
gradeLabel = new JLabel("当前年级: " + getGradeName(currentGrade),
JLabel.CENTER);
gradeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
gradeLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
topPanel.add(gradeLabel, BorderLayout.SOUTH);
mainPanel.add(topPanel, BorderLayout.NORTH);
// --- Center Panel ---
JPanel centerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 5, 10, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
centerPanel.add(new JLabel("选择年级:"), gbc);
String[] grades = {"小学", "初中", "高中"};
gradeComboBox = new JComboBox<>(grades);
gradeComboBox.setSelectedItem(getGradeName(currentGrade));
gbc.gridx = 1;
gbc.weightx = 1.0;
centerPanel.add(gradeComboBox, gbc);
gbc.weightx = 0.0;
gbc.gridx = 0;
gbc.gridy = 1;
centerPanel.add(new JLabel("题目数量:"), gbc);
SpinnerNumberModel spinnerModel = new SpinnerNumberModel(10, 10, 30, 1);
questionSpinner = new JSpinner(spinnerModel);
// 禁用 JSpinner 的文本输入功能,只允许通过按钮调节
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) questionSpinner.getEditor();
editor.getTextField().setEditable(false);
gbc.gridx = 1;
gbc.weightx = 1.0;
centerPanel.add(questionSpinner, gbc);
gbc.weightx = 0.0;
startButton = new JButton("开始答题");
startButton.setPreferredSize(new Dimension(200, 40));
startButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
centerPanel.add(startButton, gbc);
mainPanel.add(centerPanel, BorderLayout.CENTER);
// --- Bottom Panel ---
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
logoutButton = new JButton("退出登录");
bottomPanel.add(logoutButton);
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
add(mainPanel);
// --- Action Listeners ---
startButton.addActionListener(e -> startQuiz());
userSettingsButton.addActionListener(e -> openUserSettings());
logoutButton.addActionListener(e -> logout());
gradeComboBox.addActionListener(e -> updateGrade());
}
/**
* 更新当前年级
*/
private void updateGrade() {
String selected = (String) gradeComboBox.getSelectedItem();
switch (selected) {
case "小学":
currentGrade = Grade.primary;
break;
case "初中":
currentGrade = Grade.middle;
break;
case "高中":
currentGrade = Grade.high;
break;
}
gradeLabel.setText("当前年级: " + getGradeName(currentGrade));
}
/**
* 开始答题
*/
private void startQuiz() {
int questionCount = (Integer) questionSpinner.getValue();
QuizFrame quizFrame = new QuizFrame(email, currentGrade, questionCount);
quizFrame.setVisible(true);
this.dispose();
}
/**
* 打开用户设置对话框
*/
private void openUserSettings() {
UserSettingsDialog dialog = new UserSettingsDialog(this, email, username);
dialog.setVisible(true);
}
/**
* 退出登录
*/
private void logout() {
int confirm = JOptionPane.showConfirmDialog(this,
"确定要退出登录吗?",
"确认",
JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
LoginFrame loginFrame = new LoginFrame();
loginFrame.setVisible(true);
this.dispose();
}
}
/**
* 获取年级名称
*
* @param grade 年级类型
* @return 年级名称
*/
private String getGradeName(Grade grade) {
switch (grade) {
case primary:
return "小学";
case middle:
return "初中";
case high:
return "高中";
default:
return "未知";
}
}
}