首页 > 其他分享 >9.17

9.17

时间:2023-09-17 22:23:33浏览次数:35  
标签:String 9.17 JPanel add verificationCode new public

生成验证码的Java程序

 

package lll;

import java.util.Random;

public class VerificationCodeGenerator {
    public static void main(String[] args) {
        String verificationCode = generateVerificationCode(6);
        System.out.println("验证码:" + verificationCode);
    }

    public static String generateVerificationCode(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder verificationCode = new StringBuilder();
        
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            verificationCode.append(characters.charAt(index));
        }
        
        return verificationCode.toString();
    }
}

  

package lll;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class VerificationCodeGeneratorGUI extends JFrame {
    private JTextField lengthTextField;
    private JLabel verificationCodeLabel;

    public VerificationCodeGeneratorGUI() {
        setTitle("验证码生成器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JPanel lengthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        lengthPanel.add(new JLabel("验证码长度:"));
        lengthTextField = new JTextField(10);
        lengthPanel.add(lengthTextField);
        mainPanel.add(lengthPanel);

        JPanel generatePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton generateButton = new JButton("生成验证码");
        generateButton.addActionListener(new GenerateButtonListener());
        generatePanel.add(generateButton);
        mainPanel.add(generatePanel);

        JPanel verificationCodePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        verificationCodePanel.add(new JLabel("验证码:"));
        verificationCodeLabel = new JLabel();
        verificationCodePanel.add(verificationCodeLabel);
        mainPanel.add(verificationCodePanel);

        add(mainPanel);
        setVisible(true);
    }

    private class GenerateButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int length = Integer.parseInt(lengthTextField.getText());
            String verificationCode = generateVerificationCode(length);
            verificationCodeLabel.setText(verificationCode);
        }
    }

    public static String generateVerificationCode(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder verificationCode = new StringBuilder();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            verificationCode.append(characters.charAt(index));
        }

        return verificationCode.toString();
    }

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

  

module dada {
	requires java.desktop;
}

  

标签:String,9.17,JPanel,add,verificationCode,new,public
From: https://www.cnblogs.com/jais/p/17709966.html

相关文章

  • 2023.9.17——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午做任务,下午做任务。我了解到的知识点:1.完成任务环境配置;明日计划:1.上课;2.继续完成我的任务;......
  • 9.17
    packagefrist1;importjava.awt.*;importjavax.swing.*;importjavax.swing.JFrame;//使窗体位于屏幕中间publicclassDemo1{publicstaticvoidmain(String[]args){JFramewindow1=newJFrame();//java提供的JFrame类的实例是一个底层容器,即通......
  • 9.17
    importjava.util.Scanner;classWarehouseInformation{privateStringitemNumber;privateStringitemName;privateStringsupplierName;privateStringwarehousingTime;privateStringshipmentTime;privateStringwarehouseNumber;privat......
  • 畅购商城学习日志9.17
    org.springframework.beans.factory.BeanDefinitionStoreException:Failedtoparseconfigurationclass[com.changgou.GoodsApplication];nestedexceptionisorg.springframework.context.annotation.ConflictingBeanDefinitionException:Annotation-specifiedbeann......
  • 9.17课堂测试更改代码
    importjava.util.ArrayList;importjava.util.Scanner;publicclassmainFrame{publicstaticvoidmain(String[]args){ArrayList<WarehouseInformation>array=newArrayList<WarehouseInformation>();while(true){Syste......
  • 测试重写9.17
    packageqiyexinxi;importjava.util.Scanner;publicclassqiyexinxi{ //定义WarehouseInformation类publicstaticclassWarehouseInformation{privateStringitemno;//商品编号privateStringitemname;//商品名称privateStringsuppliername;//供货商名称pri......
  • 登录界面9.17
    packagetest;importjavax.swing.*;importjava.awt.*;publicclasstest2{//创造字符串储存账号publicstaticStringuname[]=newString[100];//创造字符串储存密码publicstaticStringupass[]=newString[100];//记录当前坐标publicstaticintflag_user......
  • 9.17 四次分解
     Importjava.util.Scanner; PublicclassWarehouseManagement{StaticintnumOfItems=0;StaticWarehouseInformation[]warehouseInformations=newWarehouseInformation[5]; Publicstaticvoidmain(String[]args){       initializeData();  ......
  • 每日总结|9.17-别为打翻的牛奶哭泣,今天你能拥有更美味的果汁
    超级喜欢两首歌:越来越不懂-蔡健雅你不明白-Joysaaaa今天没干什么其实,都是一些不太费脑子的。休息了,恢复精力,下周才能元气满满啊!1、王老师的需求文档作业,做了一部分,查一些资料2、看学习视频3、人月神话,今天看了不到两章(我本来还说要每天看一点呢,结果还是比较忙的)4、c#(......
  • 开学测试修改(9.17)
    packagehomework;publicclassWarehouseInformation{privateStringitemno;privateStringitemname;privateStringsuppliername;privateStringwarehousingtime;privateStringshipmenttime;privateStringwarehousenumber;privateStr......