You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jiedui/src/view/RegisterFrame.java

166 lines
6.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package view;
import controller.AuthController;
import javax.swing.*;
import java.awt.*;
public class RegisterFrame extends JFrame {
public RegisterFrame(AuthController controller) {
setTitle("注册");
setSize(400, 350);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 使用 GridBagLayout 更灵活布局
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(5, 5, 5, 5);
JTextField emailField = new JTextField();
JTextField codeField = new JTextField();
JPasswordField passwordField = new JPasswordField();
JPasswordField confirmField = new JPasswordField();
JButton sendCodeBtn = new JButton("发送验证码");
JButton registerBtn = new JButton("注册");
JButton backBtn = new JButton("返回");
// 邮箱行
gbc.gridx = 0; gbc.gridy = 0;
panel.add(new JLabel("邮箱:"), gbc);
gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 2;
gbc.weightx = 1.0;
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;
panel.add(codeField, gbc);
gbc.gridx = 2; gbc.gridy = 1; gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
panel.add(sendCodeBtn, gbc);
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;
panel.add(passwordField, 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;
panel.add(confirmField, gbc);
// 按钮行 - 添加返回按钮
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(registerBtn);
buttonPanel.add(backBtn);
gbc.gridx = 0; gbc.gridy = 4; 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;
}
// 禁用按钮
sendCodeBtn.setEnabled(false);
sendCodeBtn.setText("发送中...");
new Thread(() -> {
try {
String result = controller.register(email);
SwingUtilities.invokeLater(() -> {
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);
} else {
JOptionPane.showMessageDialog(this,
"注册失败,请重试",
"错误",
JOptionPane.ERROR_MESSAGE);
}
});
} catch (Exception ex) {
SwingUtilities.invokeLater(() -> {
sendCodeBtn.setEnabled(true);
sendCodeBtn.setText("发送验证码");
JOptionPane.showMessageDialog(this,
"发送验证码时出现错误",
"错误",
JOptionPane.ERROR_MESSAGE);
});
}
}).start();
});
registerBtn.addActionListener(e -> {
String email = emailField.getText().trim();
String code = codeField.getText().trim();
String password = new String(passwordField.getPassword());
String confirmPassword = new String(confirmField.getPassword());
// 输入验证
if (email.isEmpty() || code.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(this, "请填写所有字段", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
boolean success = controller.completeRegistration(email, code, password, confirmPassword);
if (success) {
JOptionPane.showMessageDialog(this,
"注册成功!\n请使用注册的邮箱和密码登录",
"注册成功",
JOptionPane.INFORMATION_MESSAGE);
dispose();
controller.showLogin();
} else {
JOptionPane.showMessageDialog(this,
"注册失败!\n可能的原因\n- 验证码错误\n- 密码不符合要求6-10位包含大小写字母和数字\n- 邮箱已被注册",
"注册失败",
JOptionPane.ERROR_MESSAGE);
}
});
backBtn.addActionListener(e -> {
dispose();
controller.showLogin();
});
// 添加回车键提交支持
getRootPane().setDefaultButton(registerBtn);
add(panel);
setVisible(true);
}
}