java窗口登录界面实现随机验证码

WMKQF / 2024-09-27 / 原文

创建窗口内容及验证码更换
代码示例:
package frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Jframe extends JFrame{
public Jframe(){
initJframe();
initView();
this.setVisible(true);
}
public void initJframe(){
this.setSize(488, 430);
this.setTitle("登录");
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setLayout(null);
}
public void initView() {
JLabel usenameText=new JLabel("登录名");
usenameText.setBounds(116,135,47,17);
this.getContentPane().add(usenameText);

	JTextField username=new JTextField();
	username.setBounds(195,134,200,30);
	this.getContentPane().add(username);
	
	JLabel passwordText=new JLabel("密码");
	passwordText.setBounds(130, 195, 32, 16);
	this.getContentPane().add(passwordText);
	
	JTextField password=new JTextField();
	password.setBounds(195, 195, 200, 30);
	this.getContentPane().add(password);
	
	JLabel codeText=new JLabel("验证码");
	codeText.setBounds(133, 256, 50, 30);
	this.getContentPane().add(codeText);
	
	JTextField code=new JTextField();
	code.setBounds(195, 256, 100, 30);
	this.getContentPane().add(code);
	
	String codeStr=CodeUtil.getCode();
	JLabel rightCode=new JLabel();
	rightCode.setText(codeStr);
	rightCode.setBounds(300, 256, 50, 30);
	this.getContentPane().add(rightCode);
	
	JButton login=new JButton("登录");
	login.setBounds(123, 310, 100, 47);
	this.getContentPane().add(login);
	
	JButton register=new JButton("快速注册");
	register.setBounds(256, 310, 100, 47);
	this.getContentPane().add(register);
	
	JButton change=new JButton("更换");
	change.setBounds(350, 256, 60, 30);
	this.getContentPane().add(change);
	change.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			String codeStr1=CodeUtil.getCode();
			rightCode.setText(codeStr1);
		}
	});
		
}

}
生成验证码
代码示例:
package frame;
import java.util.ArrayList;
import java.util.Random;

public class CodeUtil {
public static String getCode(){
//1.创建一个集合
ArrayList list = new ArrayList<>();//52 索引的范围:0 ~ 51
//2.添加字母 a - z A - Z
for (int i = 0; i < 26; i++) {
list.add((char)('a' + i));//a - z
list.add((char)('A' + i));//A - Z
}
//3.打印集合
//4.生成4个随机字母
String result = "";
Random r = new Random();
for (int i = 0; i < 4; i++) {
//获取随机索引
int randomIndex = r.nextInt(list.size());
char c = list.get(randomIndex);
result = result + c;
}

    //5.在后面拼接数字 0~9
    int number = r.nextInt(10);
    //6.把随机数字拼接到result的后面
    result = result + number;
    //7.把字符串变成字符数组
    char[] chars = result.toCharArray();//[A,B,C,D,5]
    //8.在字符数组中生成一个随机索引
    int index = r.nextInt(chars.length);
    //9.拿着4索引上的数字,跟随机索引上的数字进行交换
    char temp = chars[4];
    chars[4] = chars[index];
    chars[index] = temp;
    //10.把字符数组再变回字符串
    String code = new String(chars);
    return code;
}

}
主函数
代码示例:
package frame;
import frame.Jframe;
public class App {
public static void main(String[] args) {
new Jframe();
}

}
运行结果展示: