简单随机验证码的生成

Myth丶恋晨 2021-09-30 05:04 509阅读 0赞

一 代码

  1. /**
  2. *
  3. * @ClassName: TestVerificationCode
  4. *
  5. * @Description: 随机验证码的实现
  6. *
  7. */
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.util.Random;
  11. import javax.swing.*;
  12. public class TestVerificationCode
  13. {
  14. public static void main(String[] args)
  15. {
  16. JFrame frame = new JFrame("登录测试");
  17. JButton b1 = new JButton("登陆");
  18. JButton b2 = new JButton("注册");
  19. JPanel center = new JPanel();
  20. center.setLayout(new GridLayout(0, 1, 5, 5));
  21. JPanel center1 = new JPanel();
  22. JPanel center2 = new JPanel();
  23. JPanel center3 = new JPanel();
  24. JPanel west = new JPanel();
  25. JTextField t1 = new JTextField(10);
  26. JPasswordField t2 = new JPasswordField(10);
  27. JTextField t3 = new JTextField(5);
  28. center1.setLayout(new GridLayout(1, 0)); // 仅限1行,不限列
  29. center2.setLayout(new GridLayout(1, 0));
  30. center3.setLayout(new GridLayout(1, 0, 5, 5));
  31. center1.add(t1);
  32. center2.add(t2);
  33. center3.add(t3);
  34. ValidCode valid = new ValidCode();
  35. center3.add(valid);
  36. center.add(center1);
  37. center.add(center2);
  38. center.add(center3);
  39. west.setLayout(new GridLayout(0, 1));
  40. west.add(new JLabel("账 号:"));
  41. west.add(new JLabel("密 码:"));
  42. west.add(new JLabel("验证码:"));
  43. JPanel south = new JPanel();
  44. b1.addActionListener(new ActionListener() {
  45. public void actionPerformed(ActionEvent e)
  46. {
  47. if (t3.getText().equals(valid.getCode()))
  48. JOptionPane.showMessageDialog(null, "验证成功");
  49. else {
  50. JOptionPane.showMessageDialog(null, "验证失败");
  51. valid.nextCode();
  52. }
  53. }
  54. });
  55. south.add(b1);
  56. south.add(b2);
  57. frame.setSize(275, 240);
  58. frame.add(west, BorderLayout.WEST);
  59. JLabel mes = new JLabel(" 欢迎登陆邮箱");
  60. mes.setFont(new Font("黑体", Font.BOLD, 25));
  61. frame.add(mes, BorderLayout.NORTH);
  62. frame.add(center, BorderLayout.CENTER);
  63. frame.add(south, BorderLayout.SOUTH);
  64. frame.setVisible(true);
  65. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66. }
  67. }
  68. class ValidCode extends JPanel
  69. {
  70. String code;
  71. private Random random = new Random();
  72. public ValidCode()
  73. {
  74. setSize(100, 40);
  75. this.addMouseListener(new MouseAdapter() {
  76. public void mouseClicked(MouseEvent e)
  77. {
  78. setSize(100, 40);
  79. nextCode();
  80. }
  81. });
  82. }
  83. public String getCode()
  84. {
  85. return code;
  86. }
  87. void generateCode()
  88. {
  89. char[] codes = new char[5];
  90. for (int i = 0; i < 5; i++)
  91. {
  92. if (random.nextBoolean()){
  93. codes[i] = (char) (random.nextInt(26) + 65);
  94. } else {
  95. codes[i] = (char) (random.nextInt(26) + 97);
  96. }
  97. }
  98. this.code = new String(codes);
  99. }
  100. public void paint(Graphics g)
  101. {
  102. if (this.code == null)
  103. {
  104. generateCode();
  105. }
  106. Font myFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
  107. g.setFont(myFont);
  108. g.setColor(Color.WHITE);
  109. g.fillRect(0, 0, 100, 40);
  110. g.setColor(Color.BLACK);
  111. g.drawRect(0, 0, 99, 39);
  112. g.setColor(Color.LIGHT_GRAY);
  113. for (int i = 0; i < 100; i++)
  114. {
  115. int x = random.nextInt(100 - 1);
  116. int y = random.nextInt(40 - 1);
  117. int x1 = random.nextInt(100 - 10) + 10;
  118. int y1 = random.nextInt(40 - 4) + 4;
  119. g.drawLine(x, y, x1, y1);
  120. }
  121. g.setColor(Color.RED);
  122. for (int i = 0; i < 5; i++)
  123. {
  124. g.drawString(code.charAt(i) + "", 16 * i + 10, 25);
  125. }
  126. }
  127. public void nextCode()
  128. {
  129. generateCode();
  130. repaint();
  131. }
  132. }

二 运行

20190724214525399.png

发表评论

表情:
评论列表 (有 0 条评论,509人围观)

还没有评论,来说两句吧...

相关阅读