Java swing 实现QQ登录注册页面

发布时间 2023-09-17 23:10:56作者: 临江柔
  代码如下
1
package com.lty; 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.util.Random; 7 8 public class QQLoginPage { 9 private JFrame frame; 10 private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 11 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' }; 12 private JTextField accountField; 13 Font f1=new Font("宋体", Font.PLAIN, 14); //设置字体 14 private JPasswordField passwordField; 15 private JLabel captchaLabel; 16 private JTextField captchaField; 17 18 public QQLoginPage() { 19 JFrame.setDefaultLookAndFeelDecorated(true); 20 frame = new JFrame("QQ登录"); 21 frame.setSize(350, 200); 22 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23 frame.setLayout(new FlowLayout()); 24 frame.setLocation(700,500); 25 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 26 JLabel jp_jlb3=new JLabel("注册账号"); 27 jp_jlb3.setFont(f1); 28 jp_jlb3.setForeground(Color.BLUE); 29 jp_jlb3.setBounds(300, 10, 70,25); 30 JLabel jp_jlb4=new JLabel("找回密码"); 31 jp_jlb4.setFont(f1);//设置"找回密码"字体 32 jp_jlb4.setForeground(Color.BLUE); //设置"找回密码"字体颜色 33 jp_jlb4.setBounds(300, 43, 70,25); 34 JLabel accountLabel = new JLabel("账户:"); 35 accountField = new JTextField("账号/手机号",20); 36 accountField.setForeground(Color.LIGHT_GRAY); 37 accountField.setBounds(118, 11, 170,25); 38 JLabel passwordLabel = new JLabel("密码:"); 39 passwordField = new JPasswordField(20); 40 passwordField.setBounds(118, 11, 170,25); 41 frame.add(accountLabel); 42 frame.add(accountField); 43 frame.add(jp_jlb3); 44 frame.add(passwordLabel); 45 frame.add(passwordField); 46 frame.add(jp_jlb4); 47 captchaLabel = new JLabel(); 48 49 50 JLabel captchaInputLabel = new JLabel("验证码:"); 51 captchaField = new JTextField(10); 52 captchaField.setBounds(240,187,175,30); 53 frame.add(captchaInputLabel); 54 frame.add(captchaField); 55 56 JButton refreshButton = new JButton("刷新"); 57 refreshCaptcha(); 58 refreshButton.addActionListener(new ActionListener() { 59 @Override 60 public void actionPerformed(ActionEvent e) { 61 refreshCaptcha(); 62 } 63 }); 64 65 JButton loginButton = new JButton("登录"); 66 loginButton.addActionListener(new ActionListener() { 67 @Override 68 public void actionPerformed(ActionEvent e) { 69 String account = accountField.getText(); 70 String password = new String(passwordField.getPassword()); 71 String captchaInput = captchaField.getText(); 72 73 if (validateCaptcha(captchaInput)) { 74 // 在这里执行登录逻辑 75 System.out.println("账户:" + account); 76 System.out.println("密码:" + password); 77 System.out.println("验证码:" + captchaInput); 78 JOptionPane.showMessageDialog(frame, "登录成功"); 79 } else { 80 JOptionPane.showMessageDialog(frame, "验证码错误", "错误", JOptionPane.ERROR_MESSAGE); 81 refreshCaptcha(); 82 } 83 } 84 }); 85 JCheckBox jp_jcb1=new JCheckBox("记住密码"); 86 jp_jcb1.setFont(f1); 87 jp_jcb1.setBounds(114, 78, 90,20); 88 JCheckBox jp_jcb2=new JCheckBox("自动登录"); 89 jp_jcb2.setFont(f1); 90 jp_jcb2.setBounds(206, 78, 90,20); 91 JButton jp_jb2=new JButton("注册"); 92 jp_jb2.setFont(f1); 93 jp_jb2.setBounds(95, 120, 65,21); 94 frame.add(captchaLabel); 95 frame.add(refreshButton); 96 frame.add(jp_jcb1); 97 frame.add(jp_jcb2); 98 frame.add(loginButton); 99 frame.add(jp_jb2); 100 frame.setResizable(false); 101 frame.setIconImage(new ImageIcon("image/image.png").getImage()); 102 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 103 // frame.pack(); 104 frame.setVisible(true); 105 } 106 107 private void refreshCaptcha() { 108 Random random = new Random(); 109 String captcha=""; 110 for(int i=0;i<4;i++){ 111 captcha+=codeSequence[random.nextInt(codeSequence.length)]; 112 } 113 captchaLabel.setText(String.valueOf(captcha)); 114 } 115 116 private boolean validateCaptcha(String captchaInput) { 117 String captcha = captchaLabel.getText(); 118 return captcha.equals(captchaInput); 119 } 120 121 public static void main(String[] args) { 122 new QQLoginPage(); 123 } 124 }

运行结果如下

 可以刷新验证码,运用的流式结构。