话不多说,先上代码,两个java类
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* 主窗体类4932
*/
public class MainFrame extends JFrame {
private JTextField codeText; // “验证码:”文本框
private JPasswordField pwdText; // “密码:”密码框
private JTextField nameText; // “用户名:”文本框
ImageCodePanel imageCode = null; // 验证码面板
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
public MainFrame() {
//super();
setResizable(false);
setTitle("带有图片验证码的登录窗体");
setBounds(100, 100, 426, 210);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
imageCode = new ImageCodePanel();// 创建验证码面板
imageCode.setBounds(170, 85, 106, 35);// 设置位置
getContentPane().add(imageCode); // 添加验证码
final JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel, BorderLayout.CENTER);
final JLabel label = new JLabel();
label.setText("用户名:");
label.setBounds(29, 25, 66, 18);
panel.add(label);
final JLabel label_1 = new JLabel();
label_1.setText("密码:");
label_1.setBounds(29, 59, 66, 18);
panel.add(label_1);
nameText = new JTextField();
nameText.setBounds(85, 23, 310, 22);
panel.add(nameText);
pwdText = new JPasswordField();
pwdText.setBounds(85, 57, 310, 22);
panel.add(pwdText);
final JLabel label_1_1 = new JLabel();
label_1_1.setText("验证码:");
label_1_1.setBounds(29, 95, 66, 18);
panel.add(label_1_1);
codeText = new JTextField();
codeText.setBounds(85, 93, 77, 22);
panel.add(codeText);
final JButton button = new JButton();
button.setText("换一张");
button.setBounds(301, 90, 94, 28);
panel.add(button);
button.addActionListener(e -> {
if (imageCode != null) {
imageCode.draw(); // 调用方法生成验证码
}
});
final JButton button_1 = new JButton();
button_1.setText("登录");
button_1.setBounds(42, 134, 106, 28);
panel.add(button_1);
button_1.addActionListener(e -> {
String username = nameText.getText();// 从文本框中获取用户名
String password = new String(pwdText.getPassword());// 从密码框中获取密码
String code = codeText.getText();// 获得输入的验证码
String info = "";// 用户登录信息
// 判断用户名是否为null或空的字符串
if (username == null || username.isEmpty()) {
info = "用户名为空!";
}
// 判断密码是否为null或空的字符串
else if (password == null || password.isEmpty()) {
info = "密码为空!";
}
// 判断验证码是否为null或空的字符串
else if (code == null || code.isEmpty()) {
info = "验证码为空!";
}
// 判断 验证码是否正确
else if (!code.equals(imageCode.getNum())) {
info = "验证码出错啦!";
}
// 如果用户名与密码均为匹配,则登录成功
else if (username.equals("4932") && password.equals("4932")) {
info = "恭喜您,登录成功";
} else {
info = "用户名或密码错误!";
}
JOptionPane.showMessageDialog(null, info);// 通过对话框弹出用户登录信息
});
final JButton button_1_1 = new JButton();
button_1_1.setText("重置");
button_1_1.setBounds(191, 134, 106, 28);
panel.add(button_1_1);
button_1_1.addActionListener(e -> {
nameText.setText("");// 清除用户名文本框内容
pwdText.setText("");// 清除密码文本框内容
codeText.setText("");// 清除验证码文本框内容
});
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* 验证码面板类4932
*/
class ImageCodePanel extends JPanel {
private static final long serialVersionUID = -3124698225447711692L;
public static final int WIDTH = 120;// 宽度
public static final int HEIGHT = 35;// 高度
private StringBuilder num = new StringBuilder(); // 验证码
Random random = new Random();// 实例化Random
public ImageCodePanel() {
this.setVisible(true);// 显示面板
setLayout(null);// 空布局
}
public void paint(Graphics g) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);// 实例化BufferedImage对象,颜色模型为RGB
Graphics gs = image.getGraphics(); // 获取Graphics类的对象
if (!num.toString().isEmpty()) {
num = new StringBuilder();// 清空验证码
}
Font font = new Font("黑体", Font.BOLD, 20); // 通过Font构造字体
gs.setFont(font);// 设置字体
gs.fillRect(0, 0, WIDTH, HEIGHT);// 填充一个矩形
Image img = null;
try {
img = ImageIO.read(new File("D:\\UserData\\lenovo\\Desktop\\1.PNG"));
// 创建图像对象
} catch (IOException e) {
e.printStackTrace();
}
gs.drawImage(img, 0, 0, WIDTH, HEIGHT, null);// 在缓冲图像对象上绘制图像
// 输出随机的验证文字
for (int i = 0; i < 4; i++) {
char ctmp = (char) (random.nextInt(26) + 65); // 生成A~Z的字母
num.append(ctmp);// 更新验证码
Color color = new Color(20 + random.nextInt(120), 20 + random.nextInt(120), 20 + random.nextInt(120));// 生成随机颜色
gs.setColor(color); // 设置颜色
Graphics2D gs2d = (Graphics2D) gs;// 将文字旋转指定角度
AffineTransform trans = new AffineTransform();// 实例化AffineTransform
trans.rotate(random.nextInt(45) * 3.14 / 180, 22 * i + 8, 7);
float scaleSize = random.nextFloat() + 0.8f;// 缩放文字
if (scaleSize > 1f)
scaleSize = 1f;// 如果scaleSize大于1,则等于1
trans.scale(scaleSize, scaleSize); // 进行缩放
gs2d.setTransform(trans);// 设置AffineTransform对象
gs.drawString(String.valueOf(ctmp), WIDTH / 6 * i + 28, HEIGHT / 2);// 画出验证码
}
super.paint(g);// 绘制组件的默认外观
g.drawImage(image, 0, 0, null);// 在面板中画出验证码
}
// 生成验证码的方法
public void draw() {
repaint();// 调用paint()方法
}
public String getNum() {
return num.toString();// 返回验证码
}
}
请注意,验证码面板类中有一个图片引用,默认是引用桌面的1.png图片文件,如果报错,请优先查看该部分代码
最后,求三连,求三连,求三连!!!