|
|
|
|
@ -1,45 +1,134 @@
|
|
|
|
|
package com.wsf.mathapp.view;
|
|
|
|
|
|
|
|
|
|
import com.wsf.mathapp.controller.SceneManager;
|
|
|
|
|
import com.ybw.mathapp.util.ChangePassword;
|
|
|
|
|
import com.ybw.mathapp.util.Login;
|
|
|
|
|
import javafx.geometry.Insets;
|
|
|
|
|
import javafx.geometry.Pos;
|
|
|
|
|
import javafx.scene.Scene;
|
|
|
|
|
import javafx.scene.control.Alert;
|
|
|
|
|
import javafx.scene.control.Button;
|
|
|
|
|
import javafx.scene.control.Label;
|
|
|
|
|
import javafx.scene.layout.HBox;
|
|
|
|
|
import javafx.scene.layout.Priority;
|
|
|
|
|
import javafx.scene.layout.Region;
|
|
|
|
|
import javafx.scene.layout.VBox;
|
|
|
|
|
import javafx.scene.paint.Color;
|
|
|
|
|
import javafx.scene.shape.Circle;
|
|
|
|
|
import javafx.scene.text.Font;
|
|
|
|
|
import javafx.scene.text.FontWeight;
|
|
|
|
|
import javafx.scene.text.Text;
|
|
|
|
|
|
|
|
|
|
public class MainMenuView {
|
|
|
|
|
private Scene scene;
|
|
|
|
|
private final SceneManager sceneManager;
|
|
|
|
|
private String currentUsername;
|
|
|
|
|
|
|
|
|
|
// 添加界面组件引用,用于动态更新
|
|
|
|
|
private Label usernameLabel;
|
|
|
|
|
private Text avatarText;
|
|
|
|
|
private Label welcomeLabel;
|
|
|
|
|
|
|
|
|
|
public MainMenuView(SceneManager sceneManager) {
|
|
|
|
|
this.sceneManager = sceneManager;
|
|
|
|
|
this.currentUsername = sceneManager.getCurrentUserName();
|
|
|
|
|
createScene();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createScene() {
|
|
|
|
|
VBox root = new VBox(30);
|
|
|
|
|
root.setPadding(new Insets(50));
|
|
|
|
|
root.setAlignment(Pos.CENTER);
|
|
|
|
|
// 创建主容器
|
|
|
|
|
VBox mainContainer = new VBox();
|
|
|
|
|
mainContainer.setPadding(new Insets(20));
|
|
|
|
|
|
|
|
|
|
// 创建顶部用户信息栏
|
|
|
|
|
HBox userInfoBar = createUserInfoBar();
|
|
|
|
|
|
|
|
|
|
// 创建主菜单内容区域
|
|
|
|
|
VBox menuContent = createMenuContent();
|
|
|
|
|
|
|
|
|
|
mainContainer.getChildren().addAll(userInfoBar, menuContent);
|
|
|
|
|
|
|
|
|
|
scene = new Scene(mainContainer, 450, 550);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HBox createUserInfoBar() {
|
|
|
|
|
HBox userInfoBar = new HBox(15);
|
|
|
|
|
userInfoBar.setAlignment(Pos.CENTER_LEFT);
|
|
|
|
|
userInfoBar.setPadding(new Insets(0, 0, 30, 0));
|
|
|
|
|
userInfoBar.setStyle("-fx-border-color: #e0e0e0; -fx-border-width: 0 0 1 0; -fx-padding: 0 0 15 0;");
|
|
|
|
|
|
|
|
|
|
// 创建圆形头像容器
|
|
|
|
|
VBox avatarContainer = new VBox();
|
|
|
|
|
avatarContainer.setAlignment(Pos.CENTER);
|
|
|
|
|
avatarContainer.setPrefSize(50, 50);
|
|
|
|
|
|
|
|
|
|
// 创建圆形头像背景
|
|
|
|
|
Circle avatarCircle = new Circle(22);
|
|
|
|
|
avatarCircle.setFill(Color.web("#4CAF50"));
|
|
|
|
|
avatarCircle.setStroke(Color.WHITE);
|
|
|
|
|
avatarCircle.setStrokeWidth(2);
|
|
|
|
|
|
|
|
|
|
// 添加阴影效果
|
|
|
|
|
avatarCircle.setStyle("-fx-effect: drop shadow(gaussian, rgba(0,0,0,0.2), 5, 0.3, 2, 2);");
|
|
|
|
|
|
|
|
|
|
// 添加首字母文本
|
|
|
|
|
avatarText = new Text(getFirstLetter());
|
|
|
|
|
avatarText.setFill(Color.WHITE);
|
|
|
|
|
avatarText.setFont(Font.font("Arial", FontWeight.BOLD, 16));
|
|
|
|
|
|
|
|
|
|
// 使用StackPane将文本放在圆形中心
|
|
|
|
|
javafx.scene.layout.StackPane avatarStack = new javafx.scene.layout.StackPane();
|
|
|
|
|
avatarStack.getChildren().addAll(avatarCircle, avatarText);
|
|
|
|
|
avatarStack.setPrefSize(44, 44);
|
|
|
|
|
avatarStack.setAlignment(Pos.CENTER);
|
|
|
|
|
|
|
|
|
|
avatarContainer.getChildren().add(avatarStack);
|
|
|
|
|
|
|
|
|
|
// 用户名标签
|
|
|
|
|
usernameLabel = new Label(currentUsername != null ? currentUsername : "用户");
|
|
|
|
|
usernameLabel.setFont(Font.font("Arial", FontWeight.BOLD, 16));
|
|
|
|
|
usernameLabel.setStyle("-fx-text-fill: #2c3e50;");
|
|
|
|
|
|
|
|
|
|
// 间隔
|
|
|
|
|
Region spacer = new Region();
|
|
|
|
|
HBox.setHgrow(spacer, Priority.ALWAYS);
|
|
|
|
|
|
|
|
|
|
userInfoBar.getChildren().addAll(avatarContainer, usernameLabel, spacer);
|
|
|
|
|
|
|
|
|
|
return userInfoBar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private VBox createMenuContent() {
|
|
|
|
|
VBox menuContent = new VBox(25);
|
|
|
|
|
menuContent.setPadding(new Insets(20));
|
|
|
|
|
menuContent.setAlignment(Pos.CENTER);
|
|
|
|
|
|
|
|
|
|
Label titleLabel = new Label("数学学习软件");
|
|
|
|
|
titleLabel.setFont(Font.font(28));
|
|
|
|
|
titleLabel.setStyle("-fx-text-fill: #2c3e50;");
|
|
|
|
|
|
|
|
|
|
Label welcomeLabel = new Label("欢迎使用数学学习软件");
|
|
|
|
|
// 修改欢迎标签,显示用户名
|
|
|
|
|
welcomeLabel = new Label("欢迎," + (currentUsername != null ? currentUsername : "用户") + "!");
|
|
|
|
|
welcomeLabel.setFont(Font.font(18));
|
|
|
|
|
welcomeLabel.setStyle("-fx-text-fill: #7f8c8d;");
|
|
|
|
|
|
|
|
|
|
Button startButton = new Button("开始练习");
|
|
|
|
|
startButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-size: 16px;");
|
|
|
|
|
startButton.setPrefSize(200, 50);
|
|
|
|
|
startButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;");
|
|
|
|
|
startButton.setPrefSize(220, 55);
|
|
|
|
|
startButton.setOnMouseEntered(e -> startButton.setStyle("-fx-background-color: #45a049; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;"));
|
|
|
|
|
startButton.setOnMouseExited(e -> startButton.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;"));
|
|
|
|
|
|
|
|
|
|
Button changePasswordButton = new Button("修改密码");
|
|
|
|
|
changePasswordButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white; -fx-font-size: 16px;");
|
|
|
|
|
changePasswordButton.setPrefSize(200, 50);
|
|
|
|
|
changePasswordButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;");
|
|
|
|
|
changePasswordButton.setPrefSize(220, 55);
|
|
|
|
|
changePasswordButton.setOnMouseEntered(e -> changePasswordButton.setStyle("-fx-background-color: #1976D2; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;"));
|
|
|
|
|
changePasswordButton.setOnMouseExited(e -> changePasswordButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;"));
|
|
|
|
|
|
|
|
|
|
Button logoutButton = new Button("退出登录");
|
|
|
|
|
logoutButton.setStyle("-fx-background-color: #f44336; -fx-text-fill: white; -fx-font-size: 16px;");
|
|
|
|
|
logoutButton.setPrefSize(200, 50);
|
|
|
|
|
logoutButton.setStyle("-fx-background-color: #f44336; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;");
|
|
|
|
|
logoutButton.setPrefSize(220, 55);
|
|
|
|
|
logoutButton.setOnMouseEntered(e -> logoutButton.setStyle("-fx-background-color: #d32f2f; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;"));
|
|
|
|
|
logoutButton.setOnMouseExited(e -> logoutButton.setStyle("-fx-background-color: #f44336; -fx-text-fill: white; -fx-font-size: 16px; -fx-background-radius: 10;"));
|
|
|
|
|
|
|
|
|
|
startButton.setOnAction(e -> {
|
|
|
|
|
sceneManager.showLevelSelectionView();
|
|
|
|
|
@ -53,12 +142,31 @@ public class MainMenuView {
|
|
|
|
|
sceneManager.showLoginView();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
root.getChildren().addAll(
|
|
|
|
|
menuContent.getChildren().addAll(
|
|
|
|
|
titleLabel, welcomeLabel, startButton,
|
|
|
|
|
changePasswordButton, logoutButton
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
scene = new Scene(root, 400, 500);
|
|
|
|
|
return menuContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getFirstLetter() {
|
|
|
|
|
return currentUsername != null && !currentUsername.isEmpty() ?
|
|
|
|
|
currentUsername.substring(0, 1).toUpperCase() : "U";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加更新用户名的方法
|
|
|
|
|
public void updateUsername(String username) {
|
|
|
|
|
this.currentUsername = username;
|
|
|
|
|
if (usernameLabel != null) {
|
|
|
|
|
usernameLabel.setText(username != null ? username : "用户");
|
|
|
|
|
}
|
|
|
|
|
if (avatarText != null) {
|
|
|
|
|
avatarText.setText(getFirstLetter());
|
|
|
|
|
}
|
|
|
|
|
if (welcomeLabel != null) {
|
|
|
|
|
welcomeLabel.setText("欢迎," + (username != null ? username : "用户") + "!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showChangePasswordDialog() {
|
|
|
|
|
@ -66,19 +174,25 @@ public class MainMenuView {
|
|
|
|
|
javafx.scene.control.Dialog<Void> dialog = new javafx.scene.control.Dialog<>();
|
|
|
|
|
dialog.setTitle("修改密码");
|
|
|
|
|
dialog.setHeaderText("请输入密码信息");
|
|
|
|
|
dialog.setGraphic(null); // 移除默认图标
|
|
|
|
|
|
|
|
|
|
// 创建表单
|
|
|
|
|
javafx.scene.layout.GridPane grid = new javafx.scene.layout.GridPane();
|
|
|
|
|
grid.setHgap(10);
|
|
|
|
|
grid.setVgap(10);
|
|
|
|
|
grid.setPadding(new Insets(20, 150, 10, 10));
|
|
|
|
|
grid.setHgap(15);
|
|
|
|
|
grid.setVgap(15);
|
|
|
|
|
grid.setPadding(new Insets(20, 30, 10, 30));
|
|
|
|
|
|
|
|
|
|
javafx.scene.control.PasswordField oldPassword = new javafx.scene.control.PasswordField();
|
|
|
|
|
oldPassword.setPromptText("原密码");
|
|
|
|
|
oldPassword.setPromptText("请输入原密码");
|
|
|
|
|
oldPassword.setPrefWidth(200);
|
|
|
|
|
|
|
|
|
|
javafx.scene.control.PasswordField newPassword = new javafx.scene.control.PasswordField();
|
|
|
|
|
newPassword.setPromptText("新密码");
|
|
|
|
|
newPassword.setPromptText("请输入新密码");
|
|
|
|
|
newPassword.setPrefWidth(200);
|
|
|
|
|
|
|
|
|
|
javafx.scene.control.PasswordField confirmPassword = new javafx.scene.control.PasswordField();
|
|
|
|
|
confirmPassword.setPromptText("确认新密码");
|
|
|
|
|
confirmPassword.setPromptText("请确认新密码");
|
|
|
|
|
confirmPassword.setPrefWidth(200);
|
|
|
|
|
|
|
|
|
|
grid.add(new Label("原密码:"), 0, 0);
|
|
|
|
|
grid.add(oldPassword, 1, 0);
|
|
|
|
|
@ -98,7 +212,26 @@ public class MainMenuView {
|
|
|
|
|
dialog.setResultConverter(dialogButton -> {
|
|
|
|
|
if (dialogButton == javafx.scene.control.ButtonType.OK) {
|
|
|
|
|
// 这里可以添加修改密码的逻辑
|
|
|
|
|
System.out.println("修改密码功能待实现");
|
|
|
|
|
String oldPwd = oldPassword.getText();
|
|
|
|
|
String newPwd = newPassword.getText();
|
|
|
|
|
String confirmPwd = confirmPassword.getText();
|
|
|
|
|
|
|
|
|
|
if (oldPwd.isEmpty() || newPwd.isEmpty() || confirmPwd.isEmpty()) {
|
|
|
|
|
showAlert("错误", "请填写所有密码字段!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (!Login.login(currentUsername,oldPwd)){
|
|
|
|
|
showAlert("错误", "原密码错误!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (!newPwd.equals(confirmPwd)) {
|
|
|
|
|
showAlert("错误", "两次输入的新密码不一致!");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 调用修改密码的后端逻辑
|
|
|
|
|
ChangePassword.changePassword(currentUsername, confirmPwd);
|
|
|
|
|
showAlert("成功", "密码修改成功!");
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
@ -106,6 +239,14 @@ public class MainMenuView {
|
|
|
|
|
dialog.showAndWait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showAlert(String title, String message) {
|
|
|
|
|
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
|
|
|
|
alert.setTitle(title);
|
|
|
|
|
alert.setHeaderText(null);
|
|
|
|
|
alert.setContentText(message);
|
|
|
|
|
alert.showAndWait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Scene getScene() {
|
|
|
|
|
return scene;
|
|
|
|
|
}
|
|
|
|
|
|