首页 > 其他分享 >星期三

星期三

时间:2023-09-13 18:35:13浏览次数:31  
标签:codeText code graphics 星期三 import new panel

验证码界面 import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;

public class VerificationCode extends JFrame {
private String code = "";
private JTextField codeText;
private JButton button1;

VerificationCode() {
super();
setResizable(false);
setTitle("Java实现验证码功能");
setBounds(700, 450, 300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel, BorderLayout.CENTER);
JLabel label1 = new JLabel();
label1.setText("验证码:");
label1.setBounds(10, 100, 50, 20);
panel.add(label1);
JTextField codeText = new JTextField();
codeText.setBounds(60, 100, 100, 20);
panel.add(codeText);
JButton button1 = new JButton("确定");
button1.setBounds(60, 140, 80, 20);
panel.add(button1);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String info = "";
if (codeText.getText() == null)
info = "验证码为空";
else if (!codeText.getText().equalsIgnoreCase(code))
info = "验证码错误";
else
System.exit(0);
JOptionPane.showMessageDialog(null, info);
}
});
}

public void paint(Graphics g){
BufferedImage image = new BufferedImage(200, 100, 1);
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.GREEN);
Stroke stroke = new BasicStroke(2.0f);
graphics.setStroke(stroke);
graphics.setFont(new Font("仿宋", Font.BOLD, 20));
Random r = new Random();
for (int i = 1; i <= 8; i++) {
char c;
String C="";
switch (r.nextInt(2) + 1) {
case 1:
c = (char)(r.nextInt(9) + 48);
C+=c;
graphics.drawString(C, i * 10, 30);
code += c;
break;
case 2:
c = (char) (r.nextInt(26) + 65);
graphics.drawString(C+c, i * 10, 30);
code += c;
break;
case 3:
c = (char) (r.nextInt(26) + 97);
graphics.drawString(C+c, i * 10, 30);
code += c;
break;
}
}
g.drawImage(image, 40, 20, null);
}
public static void main(String[] args) {
VerificationCode frame = new VerificationCode();
frame.setVisible(true);
}

 

 

标签:codeText,code,graphics,星期三,import,new,panel
From: https://www.cnblogs.com/chukjbgg333/p/17700420.html

相关文章

  • kali官方博客 --- Kali Linux 2023.3 发布(内部基础架构与 Kali Autopilot) 2023 年 8
    今天,我们很高兴向大家介绍Kali的最新版本2023.3。这次的更新虽然没有在外观上增加很多新功能,但其中许多改进都是在系统内部进行的,这些改进对于开发团队来说非常有益处,同时也会以间接的方式对最终用户产生积极的影响。不言而喻,除了标准更新外,还有许多新软件包和工具。如果你想......
  • 7.5 星期三
      在编译软件上练习了昨天学习的循环语言。实际感觉编写下来和C语言有相似之处,不算太困难,可能是做的题太过简单。今天阅读了《大道至简》的第一章内容,颇有感触。明天计划:对循环语句进行深度练习,并学习选择等语句结构......
  • 第六周星期三每日总结
      今日完成地铁查询系统android端线路查询功能的开发,站点查询可参考此查询方式。但是此查询方式仍有不足,比如会遇到查询信息过长界面不能完全显示导致的软件闪退,期望再......