import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class RegistrationSystem extends JFrame {
private final JTextField usernameTextField;
private final JPasswordField passwordField;
private final JTextField captchaTextField;
private final JLabel captchaLabel;
private JLabel feedbackLabel;
private String captcha;
}
public RegistrationSystem() {
setTitle("Registration System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new GridLayout(6, 2));
}
// 注册页面
JLabel usernameLabel = new JLabel("Username:");
usernameTextField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
JLabel captchaInputLabel = new JLabel("Captcha:");
captchaTextField = new JTextField();
captchaLabel = new JLabel();
JButton refreshCaptchaButton = new JButton("Refresh Captcha");
JButton actionButton = new JButton("Register");
// 验证码生成与刷新
generateCaptcha();
refreshCaptchaButton.addActionListener(e -> generateCaptcha());
// 按钮点击事件
actionButton.addActionListener(e -> {
String username = usernameTextField.getText();
String password = new String(passwordField.getPassword());
String inputCaptcha = captchaTextField.getText();
// 验证码验证
if (!inputCaptcha.equals(captcha)) {
feedbackLabel.setText("Invalid captcha. Please try again.");
generateCaptcha();
return;
}
// 用户名和密码验证
if (!isValidUsername(username) || !isValidPassword(password)) {
feedbackLabel.setText("Invalid username or password. Please try again.");
generateCaptcha();
return;
}
// 根据按钮的文本判断是注册还是登录操作
if (actionButton.getText().equals("Register")) {
// 模拟注册成功,跳转到登录界面
JOptionPane.showMessageDialog(null, "Registration successful!");
showLoginPage();
} else if (actionButton.getText().equals("Login")) {
// 模拟登录成功,显示欢迎界面
JOptionPane.showMessageDialog(null, "Login successful!");
showWelcomePage(username);
}
});
// 添加组件到注册页面
add(usernameLabel);
add(usernameTextField);
add(passwordLabel);
add(passwordField);
add(captchaInputLabel);
add(captchaTextField);
add(captchaLabel);
add(refreshCaptchaButton);
add(actionButton);
// 反馈信息标签
feedbackLabel = new JLabel();
add(feedbackLabel);
// 默认显示注册页面
showRegisterPage();
}
// 显示注册页面
private void showRegisterPage() {
setTitle("Registration Page");
setVisible(true);
feedbackLabel.setText("");
usernameTextField.setText("");
passwordField.setText("");
captchaTextField.setText("");
captchaLabel.setText("");
JButton actionButton = (JButton) getContentPane().getComponent(8);
actionButton.setText("Register");
}
// 显示登录页面
private void showLoginPage() {
setTitle("Login Page");
setVisible(true);
feedbackLabel.setText("");
usernameTextField.setText("");
passwordField.setText("");
captchaTextField.setText("");
captchaLabel.setText("");
JButton actionButton = (JButton) getContentPane().getComponent(8);
actionButton.setText("Login");
}
// 显示欢迎界面
private void showWelcomePage(String username) {
setTitle("Welcome");
setSize(300, 200);
getContentPane().removeAll();
getContentPane().setLayout(new FlowLayout());
JLabel welcomeLabel = new JLabel("Hello " + username + "! Current time is: " + getCurrentTime());
getContentPane().add(welcomeLabel);
revalidate();
repaint();
}
// 生成验证码
private void generateCaptcha() {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 8; i++) {
int index = random.nextInt(characters.length());
sb.append(characters.charAt(index));
}
captcha = sb.toString();
captchaLabel.setText(captcha);
}
// 验证用户名格式是否有效
private boolean isValidUsername(String username) {
return username.matches("[a-zA-Z0-9]{8,20}");
}
// 验证密码格式是否有效
private boolean isValidPassword(String password) {
return password.matches("[a-zA-Z0-9]{8,16}");
}
// 获取当前时间
private String getCurrentTime() {
return new java.util.Date().toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(RegistrationSystem::new);
}
}
标签:java,String,用户注册,setText,private,add,源码,new,JLabel
From: https://www.cnblogs.com/newzeon/p/17718652.html