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.
pairedProject/src/main/java/com/wsf/mathapp/view/QuestionCountView.java

219 lines
5.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.wsf.mathapp.view;
import com.wsf.mathapp.controller.SceneManager;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
/**
* 题目数量选择视图类,提供用户选择答题数量的界面.
* 允许用户输入10-30之间的题目数量并开始答题.
*/
public class QuestionCountView {
private Scene scene;
private final SceneManager sceneManager;
private String level;
/**
* 构造函数,初始化题目数量选择视图.
*
* @param sceneManager 场景管理器,用于界面导航.
*/
public QuestionCountView(SceneManager sceneManager) {
this.sceneManager = sceneManager;
createScene();
}
/**
* 创建主场景,包含题目数量选择界面.
*/
private void createScene() {
VBox root = new VBox(20);
root.setPadding(new Insets(40));
root.setAlignment(Pos.CENTER);
// 创建标题标签
Label titleLabel = createTitleLabel();
// 创建级别显示标签
Label levelLabel = createLevelLabel();
// 创建数量输入框
TextField countField = createCountField();
// 创建开始答题按钮
Button startButton = createStartButton();
// 创建返回按钮
Button backButton = createBackButton();
// 创建状态标签
Label statusLabel = new Label();
// 设置按钮事件
setupStartButtonAction(startButton, countField, statusLabel);
setupBackButtonAction(backButton);
root.getChildren().addAll(
titleLabel, levelLabel, countField, startButton, backButton, statusLabel
);
scene = new Scene(root, 400, 400);
}
/**
* 创建标题标签.
*
* @return Label 标题标签对象.
*/
private Label createTitleLabel() {
Label titleLabel = new Label("选择题目数量");
titleLabel.setFont(Font.font(20));
return titleLabel;
}
/**
* 创建级别显示标签.
*
* @return Label 级别显示标签对象.
*/
private Label createLevelLabel() {
Label levelLabel = new Label();
levelLabel.setFont(Font.font(16));
return levelLabel;
}
/**
* 创建数量输入框.
*
* @return TextField 数量输入框对象.
*/
private TextField createCountField() {
TextField countField = new TextField();
countField.setPromptText("请输入题目数量 (10-30)");
countField.setMaxWidth(250);
countField.setPrefHeight(40);
return countField;
}
/**
* 创建开始答题按钮.
*
* @return Button 开始答题按钮对象.
*/
private Button createStartButton() {
Button startButton = new Button("开始答题");
startButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; "
+ "-fx-font-size: 14px;");
startButton.setPrefSize(250, 45);
return startButton;
}
/**
* 创建返回按钮.
*
* @return Button 返回按钮对象.
*/
private Button createBackButton() {
Button backButton = new Button("返回");
backButton.setStyle("-fx-background-color: #757575; -fx-text-fill: white;");
backButton.setPrefSize(250, 40);
return backButton;
}
/**
* 设置开始答题按钮的点击事件.
*
* @param startButton 开始答题按钮.
* @param countField 数量输入框.
* @param statusLabel 状态标签.
*/
private void setupStartButtonAction(Button startButton, TextField countField,
Label statusLabel) {
startButton.setOnAction(e -> handleStartQuiz(countField, statusLabel));
}
/**
* 设置返回按钮的点击事件.
*
* @param backButton 返回按钮.
*/
private void setupBackButtonAction(Button backButton) {
backButton.setOnAction(e -> sceneManager.showLevelSelectionView());
}
/**
* 处理开始答题逻辑.
*
* @param countField 数量输入框.
* @param statusLabel 状态标签.
*/
private void handleStartQuiz(TextField countField, Label statusLabel) {
try {
String countText = countField.getText().trim();
if (countText.isEmpty()) {
showError(statusLabel, "请输入题目数量!");
return;
}
int count = Integer.parseInt(countText);
if (count < 10 || count > 30) {
showError(statusLabel, "题目数量必须在10-30之间");
return;
}
// 直接调用 showQuizView 并传递参数
sceneManager.showQuizView(level, count);
} catch (NumberFormatException ex) {
showError(statusLabel, "请输入有效的数字!");
}
}
/**
* 设置当前选择的题目级别.
*
* @param level 题目难度级别.
*/
public void setLevel(String level) {
this.level = level;
updateLevelDisplay();
}
/**
* 更新界面显示当前选择的级别.
*/
private void updateLevelDisplay() {
if (scene != null) {
VBox root = (VBox) scene.getRoot();
Label levelLabel = (Label) root.getChildren().get(1);
levelLabel.setText("当前级别: " + level);
levelLabel.setStyle("-fx-text-fill: #2196F3;");
}
}
/**
* 显示错误信息.
*
* @param label 状态标签对象.
* @param message 错误信息内容.
*/
private void showError(Label label, String message) {
label.setText(message);
label.setStyle("-fx-text-fill: red;");
}
/**
* 获取当前场景.
*
* @return Scene 题目数量选择界面的场景对象.
*/
public Scene getScene() {
return scene;
}
}