今天完成了java对话框的验证码设计
import javax.swing.*;标签:setBounds,15,frame,30,add,new,100,23.9 From: https://www.cnblogs.com/Zzzhy0316/p/17718512.html
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
char ain[] = {'a', 'b', 'c', 'd', 'e', 'A', 'R', 'O', 'P', '2', '3', '4', '5', '6', '7'};
StringBuilder P = new StringBuilder();
Random random = new Random();
StringBuilder u = new StringBuilder();
for (int i = 0; i < 4; i++) {
int r = random.nextInt(0, 14);
u.append(ain[r]);
}
JFrame frame=new JFrame("登入系统");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JLabel user=new JLabel("账户:");
user.setBounds(10, 10, 100, 30);
frame.add(user);
JTextField userword =new JTextField();
userword.setBounds(50, 10, 100, 30);
frame.add(userword);
JLabel pass=new JLabel("密码:");
pass.setBounds(10, 50, 100, 30);
frame.add(pass);
JTextField password =new JTextField();
password.setBounds(50, 50, 100, 30);
frame.add(password);
JLabel o=new JLabel("验证码"+" "+u.toString());
o.setBounds(10, 90, 100, 30);
frame.add(o);
JTextField y=new JTextField();
y.setBounds(50,120,100,30);
frame.add(y);
JButton b1=new JButton("登入");
b1.setBounds(10, 200, 100, 30);
frame.add(b1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userInput = userword.getText();
String passwordInput = password.getText();
String yInput = y.getText();
String a = "123456";
String b = "123456";
if (userInput.isEmpty() || passwordInput.isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入账号和密码");
} else if (userInput.equals(a) && passwordInput.equals(b)) {
JOptionPane.showMessageDialog(null, "登入成功");
} else {
JOptionPane.showMessageDialog(null, "输入错误");
}
}
});
frame.setVisible(true);
}
}