package 验证码;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.Scanner;
public class 验证码 {
public static void main(String[] args) {
验证码 log=new 验证码();
log.login();
}
public void login (){
JFrame jf=new JFrame();
jf.setTitle("登录界面");
jf.setSize(400,200);//创建宽高
jf.setResizable(false);
jf.setLocationRelativeTo(null);//设置居中打开
jf.setDefaultCloseOperation(3);//设置关闭
jf.setVisible(true);
FlowLayout flow=new FlowLayout();
jf.setLayout(flow);
JButton btn=new JButton("登录");
JLabel loginNumber=new JLabel("账号");
JLabel passWord=new JLabel("密码");
JLabel Code=new JLabel("验证码");
String code="";
String data="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random r=new Random();
for (int i = 0; i < 6; i++) {
int index=r.nextInt(data.length());
code+=data.charAt(index);
}
String a=code;
JTextField nameInput1=new JFormattedTextField();
JTextField nameInput2=new JFormattedTextField();
JTextField nameInput3=new JFormattedTextField();
JTextField nameInput4=new JFormattedTextField(code);
btn.setText("登录");
Dimension dim=new Dimension(350,30);
Dimension dim1=new Dimension(150,30);
nameInput1.setPreferredSize(dim);
nameInput2.setPreferredSize(dim);
nameInput3.setPreferredSize(dim1);
nameInput4.setPreferredSize(dim1);
jf.add(loginNumber);
jf.add(nameInput1);
jf.add(passWord);
jf.add(nameInput2);
jf.add(Code);
jf.add(nameInput3);
jf.add(nameInput4);
jf.add(btn);
btn.addActionListener(actionEvent-> {
if (nameInput1.getText().equals("123456")) {
}else {
System.out.println("账号错误");
}
if (nameInput2.getText().equals("123456")) {
}else {
System.out.println("密码错误,请您重新输入密码");
}
if (nameInput3.getText().equals(a)) {
System.out.println("登陆成功");
} else {
System.out.println("验证码错误,请您重新输入验证码");
}
});
jf.setVisible(true);
}
}
标签:code,18,验证码,add,2023,new,jf,JLabel From: https://www.cnblogs.com/618zyk/p/17713125.html