JAVA Swing登录界面验证码功能

  有时我们在用JAVA Swing设计登录界面时,想要增添验证码功能。接下来就分享一下我的学习心得。

  首先放上效果图:

  

 

   

接下来是验证码实现部分:

  1 public class ValidCode extends JComponent implements MouseListener {
  2 
  3     private String code;
  4 
  5     private int width, height = 40;
  6 
  7     private int codeLength = 4;
  8 
  9     private Random random = new Random();
 10 
 11     public ValidCode() {
 12         width = this.codeLength * 16 + (this.codeLength - 1) * 10;
 13         setPreferredSize(new Dimension(width, height));
 14         setSize(width, height);
 15         this.addMouseListener(this);
 16         setToolTipText("点击可以更换验证码");
 17     }
 18 
 19     public int getCodeLength() {
 20         return codeLength;
 21     }
 22 
 23     /*
 24     设置验证码文字的长度
 25     */
 26     public void setCodeLength(int codeLength) {
 27         if(codeLength < 4) {
 28             this.codeLength = 4;
 29         } else {
 30             this.codeLength = codeLength;
 31         }
 32 
 33     }
 34 
 35     public String getCode() {
 36         return code;
 37     }
 38 
 39     /*
 40         产生随机的颜色
 41     */
 42     public Color getRandColor(int min, int max) {
 43 
 44         if (min > 255)
 45             min = 255;
 46         if (max > 255)
 47             max = 255;
 48         int red = random.nextInt(max - min) + min;
 49         int green = random.nextInt(max - min) + min;
 50         int blue = random.nextInt(max - min) + min;
 51         return new Color(red, green, blue);
 52     }
 53     /*
 54         设置验证码具体的字母是什么
 55     */
 56     protected String generateCode() {
 57         char[] codes = new char[this.codeLength];
 58         for (int i = 0, len = codes.length; i < len; i++) {
 59             if (random.nextBoolean()) {
 60                 codes[i] = (char) (random.nextInt(26) + 65);
 61             } else {
 62                 codes[i] = (char) (random.nextInt(26) + 97);
 63             }
 64         }
 65         this.code = new String(codes);
 66         return this.code;
 67     }
 68 
 69     @Override
 70     protected void paintComponent(Graphics g) {
 71         super.paintComponent(g);
 72         if(this.code == null || this.code.length() != this.codeLength) {
 73             this.code = generateCode();
 74         }
 75         width = this.codeLength * 16 + (this.codeLength - 1) * 10;
 76         super.setSize(width, height);
 77         super.setPreferredSize(new Dimension(width, height));
 78         Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
 79         g.setFont(mFont);
 80         //绘制出验证码的背景的矩形轮廓
 81         Graphics2D g2d = (Graphics2D) g;
 82         g2d.setColor(getRandColor(200, 250));
 83         g2d.fillRect(0, 0, width, height);
 84         g2d.setColor(getRandColor(180, 200));
 85         g2d.drawRect(0, 0, width - 1, height - 1);
 86         //绘制出验证码背景的线
 87         int i = 0, len = 150;
 88         for (; i < len; i++) {
 89             int x = random.nextInt(width - 1);
 90             int y = random.nextInt(height - 1);
 91             int x1 = random.nextInt(width - 10) + 10;
 92             int y1 = random.nextInt(height - 4) + 4;
 93             g2d.setColor(getRandColor(180, 200));
 94             g2d.drawLine(x, y, x1, y1);
 95         }
 96 
 97 
 98 
 99         //绘制出验证码的具体字母
100         i = 0; len = this.codeLength;
101         FontMetrics fm = g2d.getFontMetrics();
102         int base = (height - fm.getHeight())/2 + fm.getAscent();
103         for(;i<len;i++) {
104             int b = random.nextBoolean() ? 1 : -1;
105             g2d.rotate(random.nextInt(10)*0.01*b);
106             g2d.setColor(getRandColor(20, 130));
107             g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);
108         }
109     }
110 
111     //下一个验证码
112     public void nextCode() {
113         generateCode();
114         repaint();
115     }
116 
117     @Override
118     public void mouseClicked(MouseEvent e) {
119 
120         nextCode();
121     }
122 
123     @Override
124     public void mousePressed(MouseEvent e) {
125         // TODO Auto-generated method stub
126 
127     }
128 
129     @Override
130     public void mouseReleased(MouseEvent e) {
131         // TODO Auto-generated method stub
132 
133     }
134 
135     @Override
136     public void mouseEntered(MouseEvent e) {
137         // TODO Auto-generated method stub
138 
139     }
140 
141     @Override
142     public void mouseExited(MouseEvent e) {
143         // TODO Auto-generated method stub
144 
145     }
146 }

判断验证码是否正确部分:

public boolean isValidCodeRight() {
   if(jt_code == null) {
      return false;
   }else if(vcode == null) {
      return true;
   }else if(vcode.getCode() .equals(jt_code.getText())) {
      return true;
   }else
      return false;
}

 

posted @ 2022-06-26 12:39  阿纳先森  阅读(647)  评论(0)    收藏  举报