验证码程序
package Yzm.awt;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.*;
public class Yanzhengma extends JFrame {
public Yanzhengma() {
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,1));
panel1.add(new JButton("登陆"));
panel1.add(new JButton("快速注册"));
add(panel1, BorderLayout.SOUTH);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.setLayout(new GridLayout(3,3));
panel2.add(new JButton("登录名"));
panel2.add(new JTextField(" "),BorderLayout.CENTER);
JTextField jf = new JTextField(" ");
jf.setEditable(false);
panel2.add(jf,BorderLayout.CENTER);
panel2.add(new JButton("密码"));
panel2.add(new JTextField(" "),BorderLayout.CENTER);
JTextField jf1 = new JTextField("忘记用户名/密码?");
jf1.setEditable(false);
panel2.add(jf1,BorderLayout.CENTER);
panel2.add(new JButton("验证码"));
int a = new Random().nextInt(9);
int b = new Random().nextInt(9);
int c = new Random().nextInt(9);
int d = new Random().nextInt(9);
panel2.add(new JTextField(" "),BorderLayout.CENTER);
JTextField jf2 = new JTextField(""+a+b+c+d);
jf2.setEditable(false);
panel2.add(jf2,BorderLayout.CENTER);
add(panel2,BorderLayout.NORTH);
}
public static void main(String args[]) {
Yanzhengma frame = new Yanzhengma();
frame.setTitle("请登录");
frame.setSize(380,175);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
public class Yanzhengma extends JFrame {
public Yanzhengma() {
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,1));
panel1.add(new JButton("登陆"));
panel1.add(new JButton("快速注册"));
add(panel1, BorderLayout.SOUTH);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.setLayout(new GridLayout(3,3));
panel2.add(new JButton("登录名"));
panel2.add(new JTextField(" "),BorderLayout.CENTER);
JTextField jf = new JTextField(" ");
jf.setEditable(false);
panel2.add(jf,BorderLayout.CENTER);
panel2.add(new JButton("密码"));
panel2.add(new JTextField(" "),BorderLayout.CENTER);
JTextField jf1 = new JTextField("忘记用户名/密码?");
jf1.setEditable(false);
panel2.add(jf1,BorderLayout.CENTER);
panel2.add(new JButton("验证码"));
int a = new Random().nextInt(9);
int b = new Random().nextInt(9);
int c = new Random().nextInt(9);
int d = new Random().nextInt(9);
panel2.add(new JTextField(" "),BorderLayout.CENTER);
JTextField jf2 = new JTextField(""+a+b+c+d);
jf2.setEditable(false);
panel2.add(jf2,BorderLayout.CENTER);
add(panel2,BorderLayout.NORTH);
}
public static void main(String args[]) {
Yanzhengma frame = new Yanzhengma();
frame.setTitle("请登录");
frame.setSize(380,175);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
![](https://img2018.cnblogs.com/blog/1279396/201810/1279396-20181008222947081-1578941558.png)
![](https://img2018.cnblogs.com/blog/1279396/201810/1279396-20181008223003292-1630924153.png)
存在的问题:文本框的边框无法去除,影响美观。随机生成的0-9的数字来作为验证码,没有那种有划线来阻碍观察的验证码。