|
|
|
|
@ -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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|