Java Verification Code Program

Source: Internet
Author: User

1. Design Ideas
The output of the verification code is controlled by the random number of randomly generated numbers, using the For loop to control the number of random numbers.
Use JFrame to manage the layout and manage the location of the contents of the Login box.

2. Flowchart

3. Source code

Denglu class

Import javax.swing.*;
Import java.awt.*;
Import Javax.swing.JFrame;
public class Denglu extends jframe{
JTextField JTextField; Defining a text Box component
JPasswordField JPasswordField; Define a Password box component
JTextField jyanzhengfield;//defining Authenticode Framework Components
JLabel Jl1,jl2,jl3; User name password
JButton jb1,jb2; Button
JPanel JP1,JP2,JP3,JP4,JP5; Border
Public Denglu () {
Creating components
Jtextfield=new JTextField (10);
Jpasswordfield=new JPasswordField (10);
Jyanzhengfield=new JTextField (5);

Jb1=new JButton ("login");
Jb2=new JButton ("Fast registration");

Jl1=new JLabel ("login name");
Jl2=new JLabel ("password");
Jl3=new JLabel ("Verification Code");

Jp1=new JPanel ();
Jp2=new JPanel ();
Jp3=new JPanel ();
Jp4=new JPanel ();

Set layout
JFrame JF = new JFrame ();
Jf.setlayout (New GridLayout (5,2));

Jp1.add (JL1);
Jp1.add (JTextField);//First Panel text box and user name

Jp2.add (JL2);
Jp2.add (JPasswordField);//The Second Panel text box and password

Jp3.add (JL3);
Jp3.add (Jyanzhengfield);//Third Panel text box and verification code

Jp4.add (JB1);
Jp4.add (JB2);//Fourth panel login Quick Register

Jf.add (JP1);
Jf.add (JP2);
Jf.add (JP3);
Jf.add (JP4);
Add four panels to the login box

Setting up Frames
Jf.settitle ("login");
Jf.setsize (400, 300);
Jf.pack ();
Dimension screen = Gettoolkit (). Getscreensize ();
Jf.setlocation ((Screen.width-getwidth ())/2, (Screen.height-getheight ())/2);
Jf.setdefaultcloseoperation (Exit_on_close);
Jf.setvisible (TRUE);

}


}

Yanzheng class

Import java.awt.*;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.util.Random;
Import Javax.swing.JFrame;
public class Yanzheng extends JFrame {
private static random random = new random ();
Private String code = "";//Save Verification Code
Public Yanzheng () {//Initialize form information
Super ("Verification Code");
SetVisible (TRUE);
SetBounds ((Toolkit.getdefaulttoolkit (). Getscreensize (). width-300)/2, (Toolkit.getdefaulttoolkit (). getscreensize (). height-300)/2, 300, 200);
Setdefaultcloseoperation (Exit_on_close);
}
Public String Randomcode () {//Get Verification code
String code= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new random ();
StringBuffer s=new StringBuffer ();
int num =random.nextint (62);
Sb.append (Code.charat (num));
return s.tostring ();
}

public void Checkcode (Graphics a) {//Set verification code
Drawcode (a);
}
public void Drawcode (Graphics a) {//Set Verification code content
for (int i=0;i<6;i++) {
A.setfont (New Font ("Arial", font.bold,20));
A.drawstring (Randomcode (), 50+ (i*12), 70);
}
}
public void Paint (Graphics a) {
Checkcode (a);
}

Realize
public static void Main (string[] args) {
TODO auto-generated Method Stub
New Yanzheng ();
New Denglu ();

}
}

4. Procedure

5. Experimental summary

Verification Code:
Random numbers generated by using random can only be of type int and cannot be a character type.
Use the graphics class to transmit the paint drawing random numbers when drawing a captcha picture.
Login Interface Design:
Manage with JFrame layout
Label Component: JLabel button assembly: JButton
Font settings: Font font=new font ("script", font.italic+font.bold,28);
The GridLayout layout manager is managed in tabular format, and you must set the number of rows and columns to display when using this layout manager
The JFrame class is a container that allows programmers to add other components to it, organize them, and present them to the user.
JFrame jf=new JFrame ("");//Instantiate Form Object
Use the Dimension class to set the size of the form: GetSize () and setsize (Dimension size), respectively, to get and set the size of the square.
Problems encountered: Use StringBuffer code= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Cannot generate random character but all output

Change to String code= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new random ();
StringBuffer s=new StringBuffer ();
int num =random.nextint (62);
S.append (Code.charat (num));
return sb.tostring ();
Validation can be successfully output

Java Verification Code Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.