GUI界面java验证码代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package com.modle;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import bel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JOptionPane;
public class YanModel extends Frame{
Button bt = new Button("核对");
TextField tf = new TextField();
Label lb = new Label();
Label lbRefresh = new Label("看不清");
public void lunchFrame(){
this.setBounds(0, 0, 250, 200);
this.setLayout(null);
this.add(tf);
tf.setBounds(20, 50, 100, 50);
this.add(lb);
lb.setBounds(130, 50, 50, 50);
lb.setText(makeYanZhengMa());
lb.setBackground(new Color(0).YELLOW);
this.add(lbRefresh);
lbRefresh.setFont(new Font("楷体
",Font.HANGING_BASELINE,12));
lbRefresh.setForeground(new Color(0).BLUE);
lbRefresh.setBounds(190, 50, 50, 50);
lbRefresh.addMouseListener(new MouseAdapter(){ //刷新验证码
@Override
public void mouseClicked(MouseEvent e) {
lb.setText(makeYanZhengMa());
}
});
this.add(bt);
bt.setBounds(100, 120, 30, 30);
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(tf.getText().equals(lb.getText())){
JOptionPane.showMessageDialog(null, "验证正确");
}else{
JOptionPane.showMessageDialog(null, "验证失败");
}
}});
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
this.setVisible(true);
}
//产生随机验证码
public String makeYanZhengMa(){
String[] str = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O ","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n"," o","p","q","r","s","t","u","v","w","x","y","z",
"0","1","2","3","4","5","6","7","8","9"};
Random random = new Random();
int i = random.nextInt(62);
int j = random.nextInt(62);
int k = random.nextInt(62);
int z = random.nextInt(62);
int x = random.nextInt(62);
int y = random.nextInt(62);
return str[i]+str[j]+str[k]+str[z]+str[x]+str[y];
}
public static void main(String[] args) {
new YanModel().lunchFrame();
}
}