首页 技术 正文
技术 2022年11月14日
0 收藏 317 点赞 3,741 浏览 3053 个字

//Java实现简单验证码功能

package project;

import java.awt.Color;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

@SuppressWarnings(“serial”)
public class denglu extends JFrame {

private JLabel name, pass, card, imageCard;
private JTextField nameText, passText, cardText;
private JButton login;
private int width = 100, height = 30;
private String str = “”;
public denglu() {
setTitle(“登陆窗体”);
setFont(new Font(“”, Font.BOLD, 24));
setLayout(null);// 自定义布局

// 负责产生验证码图片
Icon icon = new ImageIcon(getCardImage(width, height));
name = new JLabel(“账 号”);
pass = new JLabel(“密 码”);
card = new JLabel(“验证码”);
imageCard = new JLabel(icon);

nameText = new JTextField();
passText = new JTextField();
cardText = new JTextField();

login = new JButton(“登 录”);

name.setBounds(80, 20, 60, 30);
pass.setBounds(80, 60, 60, 30);
card.setBounds(80, 100, 60, 30);
imageCard.setBounds(240, 100, width, height);

nameText.setBounds(150, 20, 200, 30);
passText.setBounds(150, 60, 200, 30);
cardText.setBounds(150, 100, 80, 30);

login.setBounds(120, 160, 220, 30);

add(name);
add(pass);
add(card);
add(imageCard);

add(nameText);
add(passText);
add(cardText);

add(login);

//注册事件
addMouseListener(new ChangeCard());

setBounds(0, 0, 450, 260);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public Image getCardImage(int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();
int red1 = (int) (Math.random() * 256);
int green1 = (int) (Math.random() * 256);
int blue1 = (int) (Math.random() * 256);
Color c1 = new Color(red1, green1, blue1);

g.setColor(c1);
g.fillRect(0, 0, width, height);

//生成文字
int red2 = (int) (Math.random() * 256);
int green2 = (int) (Math.random() * 256);
int blue2 = (int) (Math.random() * 256);
Color c2 = new Color(red2, green2, blue2);
str = “”;
for (int i = 0; i < 4; i++) {
char ch = (char)((int)(Math.random()*26+65));
str+=ch;
}

//加入干扰点
for (int i = 0; i < 60; i++) {
int red11 = (int)(Math.random()*256);
int green11 = (int)(Math.random()*256);
int blue11 = (int)(Math.random()*256);

Color color3 = new Color(red11, green11, blue11);
g.setColor(color3);
int x1 = (int)(Math.random()*width);
int y1 = (int)(Math.random()*height);
int x2 = (int)(Math.random()*width);
int y2 = (int)(Math.random()*height);
g.drawLine(x1, y1, x2, y2);

}

g.setColor(c2);
int x = (int)(Math.random()*(width/3));
int y = (int)(Math.random()*(height/2)+10);
g.setFont(new Font(“”, Font.BOLD, 24));
g.drawString(str, x, y);

return image;
}

//监听类:鼠标单击时执行,即单击验证码时再次随机产生一个验证码
class ChangeCard implements MouseListener{

@Override
public void mouseClicked(MouseEvent e) {
Icon icon = new ImageIcon(getCardImage(width, height));
imageCard.setIcon(icon);
}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

}

public static void main(String[] args) {
new denglu();
}
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292