隨機生成驗證碼


自動生成30道小學四則運算驗證碼

1.     設計思想

利用JFrame從而實現,編寫兩個類,第二個用來實現驗證碼的相關操作

2.     源程序代碼

//班級:信1705-2

//姓名:馬旺旺

//學號:20173564

import java.awt.*;

 

import javax.swing.*;

 

public class Yanzheng extends JFrame {

     

      private JLabel userLa;

    private JLabel pwdLa;

    private JLabel verCodeLa;//驗證碼

    private JTextField userTxt;

    private JPasswordField pwdTxt;

    private JTextField verCodeTxt;//驗證碼

    private JButton sureBt;

    private JButton quitBt;

    private Yangzhengma mp;

 

    //構造方法

    public Yanzheng()

    {

        Init();

    }

    public void Init()

    {

        this.setTitle("請登入");

 

        //創建出控件對象(因為上面只是聲明出來,並沒有給出實際的空間)

 

        //用戶文本

        userLa = new JLabel();

        userLa.setText("用戶名:");

        userLa.setSize(60, 50);

        userLa.setLocation(100, 80);

 

        //密碼文本

        pwdLa = new JLabel();

        pwdLa.setText("密碼:");

        pwdLa.setSize(50, 50);

        pwdLa.setLocation(100, 120);

 

        //用戶輸入框

        userTxt = new JTextField();

        userTxt.setSize(100, 30);

        //this.setSize(width, height)

        userTxt.setLocation(170, 95);

 

        //密碼輸入框

        pwdTxt = new JPasswordField();

        pwdTxt.setSize(100, 30);

        pwdTxt.setLocation(170, 135);

 

        //登陸

        sureBt = new JButton("登錄");

        sureBt.setSize(100, 25);

        sureBt.setLocation(135, 220);

 

        //快速注冊

        quitBt = new JButton("快速注冊");

        quitBt.setSize(100, 25);

        quitBt.setLocation(135,260);

 

        //驗證碼文本

        verCodeLa = new JLabel();

        verCodeLa.setText("驗證碼:");

        verCodeLa.setSize(60, 50);

        verCodeLa.setLocation(100,165);

 

        //驗證碼文本框

        verCodeTxt = new JTextField();

        verCodeTxt.setSize(100, 30);

        verCodeTxt.setLocation(170, 180);

 

        //驗證碼

        mp = new Yangzhengma();

        mp.setSize(100, 30);

        mp.setLocation(280, 175);

 

        this.setLayout(null);

        this.setSize(500, 400);

        this.add(userLa);

        this.add(pwdLa);

        this.add(userTxt);

        this.add(pwdTxt);

        this.add(sureBt);

        this.add(quitBt);

        this.add(verCodeLa);

        this.add(verCodeTxt);

        this.add(mp);

        this.setVisible(true);

    }

 

public static void main(String[] args) {

      new Yanzheng();

}

 

}

package 練習一;

 

import java.awt.*;

import java.util.*;

public class Yangzhengma extends Panel {

    public void paint(Graphics g)

    {

        int height = 50;

        int width = 90;

        //驗證碼框背景顏色

        g.setColor(Color.BLUE);

        //填充驗證碼背景

        g.fillRect(0, 0, width, height);

        g.setColor(Color.BLACK);

        g.drawRect(0, 0, width-1, height-1);

        Random r = new Random();

        //設置干擾點

        for(int i = 0;i<100;i++)

        {

            int x = r.nextInt(width)-1;

            int y = r.nextInt(height)-1;

            g.drawOval(x, y, 2, 2);

        }

        g.setFont(new Font("黑體",Font.BOLD,20));//設置驗證碼字體以及大小

        g.setColor(Color.red);//設置驗證碼字體顏色

        //生成隨機驗證碼

        char[] tmp = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();

        StringBuilder sb = new StringBuilder();

        for(int i = 0;i<4;i++)

        {

            int pos = r.nextInt(tmp.length);

            char c = tmp[pos];

            sb.append(c + " ");

        }

        g.drawString(sb.toString(), 10, 15);//寫入驗證碼

    }

}

3.     實現結果截圖

   

4.     實驗總結

     本次實驗為做一個文本框軟件,驗證碼的實質即為隨機數的綜合應用,添加了英文字母的應用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM