java 验证码登陆界面_以java实现的一个简单登录界面(带验证码)

importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.FontMetrics;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.util.Random;importjavax.swing.JComponent;public class Yanzhencode extends JComponent implementsMouseListener {private String codes; //自动生成的验证码

private int width, height = 40; //设置验证码高度、宽度

private int codesLength = 4; //设置代码长度

private Random random = new Random(); //生成数字的方法

publicYanzhencode() {

width= this.codesLength * 16 + (this.codesLength - 1) * 10; //根据验证码长度设置宽度

setPreferredSize(new Dimension(width, height)); //设置背景大小

setSize(width, height); //设置验证码长度和宽度

this.addMouseListener(this);

setToolTipText("点击可更换验证码");

}//得到生成的验证码

public intgetCodesLength() {returncodesLength;

}//设置验证码的长度

public void setCodesLength(intcodeLength) {if(codesLength < 4) {this.codesLength = 4;

}else{this.codesLength =codeLength;

}

}publicString getCode() {returncodes;

}//让验证码产生随机的颜色

public Color getRandColor(int min, intmax) {if (min > 255)

min= 255;if (max > 255)

max= 255;int red = random.nextInt(max - min) +min;int green = random.nextInt(max - min) +min;int blue = random.nextInt(max - min) +min;return newColor(red, green, blue);

}//设置验证码具体的数字或字母是什么

protectedString generateCode() {char[] codes = new char[this.codesLength];for (int i = 0, len = codes.length; i < len; i++) {if(random.nextBoolean()) {

codes[i]= (char) (random.nextInt(10) + 48);

}else{

codes[i]= (char) (random.nextInt(26) + 97);

}

}this.codes = newString(codes);return this.codes;

}

@Overrideprotected voidpaintComponent(Graphics g) {super.paintComponent(g);if(this.codes == null || this.codes.length() != this.codesLength) { //判断生成的验证码是否为空或超出长度

this.codes =generateCode();

}

width= this.codesLength * 16 + (this.codesLength - 1) * 10;super.setSize(width, height); //接口使用,验证码字体大小

super.setPreferredSize(new Dimension(width, height));//接口使用,验证码背景大小

Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25); //设置字体和字体大小

g.setFont(mFont); //设置对象//绘制出验证码的背景的矩形轮廓

Graphics2D g2d =(Graphics2D) g;

g2d.setColor(getRandColor(200, 250));

g2d.fillRect(0, 0, width, height);

g2d.setColor(getRandColor(180, 200));

g2d.drawRect(0, 0, width - 1, height - 1);//绘制出验证码背景的线

int i = 0, len = 150;for (; i < len; i++) {int x = random.nextInt(width - 1);int y = random.nextInt(height - 1);int x1 = random.nextInt(width - 10) + 10;int y1 = random.nextInt(height - 4) + 4;

g2d.setColor(getRandColor(180, 200));

g2d.drawLine(x, y, x1, y1);

}//绘制出验证码的具体字母

i = 0; len = this.codesLength;

FontMetrics fm=g2d.getFontMetrics();int base = (height - fm.getHeight())/2 +fm.getAscent();for(;i

g2d.rotate(random.nextInt(10)*0.01*b);

g2d.setColor(getRandColor(20, 130));

g2d.drawString(codes.charAt(i)+"", 16 * i + 10, base);

}

}//下一个验证码

public voidnextCode() {

generateCode();

repaint();

}

@Overridepublic voidmouseClicked(MouseEvent e) {

nextCode();

}

@Overridepublic voidmousePressed(MouseEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidmouseReleased(MouseEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidmouseEntered(MouseEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidmouseExited(MouseEvent e) {//TODO Auto-generated method stub

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值