java視窗登入介面實現隨機驗證碼

无名客QF發表於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();
}

}
執行結果展示:

相關文章