From 611c7f5c9faeb23e0a2113a395ba2fe0be9095fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=96=E5=85=AE=E5=86=89?= <15550273+wang-shengfa-11@user.noreply.gitee.com> Date: Sat, 11 Oct 2025 00:27:32 +0800 Subject: [PATCH] =?UTF-8?q?v3.1=20=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=B0=8Fbug=EF=BC=8C=E4=BE=8B=E5=A6=82=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=88=B0=E7=99=BB=E5=BD=95=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E5=86=8D=E5=88=B0=E6=B3=A8=E5=86=8C=E7=95=8C=E9=9D=A2=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=B8=8A=E4=B8=80=E6=AC=A1=E6=B3=A8=E5=86=8C=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BF=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wsf/mathapp/controller/SceneManager.java | 4 +- .../java/com/wsf/mathapp/view/LoginView.java | 20 ++++++-- .../com/wsf/mathapp/view/MainMenuView.java | 9 +++- .../com/wsf/mathapp/view/RegisterView.java | 49 ++++++++++++++----- users.txt | 2 +- 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/wsf/mathapp/controller/SceneManager.java b/src/main/java/com/wsf/mathapp/controller/SceneManager.java index 39c166a..f0088ce 100644 --- a/src/main/java/com/wsf/mathapp/controller/SceneManager.java +++ b/src/main/java/com/wsf/mathapp/controller/SceneManager.java @@ -6,7 +6,7 @@ import javafx.stage.Stage; public class SceneManager { private final Stage primaryStage; private final LoginView loginView; - private final RegisterView registerView; + private RegisterView registerView; private final MainMenuView mainMenuView; private final LevelSelectionView levelSelectionView; private final QuestionCountView questionCountView; @@ -31,12 +31,14 @@ public class SceneManager { public void showLoginView() { System.out.println("切换到登录界面"); + loginView.clearFields(); primaryStage.setScene(loginView.getScene()); primaryStage.show(); } public void showRegisterView() { System.out.println("切换到注册界面"); + registerView.clearFields(); primaryStage.setScene(registerView.getScene()); primaryStage.show(); } diff --git a/src/main/java/com/wsf/mathapp/view/LoginView.java b/src/main/java/com/wsf/mathapp/view/LoginView.java index 2e4c088..69afbd7 100644 --- a/src/main/java/com/wsf/mathapp/view/LoginView.java +++ b/src/main/java/com/wsf/mathapp/view/LoginView.java @@ -15,6 +15,11 @@ public class LoginView { private Scene scene; private final SceneManager sceneManager; + // 将UI组件声明为类的成员变量 + private TextField usernameOrEmailField; + private PasswordField passwordField; + private Label statusLabel; + public LoginView(SceneManager sceneManager) { this.sceneManager = sceneManager; createScene(); @@ -32,12 +37,12 @@ public class LoginView { subtitleLabel.setFont(Font.font(18)); // 修改提示文本 - TextField usernameOrEmailField = new TextField(); + usernameOrEmailField = new TextField(); usernameOrEmailField.setPromptText("请输入用户名或邮箱"); usernameOrEmailField.setMaxWidth(300); usernameOrEmailField.setPrefHeight(40); - PasswordField passwordField = new PasswordField(); + passwordField = new PasswordField(); passwordField.setPromptText("请输入密码"); passwordField.setMaxWidth(300); passwordField.setPrefHeight(40); @@ -50,7 +55,7 @@ public class LoginView { registerButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white; -fx-font-size: 14px;"); registerButton.setPrefSize(300, 40); - Label statusLabel = new Label(); + statusLabel = new Label(); loginButton.setOnAction(e -> { String usernameOrEmail = usernameOrEmailField.getText().trim(); @@ -97,6 +102,15 @@ public class LoginView { scene = new Scene(root, 400, 500); } + /** + * 清空所有输入字段和状态信息 + */ + public void clearFields() { + usernameOrEmailField.clear(); + passwordField.clear(); + statusLabel.setText(""); + } + private void showError(Label label, String message) { label.setText(message); label.setStyle("-fx-text-fill: red;"); diff --git a/src/main/java/com/wsf/mathapp/view/MainMenuView.java b/src/main/java/com/wsf/mathapp/view/MainMenuView.java index 8953009..7702c36 100644 --- a/src/main/java/com/wsf/mathapp/view/MainMenuView.java +++ b/src/main/java/com/wsf/mathapp/view/MainMenuView.java @@ -3,6 +3,7 @@ package com.wsf.mathapp.view; import com.wsf.mathapp.controller.SceneManager; import com.ybw.mathapp.util.ChangePassword; import com.ybw.mathapp.util.Login; +import com.ybw.mathapp.util.Register; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; @@ -187,7 +188,7 @@ public class MainMenuView { oldPassword.setPrefWidth(200); javafx.scene.control.PasswordField newPassword = new javafx.scene.control.PasswordField(); - newPassword.setPromptText("请输入新密码"); + newPassword.setPromptText("请输入新密码(密码要6-10位,包含大,小写字母和数字)"); newPassword.setPrefWidth(200); javafx.scene.control.PasswordField confirmPassword = new javafx.scene.control.PasswordField(); @@ -228,10 +229,14 @@ public class MainMenuView { showAlert("错误", "两次输入的新密码不一致!"); return null; } - + if (!Register.isVaildPassword(newPwd)) { + showAlert("错误", "密码要6-10位,包含大,小写字母和数字"); + return null; + } // 调用修改密码的后端逻辑 if(!ChangePassword.changePassword(currentUsername, confirmPwd)){ showAlert("错误", "密码修改失败"); + return null; } else{ showAlert("成功", "密码修改成功!"); diff --git a/src/main/java/com/wsf/mathapp/view/RegisterView.java b/src/main/java/com/wsf/mathapp/view/RegisterView.java index f1ed416..d1a3867 100644 --- a/src/main/java/com/wsf/mathapp/view/RegisterView.java +++ b/src/main/java/com/wsf/mathapp/view/RegisterView.java @@ -16,6 +16,15 @@ public class RegisterView { private Scene scene; private final SceneManager sceneManager; + // 将UI组件声明为类的成员变量 + private TextField usernameField; + private TextField emailField; + private TextField codeField; + private PasswordField passwordField; + private PasswordField confirmPasswordField; + private Label statusLabel; + private Button sendCodeButton; + public RegisterView(SceneManager sceneManager) { this.sceneManager = sceneManager; createScene(); @@ -30,31 +39,31 @@ public class RegisterView { titleLabel.setFont(Font.font(20)); // 添加用户名输入框 - TextField usernameField = new TextField(); + usernameField = new TextField(); usernameField.setPromptText("请输入用户名(3-20位字母、数字、下划线)"); usernameField.setMaxWidth(300); usernameField.setPrefHeight(35); - TextField emailField = new TextField(); + emailField = new TextField(); emailField.setPromptText("请输入邮箱"); emailField.setMaxWidth(300); emailField.setPrefHeight(35); - Button sendCodeButton = new Button("发送验证码"); + sendCodeButton = new Button("发送验证码"); sendCodeButton.setStyle("-fx-background-color: #FF9800; -fx-text-fill: white;"); sendCodeButton.setPrefSize(120, 35); - TextField codeField = new TextField(); + codeField = new TextField(); codeField.setPromptText("请输入验证码"); codeField.setMaxWidth(300); codeField.setPrefHeight(35); - PasswordField passwordField = new PasswordField(); + passwordField = new PasswordField(); passwordField.setPromptText("请输入密码(6-10位,含大小写字母和数字)"); passwordField.setMaxWidth(300); passwordField.setPrefHeight(35); - PasswordField confirmPasswordField = new PasswordField(); + confirmPasswordField = new PasswordField(); confirmPasswordField.setPromptText("请再次输入密码"); confirmPasswordField.setMaxWidth(300); confirmPasswordField.setPrefHeight(35); @@ -67,7 +76,7 @@ public class RegisterView { backButton.setStyle("-fx-background-color: #757575; -fx-text-fill: white;"); backButton.setPrefSize(300, 35); - Label statusLabel = new Label(); + statusLabel = new Label(); sendCodeButton.setOnAction(e -> { String username = usernameField.getText().trim(); @@ -159,7 +168,8 @@ public class RegisterView { if (Register.register(username, email, password)) { showSuccess(statusLabel, "注册成功!用户名: " + username); - sceneManager.showLoginView(); + sceneManager.setCurrentUserName(username); + sceneManager.showMainMenuView(); } else { showError(statusLabel, "注册失败,请检查信息!"); } @@ -185,6 +195,22 @@ public class RegisterView { scene = new Scene(root, 400, 550); // 增加高度以适应新字段 } + /** + * 清空所有输入字段和状态信息 + */ + public void clearFields() { + usernameField.clear(); + emailField.clear(); + codeField.clear(); + passwordField.clear(); + confirmPasswordField.clear(); + statusLabel.setText(""); + + // 重置发送验证码按钮状态 + sendCodeButton.setText("发送验证码"); + sendCodeButton.setDisable(false); + } + private void startCountdown(Button button) { new Thread(() -> { try { @@ -218,12 +244,13 @@ public class RegisterView { public Scene getScene() { return scene; } + public static boolean isValidUsername(String username) { if (username == null || username.trim().isEmpty()) { return false; } - // 用户名规则:只能包含英文字母和数字 - String usernameRegex = "^[a-zA-Z0-9]{3,100}$"; + // 用户名规则:只包含英文字母和数字,不限制长度 + String usernameRegex = "^[a-zA-Z0-9]+$"; return Pattern.matches(usernameRegex, username.trim()); } -} +} \ No newline at end of file diff --git a/users.txt b/users.txt index d41caa3..69fac9c 100644 --- a/users.txt +++ b/users.txt @@ -1,2 +1,2 @@ -wsf,3310207578@qq.com,Wsf1234 ybw,1798231811@qq.com,Ybw1234 +wsf,3310207578@qq.com,Wsf1234 -- 2.34.1