增加修改密码功能,增加密码限制提示,界面再次优化

jinzhibo_branch
imok 7 months ago
parent 670feecce6
commit 92903fce01

@ -36,6 +36,14 @@ public class AuthController {
return password.equals(confirmPassword) && userService.completeRegistration(email, code, password);
}
// 新增修改密码方法
public boolean changePassword(String currentPassword, String newPassword) {
if (currentUser == null) {
return false;
}
return userService.changePassword(currentUser, currentPassword, newPassword);
}
public void showLogin() {
new LoginFrame(this);
}
@ -43,4 +51,9 @@ public class AuthController {
public void showRegister() {
new RegisterFrame(this);
}
// 获取当前用户
public String getCurrentUser() {
return currentUser;
}
}

@ -14,20 +14,47 @@ public class ExamController {
public void startExam(String difficulty) {
try {
String input = JOptionPane.showInputDialog("请输入题目数量:");
// 显示题目数量输入对话框包含10-30的限制提示
String input = JOptionPane.showInputDialog(
null,
"请输入题目数量 (10-30题):",
"题目数量",
JOptionPane.QUESTION_MESSAGE
);
if (input != null) {
int count = Integer.parseInt(input);
if (count > 0) {
int count = Integer.parseInt(input.trim());
if (count >= 10 && count <= 30) {
List<model.Question> questions = questionService.generateQuestions(difficulty, count);
new ExamFrame(this, questions);
if (questions != null && !questions.isEmpty()) {
new ExamFrame(this, questions);
} else {
JOptionPane.showMessageDialog(null, "生成题目失败,请重试");
mainController.showMainFrame();
}
} else {
JOptionPane.showMessageDialog(null, "题目数量必须大于0");
JOptionPane.showMessageDialog(null,
"题目数量必须在10-30之间\n当前输入" + count + "题",
"输入错误",
JOptionPane.ERROR_MESSAGE);
mainController.showMainFrame();
}
} else {
// 用户取消输入,返回主界面
mainController.showMainFrame();
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "请输入有效数字");
JOptionPane.showMessageDialog(null,
"请输入有效数字!",
"输入错误",
JOptionPane.ERROR_MESSAGE);
mainController.showMainFrame();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "生成题目时出现错误: " + e.getMessage());
JOptionPane.showMessageDialog(null,
"生成题目时出现错误: " + e.getMessage(),
"错误",
JOptionPane.ERROR_MESSAGE);
mainController.showMainFrame();
}
}

@ -1,6 +1,8 @@
package controller;
import view.*;
import javax.swing.*;
public class MainController {
private AuthController authController = new AuthController(this);
private ExamController examController = new ExamController(this);
@ -38,6 +40,15 @@ public class MainController {
mainFrame.requestFocus();
}
/**
*
*/
public void showChangePassword() {
if (mainFrame != null) {
new ChangePasswordFrame(mainFrame, authController);
}
}
/**
*
* @param difficulty
@ -80,6 +91,13 @@ public class MainController {
return mainFrame;
}
/**
*
*/
public String getCurrentUser() {
return authController.getCurrentUser();
}
/**
* 退
*/
@ -90,4 +108,33 @@ public class MainController {
}
System.exit(0);
}
/**
*
*/
public void startApplication() {
// 启动时显示登录界面
showLogin();
}
/**
*
*/
public void handleExamCompletion() {
showMainFrame();
}
/**
*
*/
public void handlePasswordChangeSuccess() {
// 可以在这里添加一些成功后的处理逻辑
// 比如显示成功消息或者记录日志等
if (mainFrame != null) {
JOptionPane.showMessageDialog(mainFrame,
"密码修改成功!",
"成功",
JOptionPane.INFORMATION_MESSAGE);
}
}
}

