public void paint(Graphics g){ int width = 160; int height = 40; g.setColor(Color.LIGHT_GRAY); g.fillRect(0,0,width,height); g.setColor(Color.BLACK); g.drawRect(0,0,width-1,height-1); Random r = new Random(); for (int i =0 ;i<100;i++){ int x = r.nextInt(width)-2; int y = r.nextInt(height)-2; g.drawOval(x,y,2,2); } g.setFont(new Font("黑体",Font.BOLD,30)); g.setColor(Color.BLUE); char[] chars = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); StringBuilder sb= new StringBuilder(); for(int i =0; i < 4; i ++){ int pos =r.nextInt(chars.length); char c = chars[pos]; sb.append(c+" "); } g.drawString(sb.toString(),20,30); }
Swing绘制一个4位的验证码
最新推荐文章于 2023-08-07 18:06:53 发布