用Java编登录用户界面

package aaa;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.JPanel;

class CodePanel extends JPanel {
Random random = new Random();// 随机数实例化
private String code = "";// 验证码

public CodePanel() {
this.setVisible(true);
setLayout(null);
}

public void paint(Graphics g) {
String wenzi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 验证码的字母
BufferedImage image = new BufferedImage(120, 35, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.getGraphics();
if (!code.isEmpty()) {
code = "";
}
graphics.setFont(new Font("楷体", Font.BOLD, 20));
graphics.fillRect(0, 0, 120, 35);

for (int i = 0; i < 4; i++) {//生成四个待验证的文字
int index = random.nextInt(wenzi.length());
String s = wenzi.substring(index, index + 1);// 截取字段
code += s;// 验证码新增截取字段
graphics.setColor(Color.green);
Graphics2D g2d = (Graphics2D) graphics;
AffineTransform transform = new AffineTransform();
transform.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
transform.scale(scaleSize, scaleSize); // 进行缩放
g2d.setTransform(transform);// 设置AffineTransform对象
graphics.drawString(s, 120 / 6 * i + 28, 35 / 2);// 画出验证码
}
g.drawImage(image, 0, 0, null);
}

public void draw() {
repaint();// 重新绘制验证码
}

public String getNum() {
return code;// 返回验证码
}
}

package aaa;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

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;

public class yanzhengma extends JFrame {
private JTextField nameText;// 用户名输入框
private JPasswordField pwdText;// 密码输入框
private JTextField codeText;// 验证码输入框
private JButton button_1;// 登陆按钮
private CodePanel codePanel = null;
private int num = 0;// 计数器

public yanzhengma() {
super();
setResizable(false);
setTitle("Java实现验证码功能");
setBounds(700, 450, 400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
codePanel = new CodePanel();
codePanel.setBounds(150, 67, 100, 35);
getContentPane().add(codePanel);

JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel, BorderLayout.CENTER);

JLabel label_1 = new JLabel();
label_1.setText("用户名:");
label_1.setBounds(10, 10, 50, 20);
panel.add(label_1);

nameText = new JTextField();
nameText.setBounds(60, 10, 300, 20);
panel.add(nameText);

JLabel label_2 = new JLabel();
label_2.setText("密 码:");
label_2.setBounds(10, 35, 50, 20);
panel.add(label_2);

pwdText = new JPasswordField();
pwdText.setBounds(60, 35, 300, 20);
panel.add(pwdText);

JLabel label_3 = new JLabel();
label_3.setText("验证码:");
label_3.setBounds(10, 75, 50, 20);
panel.add(label_3);

codeText = new JTextField();
codeText.setBounds(60, 70, 80, 30);
panel.add(codeText);

button_1 = new JButton("登录");
button_1.setBounds(30, 110, 80, 20);
panel.add(button_1);
button_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = nameText.getText();// 从文本框中获取用户名
String password = new String(pwdText.getPassword());// 从密码框中获取密码
String code = codeText.getText().toUpperCase();// 获得输入的验证码,并转换为大写字母
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 = "验证码为空!";
}
// 如果用户名与密码均为"123456",则登录成功
else if (username.equals("123456") && password.equals("123456")) {
info = "登录成功!";
info = "登录成功!";
codePanel.draw();// 更新验证码
} else {
info = "用户名或密码错误!";
num++;
if (num == 3) {
button_1.setEnabled(false);
// 设置计时任务 1s 循环5次 停止任务
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i = 5;
public void run() {
i--;
button_1.setText(i + "秒");
if (i <= 0) {
button_1.setEnabled(true);// 设置按钮可点击 并且停止任务
button_1.setText("登陆");
codePanel.draw();// 更新验证码
timer.cancel();
}
}
}, 0, 1000);
}
}

JOptionPane.showMessageDialog(null, info);// 通过对话框弹出用户登录信息
}

});

JButton button_2 = new JButton("重置");
button_2.setBounds(130, 110, 80, 20);
panel.add(button_2);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nameText.setText("");// 清空用户名文本框
pwdText.setText("");// 清空密码框
codeText.setText("");// 清空验证码框
}
});

JButton button_3 = new JButton("换一张");
button_3.setBounds(230, 110, 80, 20);
panel.add(button_3);
button_3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//待补充的代码
    if (codePanel != null) {
        codePanel.draw();// 更新验证码
        }
}
});
}

public static void main(String[] args) {
yanzhengma frame = new yanzhengma();
frame.setVisible(true);// 窗体
}
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值