Java自动生成验证码带图形界面

本代码只是基本完成要求,不太完善。
/* 自动生成验证码Java带图形界面,基本要求完成,还不太完善,请大神完善。 作者:hzl(何泽雷)/带土。 */ import java.util.Random; import java.util.Scanner; import javax.swing.JOptionPane; import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.event.ActionEvent; //监听键盘与鼠标。 import java.awt.event.ActionListener; public class zdyz implements ActionListener{ static Random s = new Random(); static Scanner sc; static String b, c; public static void switch1() { for (int i = 0; i < 4; i++) { int x = s.nextInt(1, 4); switch (x) { case 1: { int y = s.nextInt(26); char a = (char) (y + 65); if (b == null) b = String.valueOf(a); else b += a; break; } case 2: { int y = s.nextInt(26); char a = (char) (y + 97); if (b == null) b = String.valueOf(a); else b += a; break; } case 3: { int y = s.nextInt(10); if (b == null) b = String.valueOf(y); else b += y; break; } } } } private JLabel yanz; private JTextField tyan; private JButton jb; public void settu() {switch1(); JFrame h = new JFrame("自动生成验证码"); h.setSize(400, 300); h.setLocation(200, 200); h.setLayout(null); yanz = new JLabel("验证码:"); tyan = new JTextField(""); yanz.setBounds(20, 110, 300, 30); tyan.setBounds(60, 110, 120, 30); jb = new JButton("确定"); jb.setBounds(40, 150, 60, 30); jb.addActionListener(this); JButton yz = new JButton(b); yz.setBounds(200, 110, 100, 30); yz.setBackground(Color.gray); yz.addActionListener(this); h.add(yanz); h.add(tyan); h.add(jb); h.add(yz); h.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); h.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==jb) { if(b.equals(tyan.getText())) {JOptionPane.showMessageDialog(null, "登录成功");System.exit(0); } else { JOptionPane.showMessageDialog(null, "登录失败,请重新输入");b=null; settu(); } } } public static void main(String[] args) { sc = new Scanner(System.in); zdyz t=new zdyz(); System.out.println("请按下图输入验证码"); t.settu(); } }

  

posted @ 2022-09-15 16:18  阿飞藏泪  阅读(65)  评论(0编辑  收藏  举报
1 2 3
4