import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField; //加载包swing
@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组件的绝对布局,将每一个模块的位置和大小都调整到最适合。通过产生随机数的方式产生四位验证码。
求大神教导怎样可以将输入的验证码进行读取并比对。