// com/ui/InfGenPage.java package com.pair.ui; import com.pair.model.Grade; import javafx.geometry.Pos; import javafx.scene.control.*; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.TextAlignment; public class InfGenPage extends NavigablePanel { private final TextField usernameField = new TextField(); private final TextField emailField = new TextField(); private final Label passwordLabel = new Label("******"); private final ChoiceBox gradeChoice = new ChoiceBox<>(); private final Spinner questionCountSpinner = new Spinner<>(10, 30, 10); private final Button passwordModifyButton = new Button("修改密码"); private final Button generateButton = new Button("生成题目"); private final Button modifyUsernameButton = new Button("修改用户名"); // 新增 private final Button modifyEmailButton = new Button("修改邮箱"); // 新增 public InfGenPage(Runnable onBack, String currentUsername, String currentEmail, Grade currentGrade) { super(onBack); initializeContent(); usernameField.setText(currentUsername); emailField.setText(currentEmail); gradeChoice.getSelectionModel().select(currentGrade.ordinal()); } @Override protected void buildContent() { // 外层容器:VBox 居中,带内边距和圆角阴影 VBox form = new VBox(UIConstants.DEFAULT_SPACING * 1.5); form.setAlignment(Pos.CENTER); form.setPadding(UIConstants.DEFAULT_PADDING); form.setStyle(UIConstants.FORM_STYLE); form.setMaxWidth(500); // 标题居中 Label titleLabel = new Label("中小学数学答题系统"); titleLabel.setFont(Font.font(UIConstants.FONT_FAMILY, FontWeight.BOLD, UIConstants.TITLE_FONT_SIZE)); titleLabel.setTextAlignment(TextAlignment.CENTER); titleLabel.setMaxWidth(Double.MAX_VALUE); // ========== 创建表单项行(关键:标签左对齐 + 固定宽度)========== HBox usernameRow = createFormRow("用户名:", usernameField, modifyUsernameButton); HBox emailRow = createFormRow("邮箱:", emailField, modifyEmailButton); HBox passwordRow = createFormRow("密码:", passwordLabel, passwordModifyButton); HBox gradeRow = createFormRow("学段选择:", gradeChoice, null); HBox countRow = createFormRow("题目数量:", questionCountSpinner, generateButton); // ========== 配置控件样式 ========== // 用户名输入框 usernameField.setStyle(UIConstants.INPUT_STYLE); usernameField.setPrefWidth(200); usernameField.setFont(Font.font(UIConstants.FONT_FAMILY, UIConstants.INPUT_FONT_SIZE)); // 密码标签 passwordLabel.setStyle("-fx-font-size: " + UIConstants.INPUT_FONT_SIZE + "px;"); // 邮箱输入框 emailField.setStyle(UIConstants.INPUT_STYLE); emailField.setPrefWidth(200); emailField.setFont(Font.font(UIConstants.FONT_FAMILY, UIConstants.INPUT_FONT_SIZE)); // 学段选择框 gradeChoice.getItems().addAll("小学", "初中", "高中"); gradeChoice.setValue("小学"); gradeChoice.setStyle(UIConstants.INPUT_STYLE); gradeChoice.setPrefWidth(200); // 题目数量Spinner questionCountSpinner.setEditable(true); questionCountSpinner.setPrefWidth(200); questionCountSpinner.getEditor().setStyle(UIConstants.INPUT_STYLE); questionCountSpinner.getEditor().setFont(Font.font(UIConstants.FONT_FAMILY, UIConstants.INPUT_FONT_SIZE)); // 按钮样式统一 passwordModifyButton.setStyle(UIConstants.BUTTON_STYLE); generateButton.setPrefSize(UIConstants.BUTTON_WIDTH, UIConstants.BUTTON_HEIGHT); generateButton.setFont(Font.font(UIConstants.FONT_FAMILY, UIConstants.BUTTON_FONT_SIZE)); generateButton.setStyle(UIConstants.BUTTON_STYLE); // 新增按钮样式 modifyUsernameButton.setStyle(UIConstants.BUTTON_STYLE); modifyUsernameButton.setPrefSize(UIConstants.BUTTON_WIDTH, UIConstants.BUTTON_HEIGHT); modifyUsernameButton.setFont(Font.font(UIConstants.FONT_FAMILY, UIConstants.BUTTON_FONT_SIZE)); modifyEmailButton.setStyle(UIConstants.BUTTON_STYLE); modifyEmailButton.setPrefSize(UIConstants.BUTTON_WIDTH, UIConstants.BUTTON_HEIGHT); modifyEmailButton.setFont(Font.font(UIConstants.FONT_FAMILY, UIConstants.BUTTON_FONT_SIZE)); // ========== 添加到表单 ========== form.getChildren().addAll( titleLabel, usernameRow, emailRow, passwordRow, gradeRow, countRow ); this.setCenter(form); } /** * 创建表单项行(标签左对齐,固定宽度,避免偏移) */ private HBox createFormRow(String labelText, Control content, Button rightButton) { HBox row = new HBox(15); // 间距15 row.setAlignment(Pos.CENTER_LEFT); // ← 关键:让整行左对齐 row.setMaxWidth(Double.MAX_VALUE); // 标签:左对齐,固定宽度,字体统一 Label label = new Label(labelText); label.setFont(Font.font(UIConstants.FONT_FAMILY, FontWeight.NORMAL, UIConstants.LABEL_ITEM_TITLE_SIZE)); label.setTextAlignment(TextAlignment.LEFT); // ← 关键:标签文字左对齐 label.setPrefWidth(120); // 固定宽度,确保所有标签对齐 row.getChildren().addAll(label, content); if (rightButton != null) { row.getChildren().add(rightButton); } return row; } // Getters... public TextField getUsernameField() { return usernameField; } public TextField getEmailField() { return emailField; } public ChoiceBox getGradeChoice() { return gradeChoice; } public Spinner getQuestionCountSpinner() { return questionCountSpinner; } public Button getGenerateButton() { return generateButton; } public Button getPasswordModifyButton() { return passwordModifyButton; } public Button getModifyUsernameButton() { return modifyUsernameButton; } // 新增 public Button getModifyEmailButton() { return modifyEmailButton; } // 新增 }