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;");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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>
|
||||
@ -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…
Reference in new issue