Compare commits

..

3 Commits

Author SHA1 Message Date
fanxuerun b91967dee8 markdown
4 months ago
fanxuerun 95c97cf91a edition2
4 months ago
fanxuerun e25a555a5f edtion1
4 months ago

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: MathLearningApp
Class-Path: .

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,6 +1,21 @@
在windows11 powershell下运行jdk-22
在运行前确保安装了curl可以在powershell中输入
curl.exe --version查看版本 我的是8.14.1
运行前先输入命令
[Console]::InputEncoding=[System.Text.Encoding]::UTF8
[Console]::OutputEncoding=[System.Text.Encoding]::UTF8
# 数学题学习应用
********
## 1.项目使用
项目目录下有一个MathLearningApp.jar使用jdk-22在windows11 powershell下运行。
在运行前,先输入命令
`[Console]::InputEncoding=[System.Text.Encoding]::UTF8`
`[Console]::OutputEncoding=[System.Text.Encoding]::UTF8`
再确保安装了curl可以在powershell中输入
`curl.exe --version`
来查看curl的版本信息我使用的是8.14.1
然后使用命令
`java -jar MathLeaningApp.jar` 运行目录下会出现一个存储用户数据的文件
********
## 2.项目说明
controller、view目录负责前端
model、service、util目录负责后端
MathLearningApp.java是程序入口

Binary file not shown.

Binary file not shown.

@ -1,5 +1,5 @@
import controller.MainController;
import javax.swing.*;
import javax.swing.SwingUtilities;
public class MathLearningApp {
public static void main(String[] args) {

@ -1,4 +1,3 @@
import service.QuestionService;
import model.Question;
import service.OptionsResult;

@ -1,6 +1,9 @@
package controller;
import service.*;
import view.*;
import service.UserService;
import service.EmailService;
import view.LoginFrame;
import view.RegisterFrame;
public class AuthController {
private UserService userService = new UserService();
@ -36,6 +39,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 +54,9 @@ public class AuthController {
public void showRegister() {
new RegisterFrame(this);
}
// 获取当前用户
public String getCurrentUser() {
return currentUser;
}
}

@ -1,4 +1,5 @@
package controller;
import service.QuestionService;
import view.ExamFrame;
import java.util.List;

@ -1,5 +1,9 @@
package controller;
import view.*;
import view.LoginFrame;
import view.MainFrame;
import view.ChangePasswordFrame;
import javax.swing.JOptionPane;
public class MainController {
private AuthController authController = new AuthController(this);
@ -38,6 +42,15 @@ public class MainController {
mainFrame.requestFocus();
}
/**
*
*/
public void showChangePassword() {
if (mainFrame != null) {
new ChangePasswordFrame(mainFrame, authController);
}
}
/**
*
* @param difficulty
@ -80,6 +93,13 @@ public class MainController {
return mainFrame;
}
/**
*
*/
public String getCurrentUser() {
return authController.getCurrentUser();
}
/**
* 退
*/
@ -90,4 +110,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,150 @@
package view;
import controller.AuthController;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Color;
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);
}
}

@ -1,8 +1,16 @@
package view;
import controller.ExamController;
import model.Question;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;
public class ExamFrame extends JFrame {

@ -1,30 +1,93 @@
package view;
import controller.AuthController;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.FlowLayout;
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 +97,15 @@ public class LoginFrame extends JFrame {
JOptionPane.showMessageDialog(this, "登录失败");
}
});
registerBtn.addActionListener(e -> {
dispose();
controller.showRegister();
});
// 添加回车键提交支持
getRootPane().setDefaultButton(loginBtn);
add(panel);
setVisible(true);
}

@ -1,7 +1,18 @@
package view;
import controller.MainController;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Color;
public class MainFrame extends JFrame {
private MainController controller;
@ -15,7 +26,7 @@ public class MainFrame extends JFrame {
private void initializeWindow() {
setTitle("数学学习系统 - 主界面");
setSize(400, 400); // 增大窗口尺寸
setSize(500, 550); // 稍微增加高度以容纳新按钮
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
@ -24,28 +35,36 @@ public class MainFrame extends JFrame {
private void createComponents() {
// 使用 BorderLayout 作为主布局
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
panel.setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));
// 标题面板
JPanel titlePanel = new JPanel(new FlowLayout());
JLabel titleLabel = new JLabel("选择考试难度");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
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, 14));
hintLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
hintPanel.add(hintLabel);
// 按钮面板 - 使用 GridLayout 让按钮垂直排列
JPanel buttonPanel = new JPanel(new GridLayout(4, 1, 15, 15)); // 4行1列间距15
buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50)); // 左右边距
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("退出登录");
// 设置按钮字体和大小
@ -53,43 +72,51 @@ public class MainFrame extends JFrame {
primaryBtn.setFont(buttonFont);
middleBtn.setFont(buttonFont);
highBtn.setFont(buttonFont);
changePasswordBtn.setFont(buttonFont);
logoutBtn.setFont(buttonFont);
// 设置按钮大小
Dimension buttonSize = new Dimension(200, 50);
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,
@ -108,12 +135,18 @@ public class MainFrame extends JFrame {
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(hintPanel, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.add(contentPanel, BorderLayout.CENTER);
add(panel);
}
@ -123,13 +156,10 @@ public class MainFrame extends JFrame {
*/
public void showFrame() {
setVisible(true);
toFront(); // 确保窗口在前台
requestFocus(); // 请求焦点
toFront();
requestFocus();
}
/**
* dispose 退
*/
@Override
public void dispose() {
super.dispose();

@ -1,21 +1,36 @@
package view;
import controller.AuthController;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Color;
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,42 +40,92 @@ 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);
// 发送验证码按钮事件监听器
@ -72,6 +137,12 @@ public class RegisterFrame extends JFrame {
return;
}
// 简单的邮箱格式验证
if (!isValidEmail(email)) {
JOptionPane.showMessageDialog(this, "请输入有效的邮箱地址", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
// 禁用按钮
sendCodeBtn.setEnabled(false);
sendCodeBtn.setText("发送中...");
@ -84,21 +155,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,
"注册失败,请重试",
"错误",
@ -131,6 +219,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;
@ -146,7 +242,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);
}
@ -163,4 +259,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,}$");
}
}

@ -1,7 +1,13 @@
package view;
import controller.ExamController;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Font;
public class ResultFrame extends JFrame {
public ResultFrame(ExamController controller, int score, int total) {

Loading…
Cancel
Save