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.
math-learing/src/main/java/com/personalproject/ui/views/MainMenuView.java

170 lines
5.2 KiB

package com.personalproject.ui.views;
import com.personalproject.auth.UserAccount;
import com.personalproject.controller.MathLearningController;
import com.personalproject.model.DifficultyLevel;
import com.personalproject.ui.scenes.LoginScene;
import java.util.Optional;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
/**
* 用户可以开始考试或更改设置的主菜单界面.
*/
public class MainMenuView extends BorderPane {
private final Stage primaryStage;
private final MathLearningController controller;
private final UserAccount userAccount;
private Button changePasswordButton;
private Button logoutButton;
/**
* MainMenuView 的构造函数.
*
* @param primaryStage 应用程序的主舞台
* @param controller 数学学习控制器
* @param userAccount 当前用户账户
*/
public MainMenuView(Stage primaryStage, MathLearningController controller,
UserAccount userAccount) {
this.primaryStage = primaryStage;
this.controller = controller;
this.userAccount = userAccount;
initializeUi();
}
/**
* 初始化界面组件.
*/
private void initializeUi() {
// 创建主布局
VBox mainLayout = new VBox(20);
mainLayout.setAlignment(Pos.CENTER);
mainLayout.setPadding(new Insets(20));
// 欢迎信息
Label welcomeLabel = new Label("欢迎, " + userAccount.username());
welcomeLabel.setFont(Font.font("System", FontWeight.BOLD, 18));
// 难度信息
Label difficultyLabel = new Label(
"当前难度: " + userAccount.difficultyLevel().getDisplayName());
difficultyLabel.setFont(Font.font("System", FontWeight.NORMAL, 14));
Label promptLabel = new Label("请选择考试难度开始答题");
promptLabel.setFont(Font.font("System", FontWeight.NORMAL, 14));
VBox difficultyBox = new VBox(10);
difficultyBox.setAlignment(Pos.CENTER);
difficultyBox.getChildren().addAll(
createDifficultyButton(DifficultyLevel.PRIMARY),
createDifficultyButton(DifficultyLevel.MIDDLE),
createDifficultyButton(DifficultyLevel.HIGH));
// 按钮区域
VBox buttonBox = new VBox(15);
buttonBox.setAlignment(Pos.CENTER);
changePasswordButton = new Button("修改密码");
logoutButton = new Button("退出登录");
// 设置按钮尺寸
changePasswordButton.setPrefSize(150, 40);
logoutButton.setPrefSize(150, 40);
buttonBox.getChildren().addAll(changePasswordButton, logoutButton);
// 将组件添加到主布局
mainLayout.getChildren().addAll(welcomeLabel, difficultyLabel, promptLabel, difficultyBox,
buttonBox);
// 将主布局置于边界面板中央
setCenter(mainLayout);
// 添加事件处理器
addEventHandlers();
}
/**
* 为界面组件添加事件处理器.
*/
private void addEventHandlers() {
changePasswordButton.setOnAction(e -> handleChangePassword());
logoutButton.setOnAction(e -> handleLogout());
}
private Button createDifficultyButton(DifficultyLevel level) {
Button button = new Button(level.getDisplayName());
button.setPrefSize(150, 40);
button.setOnAction(e -> handleDifficultySelection(level));
return button;
}
private void handleDifficultySelection(DifficultyLevel level) {
TextInputDialog dialog = new TextInputDialog("10");
dialog.setTitle("题目数量");
dialog.setHeaderText("请选择题目数量");
dialog.setContentText("请输入题目数量 (10-30):");
Optional<String> result = dialog.showAndWait();
if (result.isEmpty()) {
return;
}
int questionCount;
try {
questionCount = Integer.parseInt(result.get().trim());
} catch (NumberFormatException ex) {
showAlert(Alert.AlertType.ERROR, "无效输入", "请输入有效的数字。");
return;
}
if (questionCount < 10 || questionCount > 30) {
showAlert(Alert.AlertType.WARNING, "题目数量范围错误", "题目数量必须在10到30之间。");
return;
}
com.personalproject.model.ExamSession examSession = controller.createExamSession(
userAccount.username(), level, questionCount);
ExamView examView = new ExamView(primaryStage, controller, examSession);
primaryStage.getScene().setRoot(examView);
}
/**
* 处理修改密码按钮的操作.
*/
private void handleChangePassword() {
PasswordChangeView passwordChangeView = new PasswordChangeView(primaryStage, controller,
userAccount);
primaryStage.getScene().setRoot(passwordChangeView);
}
private void showAlert(Alert.AlertType alertType, String title, String message) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
/**
* 处理退出登录按钮的操作.
*/
private void handleLogout() {
// 返回登录界面
LoginScene loginScene = new LoginScene(primaryStage, controller);
primaryStage.getScene().setRoot(loginScene);
}
}