@ -0,0 +1,136 @@
package view;
import controller.AuthController;
import javax.swing.*;
import java.awt.*;
public class ChangePasswordFrame extends JDialog {
private AuthController controller;
public ChangePasswordFrame(JFrame parent, AuthController controller) {
super(parent, "修改密码", true);
this.controller = controller;
initializeUI();
}
private void initializeUI() {
setSize(400, 300);
setLocationRelativeTo(getParent());
setResizable(false);
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(8, 8, 8, 8);
JPasswordField currentPasswordField = new JPasswordField();
JPasswordField newPasswordField = new JPasswordField();
JPasswordField confirmPasswordField = new JPasswordField();
JButton submitBtn = new JButton("确认修改");
JButton cancelBtn = new JButton("取消");
// 设置字体
Font labelFont = new Font("微软雅黑", Font.PLAIN, 14);
Font fieldFont = new Font("微软雅黑", Font.PLAIN, 14);
Font buttonFont = new Font("微软雅黑", Font.PLAIN, 14);
// 设置组件尺寸
Dimension fieldSize = new Dimension(200, 35);
currentPasswordField.setPreferredSize(fieldSize);
newPasswordField.setPreferredSize(fieldSize);
confirmPasswordField.setPreferredSize(fieldSize);
Dimension buttonSize = new Dimension(120, 35);
submitBtn.setPreferredSize(buttonSize);
cancelBtn.setPreferredSize(buttonSize);
// 当前密码行
gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 0.3;
JLabel currentLabel = new JLabel("当前密码:");
currentLabel.setFont(labelFont);
panel.add(currentLabel, gbc);
gbc.gridx = 1; gbc.gridy = 0; gbc.weightx = 0.7;
currentPasswordField.setFont(fieldFont);
panel.add(currentPasswordField, gbc);
// 新密码行
gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 0.3;
JLabel newLabel = new JLabel("新密码:");
newLabel.setFont(labelFont);
panel.add(newLabel, gbc);
gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 0.7;
newPasswordField.setFont(fieldFont);
panel.add(newPasswordField, gbc);
// 确认密码行
gbc.gridx = 0; gbc.gridy = 2; gbc.weightx = 0.3;
JLabel confirmLabel = new JLabel("确认密码:");
confirmLabel.setFont(labelFont);
panel.add(confirmLabel, gbc);
gbc.gridx = 1; gbc.gridy = 2; gbc.weightx = 0.7;
confirmPasswordField.setFont(fieldFont);
panel.add(confirmPasswordField, gbc);
// 提示信息
gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2;
JLabel hintLabel = new JLabel("密码要求6-10位包含大小写字母和数字");
hintLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
hintLabel.setForeground(Color.GRAY);
panel.add(hintLabel, gbc);
// 按钮行
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0));
submitBtn.setFont(buttonFont);
cancelBtn.setFont(buttonFont);
buttonPanel.add(submitBtn);
buttonPanel.add(cancelBtn);
gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2;
panel.add(buttonPanel, gbc);
// 事件监听
submitBtn.addActionListener(e -> {
String currentPassword = new String(currentPasswordField.getPassword());
String newPassword = new String(newPasswordField.getPassword());
String confirmPassword = new String(confirmPasswordField.getPassword());
if (currentPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
JOptionPane.showMessageDialog(this, "请填写所有字段", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
if (!newPassword.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "两次输入的新密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
boolean success = controller.changePassword(currentPassword, newPassword);
if (success) {
JOptionPane.showMessageDialog(this,
"密码修改成功!",
"成功",
JOptionPane.INFORMATION_MESSAGE);
dispose();
} else {
JOptionPane.showMessageDialog(this,
"密码修改失败!\n可能的原因\n- 当前密码错误\n- 新密码不符合要求",
"失败",
JOptionPane.ERROR_MESSAGE);
}
});
cancelBtn.addActionListener(e -> {
dispose();
});
// 添加回车键支持
getRootPane().setDefaultButton(submitBtn);
add(panel);
setVisible(true);
}
}

@ -18,7 +18,7 @@ public class ExamFrame extends JFrame {
this.answers = new int[questions.size()];
for (int i = 0; i < answers.length; i++) answers[i] = -1;
setTitle("考试");
setTitle("考试 - 共" + questions.size() + "题");
setSize(500, 300);
setLocationRelativeTo(null);

@ -6,25 +6,75 @@ import java.awt.*;
public class LoginFrame extends JFrame {
public LoginFrame(AuthController controller) {
setTitle("登录");
setSize(300, 200);
setSize(400, 300); // 增大窗口尺寸
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JPanel panel = new JPanel(new GridBagLayout()); // 使用 GridBagLayout 更灵活布局
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30)); // 增大边距
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 10, 10, 10); // 增大间距
JTextField emailField = new JTextField();
JPasswordField passwordField = new JPasswordField();
JButton loginBtn = new JButton("登录");
JButton registerBtn = new JButton("注册");
panel.add(new JLabel("邮箱:"));
panel.add(emailField);
panel.add(new JLabel("密码:"));
panel.add(passwordField);
panel.add(loginBtn);
panel.add(registerBtn);
// 设置组件字体
Font labelFont = new Font("微软雅黑", Font.PLAIN, 16);
Font fieldFont = new Font("微软雅黑", Font.PLAIN, 14);
Font buttonFont = new Font("微软雅黑", Font.PLAIN, 14);
JLabel emailLabel = new JLabel("邮箱:");
JLabel passwordLabel = new JLabel("密码:");
emailLabel.setFont(labelFont);
passwordLabel.setFont(labelFont);
emailField.setFont(fieldFont);
passwordField.setFont(fieldFont);
loginBtn.setFont(buttonFont);
registerBtn.setFont(buttonFont);
// 设置输入框和按钮的推荐大小
Dimension fieldSize = new Dimension(200, 35); // 增大输入框尺寸
emailField.setPreferredSize(fieldSize);
emailField.setMinimumSize(fieldSize);
passwordField.setPreferredSize(fieldSize);
passwordField.setMinimumSize(fieldSize);
Dimension buttonSize = new Dimension(100, 40); // 增大按钮尺寸
loginBtn.setPreferredSize(buttonSize);
registerBtn.setPreferredSize(buttonSize);
// 邮箱行
gbc.gridx = 0; gbc.gridy = 0;
gbc.weightx = 0.3;
panel.add(emailLabel, gbc);
gbc.gridx = 1; gbc.gridy = 0;
gbc.weightx = 0.7;
panel.add(emailField, gbc);
// 密码行
gbc.gridx = 0; gbc.gridy = 1;
gbc.weightx = 0.3;
panel.add(passwordLabel, gbc);
gbc.gridx = 1; gbc.gridy = 1;
gbc.weightx = 0.7;
panel.add(passwordField, gbc);
// 按钮行
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0)); // 增大按钮间距
buttonPanel.add(loginBtn);
buttonPanel.add(registerBtn);
gbc.gridx = 0; gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.weightx = 1.0;
panel.add(buttonPanel, gbc);
loginBtn.addActionListener(e -> {
String email = emailField.getText();
String password = new String(passwordField.getPassword());
@ -34,12 +84,15 @@ public class LoginFrame extends JFrame {
JOptionPane.showMessageDialog(this, "登录失败");
}
});
registerBtn.addActionListener(e -> {
dispose();
controller.showRegister();
});
// 添加回车键提交支持
getRootPane().setDefaultButton(loginBtn);
add(panel);
setVisible(true);
}

