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.
student_studying/src/ui/RegisterPanel.java

111 lines
3.5 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 ui;
import controller.AppController;
import util.EmailUtil;
import javax.swing.*;
import java.awt.*;
/**
* 注册界面
*/
public class RegisterPanel extends JPanel {
private JTextField emailField;
private JTextField codeField;
private JPasswordField passwordField;
private JPasswordField confirmField;
private String sentCode = null;
public RegisterPanel(AppController controller) {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 8, 8, 8);
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel title = new JLabel("用户注册", JLabel.CENTER);
title.setFont(new Font("微软雅黑", Font.BOLD, 22));
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
add(title, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0; gbc.gridy = 1;
add(new JLabel("邮箱:"), gbc);
gbc.gridx = 1;
emailField = new JTextField(20);
add(emailField, gbc);
gbc.gridx = 0; gbc.gridy = 2;
add(new JLabel("验证码:"), gbc);
gbc.gridx = 1;
codeField = new JTextField(10);
add(codeField, gbc);
gbc.gridx = 0; gbc.gridy = 3;
add(new JLabel("密码:"), gbc);
gbc.gridx = 1;
passwordField = new JPasswordField(20);
add(passwordField, gbc);
gbc.gridx = 0; gbc.gridy = 4;
add(new JLabel("确认密码:"), gbc);
gbc.gridx = 1;
confirmField = new JPasswordField(20);
add(confirmField, gbc);
JButton sendCodeButton = new JButton("发送验证码");
gbc.gridx = 0; gbc.gridy = 5;
add(sendCodeButton, gbc);
JButton registerButton = new JButton("注册");
gbc.gridx = 1;
add(registerButton, gbc);
JButton backButton = new JButton("返回登录");
gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 2;
add(backButton, gbc);
sendCodeButton.addActionListener(e -> {
String email = emailField.getText().trim();
if (!email.matches("^[A-Za-z0-9+_.-]+@(.+)$")) {
JOptionPane.showMessageDialog(this, "邮箱格式不正确!");
return;
}
sentCode = EmailUtil.sendVerificationCode(email);
JOptionPane.showMessageDialog(this, "验证码已发送(调试模式控制台查看)!");
});
registerButton.addActionListener(e -> {
String email = emailField.getText().trim();
String code = codeField.getText().trim();
String password = new String(passwordField.getPassword());
String confirm = new String(confirmField.getPassword());
if (sentCode == null) {
JOptionPane.showMessageDialog(this, "请先获取验证码!");
return;
}
if (!code.equals(sentCode)) {
JOptionPane.showMessageDialog(this, "验证码错误!");
return;
}
if (!password.equals(confirm)) {
JOptionPane.showMessageDialog(this, "两次密码不一致!");
return;
}
if (!password.matches("(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[A-Za-z\\d]{6,10}")) {
JOptionPane.showMessageDialog(this, "密码需包含大小写字母与数字长度6-10位");
return;
}
controller.handleRegister(email, password);
});
backButton.addActionListener(e -> controller.showLoginPanel());
}
}