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.3 KiB
113 lines
3.3 KiB
package com.mathgenerator.controller;
|
|
|
|
import com.mathgenerator.model.User;
|
|
import com.mathgenerator.service.UserService;
|
|
import javafx.event.ActionEvent;
|
|
import javafx.fxml.FXML;
|
|
import javafx.fxml.FXMLLoader;
|
|
import javafx.scene.Parent;
|
|
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.stage.Stage;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Optional;
|
|
|
|
public class LoginController {
|
|
|
|
// 依赖注入后端服务
|
|
private final UserService userService = new UserService();
|
|
|
|
// @FXML注解将FXML文件中的控件与这里的变量关联起来
|
|
@FXML
|
|
private TextField usernameField;
|
|
|
|
@FXML
|
|
private PasswordField passwordField;
|
|
|
|
@FXML
|
|
private Button loginButton;
|
|
|
|
@FXML
|
|
private Button registerButton;
|
|
|
|
@FXML
|
|
private Label statusLabel;
|
|
|
|
/**
|
|
* 处理登录按钮点击事件。
|
|
* @param event 事件对象
|
|
*/
|
|
@FXML
|
|
private void handleLoginButtonAction(ActionEvent event) {
|
|
String username = usernameField.getText();
|
|
String password = passwordField.getText();
|
|
|
|
if (username.isEmpty() || password.isEmpty()) {
|
|
statusLabel.setText("用户名和密码不能为空!");
|
|
return;
|
|
}
|
|
|
|
Optional<User> userOptional = userService.login(username, password);
|
|
|
|
if (userOptional.isPresent()) {
|
|
statusLabel.setText("登录成功!");
|
|
// 登录成功,调用新方法跳转到主菜单
|
|
loadMainMenu(userOptional.get());
|
|
} else {
|
|
statusLabel.setText("登录失败:用户名或密码错误。");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理注册按钮点击事件,跳转到注册界面。
|
|
* @param event 事件对象
|
|
*/
|
|
@FXML
|
|
private void handleRegisterButtonAction(ActionEvent event) {
|
|
loadScene("/com/mathgenerator/view/RegisterView.fxml");
|
|
}
|
|
|
|
/**
|
|
* 加载主菜单界面,并传递用户信息。
|
|
* @param user 登录成功的用户对象
|
|
*/
|
|
private void loadMainMenu(User user) {
|
|
try {
|
|
// 1. 加载 FXML 文件
|
|
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/mathgenerator/view/MainMenuView.fxml"));
|
|
Parent root = loader.load();
|
|
|
|
// 2. 获取新界面的控制器
|
|
MainMenuController controller = loader.getController();
|
|
|
|
// 3. 调用控制器的方法,传递数据
|
|
controller.initData(user);
|
|
|
|
// 4. 显示新场景
|
|
Stage stage = (Stage) loginButton.getScene().getWindow();
|
|
stage.setScene(new Scene(root));
|
|
stage.setTitle("主菜单");
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 切换到简单场景的辅助方法(如注册页)。
|
|
* @param fxmlPath FXML文件的路径
|
|
*/
|
|
private void loadScene(String fxmlPath) {
|
|
try {
|
|
Parent root = FXMLLoader.load(getClass().getResource(fxmlPath));
|
|
Stage stage = (Stage) loginButton.getScene().getWindow();
|
|
stage.setScene(new Scene(root));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |