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.
164 lines
6.1 KiB
164 lines
6.1 KiB
package view;
|
|
|
|
import controller.NavigationController;
|
|
import controller.NavigationService;
|
|
import controller.UserController;
|
|
import model.User;
|
|
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JTextField;
|
|
import javax.swing.JPasswordField;
|
|
import javax.swing.JButton;
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.JOptionPane;
|
|
import java.awt.BorderLayout;
|
|
import java.awt.GridLayout;
|
|
import java.awt.FlowLayout;
|
|
import java.awt.Font;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
|
|
public class RegisterView extends JFrame {
|
|
private JTextField usernameField; // 用户名输入框
|
|
private JTextField emailField;
|
|
private JTextField codeField;
|
|
private JPasswordField passwordField;
|
|
private JPasswordField confirmPasswordField;
|
|
private UserController userController;
|
|
private NavigationService navigationService; // 新增
|
|
private String currentUsername; // 保存当前注册的用户名
|
|
|
|
public RegisterView() {
|
|
userController = new UserController();
|
|
navigationService = NavigationController.getInstance(); // 新增
|
|
initializeUI();
|
|
}
|
|
|
|
private void initializeUI() {
|
|
setTitle("数学学习软件 - 注册");
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
setSize(550, 500);
|
|
setLocationRelativeTo(null);
|
|
setResizable(false);
|
|
|
|
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
|
|
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
|
|
|
// Title
|
|
JLabel titleLabel = new JLabel("用户注册", JLabel.CENTER);
|
|
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
|
|
mainPanel.add(titleLabel, BorderLayout.NORTH);
|
|
|
|
// Form panel - 增加用户名字段
|
|
JPanel formPanel = new JPanel(new GridLayout(8, 2, 10, 10));
|
|
|
|
JLabel usernameLabel = new JLabel("用户名:");
|
|
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
usernameField = new JTextField();
|
|
|
|
JLabel emailLabel = new JLabel("邮箱:");
|
|
emailLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
emailField = new JTextField();
|
|
|
|
JLabel codeLabel = new JLabel("注册码:");
|
|
codeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
codeField = new JTextField();
|
|
|
|
JLabel passwordLabel = new JLabel("密码:");
|
|
passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
passwordField = new JPasswordField();
|
|
|
|
JLabel confirmPasswordLabel = new JLabel("确认密码:");
|
|
confirmPasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
confirmPasswordField = new JPasswordField();
|
|
|
|
JButton sendCodeButton = new JButton("发送注册码");
|
|
sendCodeButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
|
|
|
|
JButton registerButton = new JButton("完成注册");
|
|
registerButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
|
|
// 布局表单
|
|
formPanel.add(usernameLabel);
|
|
formPanel.add(usernameField);
|
|
formPanel.add(emailLabel);
|
|
formPanel.add(emailField);
|
|
formPanel.add(sendCodeButton);
|
|
formPanel.add(new JLabel());
|
|
formPanel.add(codeLabel);
|
|
formPanel.add(codeField);
|
|
formPanel.add(passwordLabel);
|
|
formPanel.add(passwordField);
|
|
formPanel.add(confirmPasswordLabel);
|
|
formPanel.add(confirmPasswordField);
|
|
|
|
formPanel.add(registerButton);
|
|
|
|
mainPanel.add(formPanel, BorderLayout.CENTER);
|
|
|
|
// Back button
|
|
JButton backButton = new JButton("返回登录");
|
|
backButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
backButton.addActionListener(e -> navigationService.showLoginView()); // 修改
|
|
|
|
JPanel bottomPanel = new JPanel(new FlowLayout());
|
|
bottomPanel.add(backButton);
|
|
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
|
|
|
|
// Add action listeners
|
|
sendCodeButton.addActionListener(new SendCodeAction());
|
|
registerButton.addActionListener(new RegisterAction());
|
|
|
|
add(mainPanel);
|
|
}
|
|
|
|
private class SendCodeAction implements ActionListener {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
String username = usernameField.getText().trim();
|
|
String email = emailField.getText().trim();
|
|
|
|
if (username.isEmpty() || email.isEmpty()) {
|
|
JOptionPane.showMessageDialog(RegisterView.this,
|
|
"请输入用户名和邮箱", "错误", JOptionPane.ERROR_MESSAGE);
|
|
return;
|
|
}
|
|
|
|
String result = userController.registerUser(username, email);
|
|
JOptionPane.showMessageDialog(RegisterView.this, result);
|
|
|
|
if (result.contains("发送")) {
|
|
currentUsername = username;
|
|
}
|
|
}
|
|
}
|
|
|
|
private class RegisterAction implements ActionListener {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
String username = usernameField.getText().trim();
|
|
String code = codeField.getText().trim();
|
|
String password = new String(passwordField.getPassword());
|
|
String confirmPassword = new String(confirmPasswordField.getPassword());
|
|
|
|
if (username.isEmpty() || code.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
|
|
JOptionPane.showMessageDialog(RegisterView.this,
|
|
"请填写所有字段", "错误", JOptionPane.ERROR_MESSAGE);
|
|
return;
|
|
}
|
|
|
|
String result = userController.completeRegistration(username, code, password, confirmPassword);
|
|
|
|
if (result.equals("注册成功!")) {
|
|
JOptionPane.showMessageDialog(RegisterView.this,
|
|
result, "成功", JOptionPane.INFORMATION_MESSAGE);
|
|
navigationService.showLoginView(); // 修改
|
|
} else {
|
|
JOptionPane.showMessageDialog(RegisterView.this,
|
|
result, "错误", JOptionPane.ERROR_MESSAGE);
|
|
}
|
|
}
|
|
}
|
|
} |