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.
113 lines
3.4 KiB
113 lines
3.4 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 com.ybw.mathapp.util.Register;
|
|
import javafx.geometry.Insets;
|
|
import javafx.geometry.Pos;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.layout.VBox;
|
|
import javafx.scene.text.Font;
|
|
|
|
public class LoginView {
|
|
private Scene scene;
|
|
private final SceneManager 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);
|
|
|
|
Label titleLabel = new Label("数学学习软件");
|
|
titleLabel.setFont(Font.font(24));
|
|
|
|
Label subtitleLabel = new Label("用户登录");
|
|
subtitleLabel.setFont(Font.font(18));
|
|
|
|
// 修改提示文本
|
|
TextField usernameOrEmailField = new TextField();
|
|
usernameOrEmailField.setPromptText("请输入用户名或邮箱");
|
|
usernameOrEmailField.setMaxWidth(300);
|
|
usernameOrEmailField.setPrefHeight(40);
|
|
|
|
PasswordField passwordField = new PasswordField();
|
|
passwordField.setPromptText("请输入密码");
|
|
passwordField.setMaxWidth(300);
|
|
passwordField.setPrefHeight(40);
|
|
|
|
Button loginButton = new Button("登录");
|
|
loginButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-size: 14px;");
|
|
loginButton.setPrefSize(300, 40);
|
|
|
|
Button registerButton = new Button("注册账号");
|
|
registerButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white; -fx-font-size: 14px;");
|
|
registerButton.setPrefSize(300, 40);
|
|
|
|
Label statusLabel = new Label();
|
|
|
|
loginButton.setOnAction(e -> {
|
|
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) {
|
|
showSuccess(statusLabel);
|
|
String username;
|
|
if (LoginFileUtils.emailFindName(usernameOrEmail) == null){
|
|
username = usernameOrEmail;
|
|
}
|
|
else {
|
|
username = LoginFileUtils.emailFindName(usernameOrEmail);
|
|
}
|
|
sceneManager.setCurrentUserName(username);
|
|
sceneManager.showMainMenuView();
|
|
} else {
|
|
showError(statusLabel, "用户名/邮箱或密码错误!");
|
|
}
|
|
});
|
|
|
|
registerButton.setOnAction(e -> {
|
|
sceneManager.showRegisterView();
|
|
});
|
|
|
|
// 添加回车键登录支持
|
|
usernameOrEmailField.setOnAction(e -> loginButton.fire());
|
|
passwordField.setOnAction(e -> loginButton.fire());
|
|
|
|
root.getChildren().addAll(
|
|
titleLabel, subtitleLabel, usernameOrEmailField, passwordField,
|
|
loginButton, registerButton, statusLabel
|
|
);
|
|
|
|
scene = new Scene(root, 400, 500);
|
|
}
|
|
|
|
private void showError(Label label, String message) {
|
|
label.setText(message);
|
|
label.setStyle("-fx-text-fill: red;");
|
|
}
|
|
|
|
private void showSuccess(Label label) {
|
|
label.setText("登录成功!");
|
|
label.setStyle("-fx-text-fill: green;");
|
|
}
|
|
|
|
public Scene getScene() {
|
|
return scene;
|
|
}
|
|
} |