import java.awt.Container;java
import java.awt.event.ActionEvent;dom
import java.awt.event.ActionListener;佈局
import java.util.Random;對象
import javax.swing.JButton;get
import javax.swing.JFrame;it
import javax.swing.JLabel;io
import javax.swing.JOptionPane;event
import javax.swing.JPasswordField;驗證碼
import javax.swing.JTextField; //加載包swingclass
@SuppressWarnings("serial")
public class ZC extends JFrame
{
public ZC()
{
String[] a={"a","s","d","f","g","h","j","k","l","q","w","e","r","t","y","u","i","o","p","z","x","c","v","b","n","m","0","1","2","3","4","5","6","7","8","9"};
setTitle("歡迎登錄");//設置窗體標題
setLayout(null);//窗體取消佈局管理器設置
setBounds(0,0,250,350);//定位窗體的位置與大小
@SuppressWarnings("unused")
Container c=getContentPane();//建立容器對象
Random s1=new Random();//產生隨機驗證碼
Random s2=new Random();
Random s3=new Random();
Random s4=new Random();
int i,j,k,l; i=s1.nextInt(35);
j=s2.nextInt(35); k=s3.nextInt(35);
l=s4.nextInt(35);
JLabel yh=new JLabel("用戶名");//建立一個用戶名標籤
JLabel mm=new JLabel("密碼");//建立一個密碼標籤
JLabel yz=new JLabel("驗證碼");//建立一個驗證碼標籤
JTextField yh1=new JTextField(null);//添加一個文本框
JPasswordField mm1=new JPasswordField(null);//添加一個密碼框
JTextField yz1=new JTextField(null);//添加一個文本框
JLabel yz2=new JLabel(a[i]+a[j]+a[k]+a[l]);
JButton b1=new JButton("登錄");
JButton b2=new JButton("取消");
yh.setBounds(10,30,80,30);//用戶名位置和大小
mm.setBounds(10,75,80,30);//密碼標籤位置和大小
yz.setBounds(10,120,80,30);//驗證碼標籤位置和大小
yh1.setBounds(60,30,100,30);//用戶名框位置和大小
mm1.setBounds(60,75,100,30);//密碼框位置和大小
yz1.setBounds(60,120,100,30);//驗證碼框位置和大小
yz2.setBounds(70,170,80,30);
b1.setBounds(20,220,80,30); b2.setBounds(120,220,80,30);
c.add(yh);//講標籤和框添加到容器
c.add(mm);
c.add(yz);
c.add(yh1);
c.add(mm1);
c.add(yz1);
c.add(b2);
c.add(yz2);
c.add(b1);
setVisible(true);//使窗體可見
}
public static void main( String args[] )
{
new ZC();
}
}
利用了java中swing組件的絕對佈局,將每個模塊的位置和大小都調整到最適合。經過產生隨機數的方式產生四位驗證碼。
求大神教導怎樣能夠將輸入的驗證碼進行讀取並比對。