@ -15,53 +15,97 @@ public class MainFrame extends JFrame {
private void initializeWindow() {
setTitle("数学学习系统 - 主界面");
setSize(300, 250);
setSize(500, 550); // 稍微增加高度以容纳新按钮
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}
private void createComponents() {
JPanel panel = new JPanel(new GridLayout(5, 1, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JLabel titleLabel = new JLabel("选择考试难度", JLabel.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
// 使用 BorderLayout 作为主布局
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));
// 标题面板
JPanel titlePanel = new JPanel(new FlowLayout());
JLabel titleLabel = new JLabel("选择考试难度");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
titlePanel.add(titleLabel);
// 用户信息面板(显示当前登录用户)
JPanel userPanel = new JPanel(new FlowLayout());
JLabel userLabel = new JLabel("当前用户: " + controller.getCurrentUser());
userLabel.setForeground(Color.BLUE);
userLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
userPanel.add(userLabel);
// 提示面板
JPanel hintPanel = new JPanel(new FlowLayout());
JLabel hintLabel = new JLabel("题目数量限制10-30题");
hintLabel.setForeground(Color.GRAY);
hintLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
hintPanel.add(hintLabel);
// 按钮面板 - 使用 GridLayout 让按钮垂直排列
JPanel buttonPanel = new JPanel(new GridLayout(5, 1, 15, 15)); // 改为5行1列
buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
JButton primaryBtn = new JButton("小学");
JButton middleBtn = new JButton("初中");
JButton highBtn = new JButton("高中");
JButton changePasswordBtn = new JButton("修改密码"); // 新增修改密码按钮
JButton logoutBtn = new JButton("退出登录");
// 设置按钮字体
Font buttonFont = new Font("微软雅黑", Font.PLAIN, 14);
// 设置按钮字体和大小
Font buttonFont = new Font("微软雅黑", Font.PLAIN, 16);
primaryBtn.setFont(buttonFont);
middleBtn.setFont(buttonFont);
highBtn.setFont(buttonFont);
changePasswordBtn.setFont(buttonFont);
logoutBtn.setFont(buttonFont);
// 设置按钮大小
Dimension buttonSize = new Dimension(250, 50);
primaryBtn.setPreferredSize(buttonSize);
middleBtn.setPreferredSize(buttonSize);
highBtn.setPreferredSize(buttonSize);
changePasswordBtn.setPreferredSize(buttonSize);
logoutBtn.setPreferredSize(buttonSize);
// 设置按钮颜色
primaryBtn.setBackground(new Color(173, 216, 230)); // 浅蓝色
middleBtn.setBackground(new Color(144, 238, 144)); // 浅绿色
highBtn.setBackground(new Color(255, 182, 193)); // 浅粉色
logoutBtn.setBackground(new Color(240, 240, 240)); // 浅灰色
primaryBtn.setBackground(new Color(173, 216, 230));
middleBtn.setBackground(new Color(144, 238, 144));
highBtn.setBackground(new Color(255, 182, 193));
changePasswordBtn.setBackground(new Color(255, 255, 150)); // 黄色突出显示
logoutBtn.setBackground(new Color(240, 240, 240));
// 设置按钮边框
primaryBtn.setBorder(BorderFactory.createRaisedBevelBorder());
middleBtn.setBorder(BorderFactory.createRaisedBevelBorder());
highBtn.setBorder(BorderFactory.createRaisedBevelBorder());
changePasswordBtn.setBorder(BorderFactory.createRaisedBevelBorder());
logoutBtn.setBorder(BorderFactory.createRaisedBevelBorder());
// 按钮事件监听
primaryBtn.addActionListener(e -> {
setVisible(false); // 隐藏主界面
setVisible(false);
controller.startExam("小学");
});
middleBtn.addActionListener(e -> {
setVisible(false); // 隐藏主界面
setVisible(false);
controller.startExam("初中");
});
highBtn.addActionListener(e -> {
setVisible(false); // 隐藏主界面
setVisible(false);
controller.startExam("高中");
});
changePasswordBtn.addActionListener(e -> {
controller.showChangePassword();
});
logoutBtn.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog(
this,
@ -76,12 +120,22 @@ public class MainFrame extends JFrame {
}
});
// 添加组件到面板
panel.add(titleLabel);
panel.add(primaryBtn);
panel.add(middleBtn);
panel.add(highBtn);
panel.add(logoutBtn);
// 添加按钮到按钮面板
buttonPanel.add(primaryBtn);
buttonPanel.add(middleBtn);
buttonPanel.add(highBtn);
buttonPanel.add(changePasswordBtn);
buttonPanel.add(logoutBtn);
// 创建主内容面板
JPanel contentPanel = new JPanel(new BorderLayout(0, 10));
contentPanel.add(userPanel, BorderLayout.NORTH);
contentPanel.add(hintPanel, BorderLayout.CENTER);
contentPanel.add(buttonPanel, BorderLayout.SOUTH);
// 将所有面板添加到主面板
panel.add(titlePanel, BorderLayout.NORTH);
panel.add(contentPanel, BorderLayout.CENTER);
add(panel);
}
@ -91,13 +145,10 @@ public class MainFrame extends JFrame {
*/
public void showFrame() {
setVisible(true);
toFront(); // 确保窗口在前台
requestFocus(); // 请求焦点
toFront();
requestFocus();
}
/**
* dispose 退
*/
@Override
public void dispose() {
super.dispose();

@ -6,16 +6,16 @@ import java.awt.*;
public class RegisterFrame extends JFrame {
public RegisterFrame(AuthController controller) {
setTitle("注册");
setSize(400, 350);
setSize(500, 450); // 增大窗口尺寸
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 使用 GridBagLayout 更灵活布局
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
panel.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 25));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.insets = new Insets(8, 8, 8, 8);
JTextField emailField = new JTextField();
JTextField codeField = new JTextField();
@ -25,51 +25,109 @@ public class RegisterFrame extends JFrame {
JButton registerBtn = new JButton("注册");
JButton backBtn = new JButton("返回");
// 设置字体
Font labelFont = new Font("微软雅黑", Font.PLAIN, 16);
Font fieldFont = new Font("微软雅黑", Font.PLAIN, 14);
Font buttonFont = new Font("微软雅黑", Font.PLAIN, 14);
Font hintFont = new Font("微软雅黑", Font.PLAIN, 12);
// 设置组件尺寸
Dimension fieldSize = new Dimension(250, 35);
emailField.setPreferredSize(fieldSize);
codeField.setPreferredSize(fieldSize);
passwordField.setPreferredSize(fieldSize);
confirmField.setPreferredSize(fieldSize);
Dimension buttonSize = new Dimension(120, 35);
sendCodeBtn.setPreferredSize(new Dimension(120, 30));
registerBtn.setPreferredSize(buttonSize);
backBtn.setPreferredSize(buttonSize);
// 邮箱行
gbc.gridx = 0; gbc.gridy = 0;
panel.add(new JLabel("邮箱:"), gbc);
gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 2;
gbc.weightx = 1.0;
gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 0.3;
JLabel emailLabel = new JLabel("邮箱:");
emailLabel.setFont(labelFont);
panel.add(emailLabel, gbc);
gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 2; gbc.weightx = 0.7;
emailField.setFont(fieldFont);
panel.add(emailField, gbc);
// 验证码行 - 修复布局
gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.weightx = 0;
panel.add(new JLabel("验证码:"), gbc);
gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 1.0;
// 验证码行
gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.weightx = 0.3;
JLabel codeLabel = new JLabel("验证码:");
codeLabel.setFont(labelFont);
panel.add(codeLabel, gbc);
gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 1; gbc.weightx = 0.5;
codeField.setFont(fieldFont);
panel.add(codeField, gbc);
gbc.gridx = 2; gbc.gridy = 1; gbc.weightx = 0;
gbc.gridx = 2; gbc.gridy = 1; gbc.gridwidth = 1; gbc.weightx = 0.2;
gbc.fill = GridBagConstraints.NONE;
sendCodeBtn.setFont(buttonFont);
panel.add(sendCodeBtn, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL; // 恢复填充
gbc.fill = GridBagConstraints.HORIZONTAL;
// 密码行
gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 1; gbc.weightx = 0;
panel.add(new JLabel("密码:"), gbc);
gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 2; gbc.weightx = 1.0;
gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 1; gbc.weightx = 0.3;
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setFont(labelFont);
panel.add(passwordLabel, gbc);
gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 2; gbc.weightx = 0.7;
passwordField.setFont(fieldFont);
panel.add(passwordField, gbc);
// 密码提示行
gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 2; gbc.weightx = 0.7;
JLabel passwordHintLabel = new JLabel("密码要求6-10位必须包含大小写字母和数字");
passwordHintLabel.setFont(hintFont);
passwordHintLabel.setForeground(Color.GRAY);
panel.add(passwordHintLabel, gbc);
// 确认密码行
gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 1; gbc.weightx = 0;
panel.add(new JLabel("确认密码:"), gbc);
gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 2; gbc.weightx = 1.0;
gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 1; gbc.weightx = 0.3;
JLabel confirmLabel = new JLabel("确认密码:");
confirmLabel.setFont(labelFont);
panel.add(confirmLabel, gbc);
gbc.gridx = 1; gbc.gridy = 4; gbc.gridwidth = 2; gbc.weightx = 0.7;
confirmField.setFont(fieldFont);
panel.add(confirmField, gbc);
// 按钮行 - 添加返回按钮
JPanel buttonPanel = new JPanel(new FlowLayout());
// 确认密码提示行
gbc.gridx = 1; gbc.gridy = 5; gbc.gridwidth = 2; gbc.weightx = 0.7;
JLabel confirmHintLabel = new JLabel("请再次输入密码");
confirmHintLabel.setFont(hintFont);
confirmHintLabel.setForeground(Color.GRAY);
panel.add(confirmHintLabel, gbc);
// 按钮行
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0));
registerBtn.setFont(buttonFont);
backBtn.setFont(buttonFont);
buttonPanel.add(registerBtn);
buttonPanel.add(backBtn);
gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 3;
gbc.weightx = 1.0;
gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 3; gbc.weightx = 1.0;
panel.add(buttonPanel, gbc);
// 发送验证码按钮事件监听器
sendCodeBtn.addActionListener(e -> {
String email = emailField.getText().trim();
if (email.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入邮箱地址", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
// 简单的邮箱格式验证
if (!isValidEmail(email)) {
JOptionPane.showMessageDialog(this, "请输入有效的邮箱地址", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
// 禁用按钮
sendCodeBtn.setEnabled(false);
sendCodeBtn.setText("发送中...");
@ -82,21 +140,38 @@ public class RegisterFrame extends JFrame {
sendCodeBtn.setEnabled(true);
sendCodeBtn.setText("发送验证码");
if (result != null && !result.startsWith("already") &&
!result.startsWith("code") && !result.startsWith("invalid") &&
!result.startsWith("send")) {
// 成功发送,只显示通用提示
JOptionPane.showMessageDialog(this,
"验证码已发送到您的邮箱,请查收",
"发送成功",
JOptionPane.INFORMATION_MESSAGE);
} else if (result != null) {
// 处理错误情况(根据你的 AuthController 返回值调整)
JOptionPane.showMessageDialog(this,
"发送失败,请检查邮箱地址",
"错误",
JOptionPane.ERROR_MESSAGE);
if (result != null) {
// 根据返回结果判断不同的情况
if (result.equals("registered")) {
// 邮箱已注册
JOptionPane.showMessageDialog(this,
"该邮箱已被注册,请直接登录",
"邮箱已注册",
JOptionPane.WARNING_MESSAGE);
} else if (result.equals("recheck your email")) {
// 验证码已发送过
JOptionPane.showMessageDialog(this,
"验证码已发送到您的邮箱,请查收\n如未收到请稍后再试",
"验证码已发送",
JOptionPane.INFORMATION_MESSAGE);
} else if (!result.startsWith("already") &&
!result.startsWith("code") &&
!result.startsWith("invalid") &&
!result.startsWith("send")) {
// 成功发送验证码
JOptionPane.showMessageDialog(this,
"验证码已发送到您的邮箱,请查收",
"发送成功",
JOptionPane.INFORMATION_MESSAGE);
} else {
// 其他错误情况
JOptionPane.showMessageDialog(this,
"发送失败,请检查邮箱地址",
"错误",
JOptionPane.ERROR_MESSAGE);
}
} else {
// 返回null的情况
JOptionPane.showMessageDialog(this,
"注册失败,请重试",
"错误",
@ -129,6 +204,14 @@ public class RegisterFrame extends JFrame {
return;
}
if (!isValidPassword(password)) {
JOptionPane.showMessageDialog(this,
"密码不符合要求!\n密码必须\n- 6-10位长度\n- 包含大小写字母\n- 包含数字",
"密码错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
return;
@ -144,7 +227,7 @@ public class RegisterFrame extends JFrame {
controller.showLogin();
} else {
JOptionPane.showMessageDialog(this,
"注册失败!\n可能的原因\n- 验证码错误\n- 密码不符合要求6-10位包含大小写字母和数字\n- 邮箱已被注册",
"注册失败!\n可能的原因\n- 验证码错误\n- 密码不符合要求\n- 邮箱已被注册",
"注册失败",
JOptionPane.ERROR_MESSAGE);
}
@ -161,4 +244,29 @@ public class RegisterFrame extends JFrame {
add(panel);
setVisible(true);
}
/**
*
*/
private boolean isValidPassword(String password) {
if (password.length() < 6 || password.length() > 10) {
return false;
}
boolean hasUpper = false, hasLower = false, hasDigit = false;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) hasUpper = true;
if (Character.isLowerCase(c)) hasLower = true;
if (Character.isDigit(c)) hasDigit = true;
}
return hasUpper && hasLower && hasDigit;
}
/**
*
*/
private boolean isValidEmail(String email) {
return email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
}
}

@ -6,31 +6,48 @@ import java.awt.*;
public class ResultFrame extends JFrame {
public ResultFrame(ExamController controller, int score, int total) {
setTitle("考试结果");
setSize(250, 150);
setSize(300, 200);
setLocationRelativeTo(null);
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("得分: " + score + "/" + total, JLabel.CENTER);
// 创建结果信息面板
JPanel resultPanel = new JPanel(new GridLayout(3, 1));
JLabel scoreLabel = new JLabel("得分: " + score + "/" + total, JLabel.CENTER);
JLabel totalLabel = new JLabel("总题数: " + total + "题", JLabel.CENTER);
double percentage = (double) score / total * 100;
JLabel percentageLabel = new JLabel(String.format("正确率: %.1f%%", percentage), JLabel.CENTER);
// 设置字体
Font boldFont = new Font("微软雅黑", Font.BOLD, 16);
scoreLabel.setFont(boldFont);
totalLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
percentageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
resultPanel.add(scoreLabel);
resultPanel.add(totalLabel);
resultPanel.add(percentageLabel);
JButton continueBtn = new JButton("继续做题");
JButton exitBtn = new JButton("退出");
continueBtn.addActionListener(e -> {
dispose();
controller.returnToMain();
});
exitBtn.addActionListener(e -> {
dispose();
controller.returnToMain();
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(continueBtn);
buttonPanel.add(exitBtn);
panel.add(label, BorderLayout.CENTER);
panel.add(resultPanel, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
add(panel);
setVisible(true);
}

Loading…
Cancel
Save