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.
265 lines
6.7 KiB
265 lines
6.7 KiB
package com.wsf.mathapp.view;
|
|
|
|
import com.wsf.mathapp.controller.SceneManager;
|
|
import com.ybw.mathapp.util.Login;
|
|
import com.ybw.mathapp.util.LoginFileUtils;
|
|
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.PasswordField;
|
|
import javafx.scene.control.TextField;
|
|
import javafx.scene.layout.VBox;
|
|
import javafx.scene.text.Font;
|
|
|
|
/**
|
|
* 登录视图类,提供用户登录功能界面.
|
|
* 支持用户名或邮箱登录,包含输入验证和登录状态提示.
|
|
*/
|
|
public class LoginView {
|
|
private Scene scene;
|
|
private final SceneManager sceneManager;
|
|
|
|
// 将UI组件声明为类的成员变量
|
|
private TextField usernameOrEmailField;
|
|
private PasswordField passwordField;
|
|
private Label statusLabel;
|
|
|
|
/**
|
|
* 构造函数,初始化登录视图.
|
|
*
|
|
* @param sceneManager 场景管理器,用于界面导航.
|
|
*/
|
|
public LoginView(SceneManager sceneManager) {
|
|
this.sceneManager = sceneManager;
|
|
createScene();
|
|
}
|
|
|
|
/**
|
|
* 创建登录界面的主场景.
|
|
*/
|
|
private void createScene() {
|
|
VBox root = new VBox(20);
|
|
root.setPadding(new Insets(40));
|
|
root.setAlignment(Pos.CENTER);
|
|
|
|
// 创建用户名/邮箱输入框
|
|
usernameOrEmailField = createUsernameField();
|
|
|
|
// 创建密码输入框
|
|
passwordField = createPasswordField();
|
|
|
|
// 创建登录按钮
|
|
Button loginButton = createLoginButton();
|
|
|
|
// 创建注册按钮
|
|
Button registerButton = createRegisterButton();
|
|
|
|
// 创建状态标签
|
|
statusLabel = new Label();
|
|
|
|
// 设置按钮事件
|
|
setupLoginButtonAction(loginButton);
|
|
setupRegisterButtonAction(registerButton);
|
|
|
|
// 添加回车键登录支持
|
|
setupEnterKeySupport(loginButton);
|
|
// 创建界面标题
|
|
Label titleLabel = createTitleLabel();
|
|
|
|
// 创建副标题
|
|
Label subtitleLabel = createSubtitleLabel();
|
|
root.getChildren().addAll(
|
|
titleLabel, subtitleLabel, usernameOrEmailField, passwordField,
|
|
loginButton, registerButton, statusLabel
|
|
);
|
|
scene = new Scene(root, 400, 500);
|
|
}
|
|
|
|
/**
|
|
* 创建主标题标签.
|
|
*
|
|
* @return Label 主标题标签对象.
|
|
*/
|
|
private Label createTitleLabel() {
|
|
Label titleLabel = new Label("数学学习软件");
|
|
titleLabel.setFont(Font.font(24));
|
|
return titleLabel;
|
|
}
|
|
|
|
/**
|
|
* 创建副标题标签.
|
|
*
|
|
* @return Label 副标题标签对象.
|
|
*/
|
|
private Label createSubtitleLabel() {
|
|
Label subtitleLabel = new Label("用户登录");
|
|
subtitleLabel.setFont(Font.font(18));
|
|
return subtitleLabel;
|
|
}
|
|
|
|
/**
|
|
* 创建用户名/邮箱输入框.
|
|
*
|
|
* @return TextField 用户名/邮箱输入框对象.
|
|
*/
|
|
private TextField createUsernameField() {
|
|
TextField field = new TextField();
|
|
field.setPromptText("请输入用户名或邮箱");
|
|
field.setMaxWidth(300);
|
|
field.setPrefHeight(40);
|
|
return field;
|
|
}
|
|
|
|
/**
|
|
* 创建密码输入框.
|
|
*
|
|
* @return PasswordField 密码输入框对象.
|
|
*/
|
|
private PasswordField createPasswordField() {
|
|
PasswordField field = new PasswordField();
|
|
field.setPromptText("请输入密码");
|
|
field.setMaxWidth(300);
|
|
field.setPrefHeight(40);
|
|
return field;
|
|
}
|
|
|
|
/**
|
|
* 创建登录按钮.
|
|
*
|
|
* @return Button 登录按钮对象.
|
|
*/
|
|
private Button createLoginButton() {
|
|
Button button = new Button("登录");
|
|
button.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-size: 14px;");
|
|
button.setPrefSize(300, 40);
|
|
return button;
|
|
}
|
|
|
|
/**
|
|
* 创建注册按钮.
|
|
*
|
|
* @return Button 注册按钮对象.
|
|
*/
|
|
private Button createRegisterButton() {
|
|
Button button = new Button("注册账号");
|
|
button.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white; -fx-font-size: 14px;");
|
|
button.setPrefSize(300, 40);
|
|
return button;
|
|
}
|
|
|
|
/**
|
|
* 设置登录按钮的点击事件.
|
|
*
|
|
* @param loginButton 登录按钮对象.
|
|
*/
|
|
private void setupLoginButtonAction(Button loginButton) {
|
|
loginButton.setOnAction(e -> handleLogin());
|
|
}
|
|
|
|
/**
|
|
* 设置注册按钮的点击事件.
|
|
*
|
|
* @param registerButton 注册按钮对象.
|
|
*/
|
|
private void setupRegisterButtonAction(Button registerButton) {
|
|
registerButton.setOnAction(e -> sceneManager.showRegisterView());
|
|
}
|
|
|
|
/**
|
|
* 设置回车键登录支持.
|
|
*
|
|
* @param loginButton 登录按钮对象.
|
|
*/
|
|
private void setupEnterKeySupport(Button loginButton) {
|
|
usernameOrEmailField.setOnAction(e -> loginButton.fire());
|
|
passwordField.setOnAction(e -> loginButton.fire());
|
|
}
|
|
|
|
/**
|
|
* 处理登录逻辑.
|
|
*/
|
|
private void handleLogin() {
|
|
String usernameOrEmail = usernameOrEmailField.getText().trim();
|
|
String password = passwordField.getText();
|
|
|
|
if (usernameOrEmail.isEmpty() || password.isEmpty()) {
|
|
showError(statusLabel, "用户名/邮箱和密码不能为空!");
|
|
return;
|
|
}
|
|
|
|
// 调用后端的登录方法,支持用户名或邮箱登录
|
|
boolean success = Login.login(usernameOrEmail, password);
|
|
System.out.println(usernameOrEmail);
|
|
System.out.println(password);
|
|
|
|
if (success) {
|
|
handleLoginSuccess(usernameOrEmail);
|
|
} else {
|
|
showError(statusLabel, "用户名/邮箱或密码错误!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理登录成功后的逻辑.
|
|
*
|
|
* @param usernameOrEmail 登录使用的用户名或邮箱.
|
|
*/
|
|
private void handleLoginSuccess(String usernameOrEmail) {
|
|
showSuccess(statusLabel);
|
|
String username = getUsernameFromInput(usernameOrEmail);
|
|
sceneManager.setCurrentUserName(username);
|
|
sceneManager.showMainMenuView();
|
|
}
|
|
|
|
/**
|
|
* 根据输入获取用户名.
|
|
*
|
|
* @param usernameOrEmail 用户输入的用户名或邮箱.
|
|
* @return String 对应的用户名.
|
|
*/
|
|
private String getUsernameFromInput(String usernameOrEmail) {
|
|
String foundName = LoginFileUtils.emailFindName(usernameOrEmail);
|
|
return foundName == null ? usernameOrEmail : foundName;
|
|
}
|
|
|
|
/**
|
|
* 清空所有输入字段和状态信息.
|
|
*/
|
|
public void clearFields() {
|
|
usernameOrEmailField.clear();
|
|
passwordField.clear();
|
|
statusLabel.setText("");
|
|
}
|
|
|
|
/**
|
|
* 显示错误信息.
|
|
*
|
|
* @param label 状态标签对象.
|
|
* @param message 错误信息内容.
|
|
*/
|
|
private void showError(Label label, String message) {
|
|
label.setText(message);
|
|
label.setStyle("-fx-text-fill: red;");
|
|
}
|
|
|
|
/**
|
|
* 显示成功信息.
|
|
*
|
|
* @param label 状态标签对象.
|
|
*/
|
|
private void showSuccess(Label label) {
|
|
label.setText("登录成功!");
|
|
label.setStyle("-fx-text-fill: green;");
|
|
}
|
|
|
|
/**
|
|
* 获取当前场景.
|
|
*
|
|
* @return Scene 登录界面的场景对象.
|
|
*/
|
|
public Scene getScene() {
|
|
return scene;
|
|
}
|
|
} |