新增修改密码功能

develop2
梁晨旭 2 months ago
parent da71917de0
commit b0a1eceeaa

@ -0,0 +1,149 @@
package com.example.mathsystemtogether;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
public class ChangePasswordController {
@FXML
private TextField usernameField;
@FXML
private PasswordField oldPasswordField;
@FXML
private PasswordField newPasswordField;
@FXML
private PasswordField confirmNewPasswordField;
@FXML
private Button changePasswordButton;
@FXML
private Button cancelButton;
@FXML
private Label statusLabel;
private String currentUser;
private ExamController examController;
private static final String USER_DATA_FILE = "user_data.txt";
public void setCurrentUser(String username) {
this.currentUser = username;
usernameField.setText(username);
}
public void setExamController(ExamController examController) {
this.examController = examController;
}
@FXML
private void handleChangePassword() {
String oldPassword = oldPasswordField.getText();
String newPassword = newPasswordField.getText();
String confirmNewPassword = confirmNewPasswordField.getText();
if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmNewPassword.isEmpty()) {
showStatus("请填写所有字段", true);
return;
}
if (!examController.checkUserPassword(currentUser, oldPassword)) {
showStatus("原密码错误", true);
return;
}
if (newPassword.length() < 6) {
showStatus("新密码至少需要6个字符", true);
return;
}
if (!newPassword.equals(confirmNewPassword)) {
showStatus("两次输入的新密码不一致", true);
return;
}
if (oldPassword.equals(newPassword)) {
showStatus("新密码不能与原密码相同", true);
return;
}
if (updatePassword(currentUser, newPassword)) {
showStatus("密码修改成功!", false);
// 延迟关闭窗口
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
javafx.application.Platform.runLater(() -> {
handleClose();
});
}
},
1500
);
} else {
showStatus("密码修改失败,请重试", true);
}
}
@FXML
private void handleCancel() {
handleClose();
}
private void handleClose() {
Stage stage = (Stage) cancelButton.getScene().getWindow();
stage.close();
}
private boolean updatePassword(String username, String newPassword) {
try {
// 读取所有用户数据
List<String> lines = Files.readAllLines(Paths.get(USER_DATA_FILE));
boolean userFound = false;
for (int i = 0; i < lines.size(); i++) {
String[] parts = lines.get(i).split("\\|");
if (parts.length >= 2 && parts[0].equals(username)) {
// 更新密码
parts[1] = newPassword;
lines.set(i, String.join("|", parts));
userFound = true;
break;
}
}
if (userFound) {
// 写回文件
Files.write(Paths.get(USER_DATA_FILE), lines);
// 更新内存中的用户数据
examController.updateUserPassword(username, newPassword);
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private void showStatus(String message, boolean isError) {
statusLabel.setText(message);
if (isError) {
statusLabel.setStyle("-fx-text-fill: #DC143C; -fx-font-weight: bold; -fx-font-size: 14;");
} else {
statusLabel.setStyle("-fx-text-fill: #228B22; -fx-font-weight: bold; -fx-font-size: 14;");
}
}
}

@ -33,6 +33,7 @@ public class ExamController {
@FXML private Button startExamButton;
@FXML private Button logoutButton;
@FXML private Label statusLabel;
@FXML private Button changePasswordButton;
// 数据成员
private Account currentAccount;
@ -80,7 +81,7 @@ public class ExamController {
if (!Files.exists(Paths.get(USER_DATA_FILE))) {
return;
}
List<String> lines = Files.readAllLines(Paths.get(USER_DATA_FILE));
for (String line : lines) {
String[] parts = line.split("\\|");
@ -88,14 +89,14 @@ public class ExamController {
String username = parts[0];
String password = parts[1];
String levelStr = parts[3];
Level level;
try {
level = Level.valueOf(levelStr);
} catch (IllegalArgumentException e) {
level = Level.; // 默认级别
}
userMap.put(username, new Account(username, password, level));
}
}
@ -140,7 +141,7 @@ public class ExamController {
registerStage.setScene(scene);
registerStage.setResizable(false);
registerStage.show();
} catch (Exception e) {
loginStatusLabel.setText("打开注册界面失败:" + e.getMessage());
loginStatusLabel.setStyle("-fx-text-fill: red;");
@ -197,6 +198,48 @@ public class ExamController {
}
}
@FXML
private void handleChangePassword() {
if (currentAccount == null) {
statusLabel.setText("请先登录");
statusLabel.setStyle("-fx-text-fill: red;");
return;
}
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("change-password-view.fxml"));
Scene scene = new Scene(loader.load(), 600, 500);
Stage changePasswordStage = new Stage();
changePasswordStage.setTitle("修改密码");
changePasswordStage.setScene(scene);
changePasswordStage.setResizable(false);
// 传递当前用户信息给密码修改控制器
ChangePasswordController controller = loader.getController();
controller.setCurrentUser(currentAccount.username);
controller.setExamController(this);
changePasswordStage.show();
} catch (Exception e) {
statusLabel.setText("打开修改密码界面失败:" + e.getMessage());
statusLabel.setStyle("-fx-text-fill: red;");
}
}
// 供 ChangePasswordController 调用的方法
public boolean checkUserPassword(String username, String password) {
Account account = userMap.get(username);
return account != null && account.password.equals(password);
}
// 更新用户密码
public void updateUserPassword(String username, String newPassword) {
Account account = userMap.get(username);
if (account != null) {
account.password = newPassword;
}
}
private void startExam() {
try {
// 打开专门的考试界面
@ -240,4 +283,4 @@ public class ExamController {
enum Level {
, ,
}
}
}

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.effect.DropShadow?>
<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.mathsystemtogether.ChangePasswordController"
spacing="20.0" style="-fx-background-color: linear-gradient(to bottom, #6A0DAD, #4B0082, #2E0854);"
prefHeight="500.0" prefWidth="600.0">
<padding>
<Insets bottom="30.0" left="40.0" right="40.0" top="30.0"/>
</padding>
<VBox spacing="15.0" style="-fx-background-color: linear-gradient(to right, #8A2BE2, #9932CC, #8B008B); -fx-background-radius: 15; -fx-padding: 20;">
<Label text="🔐 修改密码" textFill="white" textAlignment="CENTER" style="-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 10, 0, 0, 2);">
<font>
<Font name="System Bold" size="24.0"/>
</font>
</Label>
</VBox>
<VBox spacing="20.0" style="-fx-background-color: linear-gradient(to bottom, #E6E6FA, #D8BFD8, #DDA0DD); -fx-padding: 25; -fx-background-radius: 15; -fx-effect: dropshadow(gaussian, rgba(139,0,139,0.3), 15, 0, 0, 5);">
<HBox spacing="15.0" alignment="CENTER_LEFT">
<Label text="👤 用户名:" minWidth="100.0" textFill="#6A0DAD">
<font>
<Font name="System Bold" size="14.0"/>
</font>
</Label>
<TextField fx:id="usernameField" editable="false" prefWidth="250.0" style="-fx-background-color: white; -fx-background-radius: 8; -fx-border-color: #8A2BE2; -fx-border-radius: 8; -fx-border-width: 2; -fx-padding: 8;"/>
</HBox>
<HBox spacing="15.0" alignment="CENTER_LEFT">
<Label text="🔑 原密码:" minWidth="100.0" textFill="#6A0DAD">
<font>
<Font name="System Bold" size="14.0"/>
</font>
</Label>
<PasswordField fx:id="oldPasswordField" prefWidth="250.0" style="-fx-background-color: white; -fx-background-radius: 8; -fx-border-color: #8A2BE2; -fx-border-radius: 8; -fx-border-width: 2; -fx-padding: 8;"/>
</HBox>
<HBox spacing="15.0" alignment="CENTER_LEFT">
<Label text="🆕 新密码:" minWidth="100.0" textFill="#6A0DAD">
<font>
<Font name="System Bold" size="14.0"/>
</font>
</Label>
<PasswordField fx:id="newPasswordField" prefWidth="250.0" style="-fx-background-color: white; -fx-background-radius: 8; -fx-border-color: #8A2BE2; -fx-border-radius: 8; -fx-border-width: 2; -fx-padding: 8;"/>
</HBox>
<HBox spacing="15.0" alignment="CENTER_LEFT">
<Label text="✅ 确认密码:" minWidth="100.0" textFill="#6A0DAD">
<font>
<Font name="System Bold" size="14.0"/>
</font>
</Label>
<PasswordField fx:id="confirmNewPasswordField" prefWidth="250.0" style="-fx-background-color: white; -fx-background-radius: 8; -fx-border-color: #8A2BE2; -fx-border-radius: 8; -fx-border-width: 2; -fx-padding: 8;"/>
</HBox>
<HBox spacing="20.0" alignment="CENTER">
<Button fx:id="changePasswordButton" text="💾 保存修改" onAction="#handleChangePassword" style="-fx-background-color: linear-gradient(to right, #32CD32, #228B22); -fx-text-fill: white; -fx-background-radius: 10; -fx-padding: 12 25; -fx-font-size: 14; -fx-font-weight: bold; -fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);"/>
<Button fx:id="cancelButton" text="❌ 取消" onAction="#handleCancel" style="-fx-background-color: linear-gradient(to right, #DC143C, #B22222); -fx-text-fill: white; -fx-background-radius: 10; -fx-padding: 12 25; -fx-font-size: 14; -fx-font-weight: bold; -fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);"/>
</HBox>
<Label fx:id="statusLabel" textFill="#8B0000" style="-fx-font-weight: bold; -fx-font-size: 14;" textAlignment="CENTER"/>
</VBox>
</VBox>

@ -87,11 +87,13 @@
<Button fx:id="startExamButton" text="🚀 开始考试" onAction="#handleStartExam" style="-fx-background-color: linear-gradient(to right, #8A2BE2, #9932CC); -fx-text-fill: white; -fx-background-radius: 10; -fx-padding: 12 25; -fx-font-size: 14; -fx-font-weight: bold; -fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);"/>
</HBox>
<HBox spacing="15.0" alignment="CENTER_LEFT">
<Button fx:id="logoutButton" text="🚪 退出登录" onAction="#handleLogout" style="-fx-background-color: linear-gradient(to right, #DC143C, #B22222); -fx-text-fill: white; -fx-background-radius: 10; -fx-padding: 10 20; -fx-font-size: 14; -fx-font-weight: bold; -fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);"/>
</HBox>
<HBox spacing="15.0" alignment="CENTER_LEFT">
<Button fx:id="logoutButton" text="🚪 退出登录" onAction="#handleLogout" style="-fx-background-color: linear-gradient(to right, #DC143C, #B22222); -fx-text-fill: white; -fx-background-radius: 10; -fx-padding: 10 20; -fx-font-size: 14; -fx-font-weight: bold; -fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);"/>
<Button fx:id="changePasswordButton" text="🔑 修改密码" onAction="#handleChangePassword" style="-fx-background-color: linear-gradient(to right, #FF8C00, #FFA500); -fx-text-fill: white; -fx-background-radius: 10; -fx-padding: 10 20; -fx-font-size: 14; -fx-font-weight: bold; -fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);"/>
</HBox>
<Label fx:id="statusLabel" textFill="#4B0082" style="-fx-font-weight: bold; -fx-font-size: 14;"/>
<Label fx:id="statusLabel" textFill="#4B0082" style="-fx-font-weight: bold; -fx-font-size: 14;"/>
</VBox>

@ -1,2 +1,2 @@
wgll|123456|ymhlovesLQX@163.com|小学|1760162416490
666|123456789|252436951@qq.com|小学|1760166999971
wgll|123456|ymhlovesLQX@163.com|小学|1760162416490
666|123456789|252436951@qq.com|小学|1760166999971

Loading…
Cancel
Save