用户注册与登录界面java源码(带验证码)
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); }
}
本文作者:丰川扬子
本文链接:https://www.cnblogs.com/newzeon/p/17718652.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步