diff --git a/src/main/java/com/example/myapp/controller/GradeSelectionController.java b/src/main/java/com/example/myapp/controller/GradeSelectionController.java new file mode 100644 index 0000000..79d4fcb --- /dev/null +++ b/src/main/java/com/example/myapp/controller/GradeSelectionController.java @@ -0,0 +1,92 @@ +package com.example.myapp.controller; + +import com.example.myapp.Main; +import com.example.myapp.service.ExamService; +import javafx.fxml.FXML; +import javafx.scene.control.*; + +public class GradeSelectionController { + @FXML private Label welcomeLabel; + + private String email; + private final ExamService examService = ExamService.getInstance(); + + public void initUser(String email) { + this.email = email; + welcomeLabel.setText("欢迎 " + email + ",请选择学段"); + } + + @FXML + public void onPrimarySchoolSelected() { + showQuestionCountDialog("小学"); + } + + @FXML + public void onMiddleSchoolSelected() { + showQuestionCountDialog("初中"); + } + + @FXML + public void onHighSchoolSelected() { + showQuestionCountDialog("高中"); + } + + @FXML + public void onBackToLogin() { + try { + Main.showLogin(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @FXML + public void onBackToDashboard() { + try { + Main.showDashboard(email); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void showQuestionCountDialog(String gradeLevel) { + TextInputDialog dialog = new TextInputDialog("10"); + dialog.setTitle("题目数量"); + dialog.setHeaderText("选择" + gradeLevel + "题目"); + dialog.setContentText("请输入题目数量(1-100):"); + + dialog.showAndWait().ifPresent(countStr -> { + try { + int questionCount = Integer.parseInt(countStr); + if (questionCount < 1 || questionCount > 100) { + showAlert("题目数量必须在1-100之间"); + return; + } + + boolean success = examService.generateExam(gradeLevel, questionCount, email); + if (success) { + // 修复:添加异常处理 + try { + Main.showExamPage(email); + } catch (Exception e) { + e.printStackTrace(); + showAlert("无法进入考试页面"); + } + } else { + showAlert("生成试卷失败,请重试"); + } + + } catch (NumberFormatException e) { + showAlert("请输入有效的数字"); + } + }); + } + + private void showAlert(String message) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("错误"); + alert.setHeaderText(null); + alert.setContentText(message); + alert.showAndWait(); + } +} \ No newline at end of file