原码:
test.java:
package tuxing;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionEvent; //监听键盘与鼠标。
import java.awt.event.ActionListener;
import java.util.Random;
public class test implements ActionListener {
private JFrame f;
private JLabel lName;
private JLabel lPassword;
private JLabel lyanzheng;
private JLabel wj;
private JButton yanzheng;
private JTextField tfName;
private JTextField tfPassword;
private JTextField tfyanzheng;
private JButton jb;
private String s;
private JButton zhuce;
private ch c;
private int n;
public void designFrame(){
c=new ch();
n=0;
f=new JFrame("新课程添加");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
lName = new JLabel("账号:");
// 输入框
tfName = new JTextField("");
lName.setBounds(20,30,300,30);
tfName.setBounds(60,30,120,30);
lPassword = new JLabel("密码:");
// 输入框
tfPassword = new JTextField("");
lPassword.setBounds(20,70,300,30);
tfPassword.setBounds(60,70,120,30);
lyanzheng=new JLabel("验证码:");
tfyanzheng=new JTextField ("");
lyanzheng.setBounds(20,110,300,30);
tfyanzheng.setBounds(60,110,120,30);
JLabel l = new JLabel();
jb=new JButton("确定");
jb.setBounds(40,150,60,30);
jb.addActionListener(this);
Random ra=new Random();
String a="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
StringBuffer sb=new StringBuffer();
for(int i=0;i<4;i++){
int number=ra.nextInt(62);
sb.append(a.charAt(number));
}
s = new String(sb);
yanzheng=new JButton(s);
yanzheng.setBounds(200,110,100,30);
yanzheng.setBackground(Color.gray);
yanzheng.addActionListener(this);
zhuce =new JButton("注册");
zhuce.setBounds(120,150,60,30);
zhuce.addActionListener(this);
wj=new JLabel("忘记密码?");
wj.setForeground(Color.blue);
wj.setBounds(200, 70, 100, 30);
f.add(lName);
f.add(tfName);
f.add(lPassword);
f.add(tfPassword);
f.add(lyanzheng);
f.add(tfyanzheng);
f.add(jb);
f.add(yanzheng);
f.add(zhuce);
f.add(wj);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
tfPassword.grabFocus();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()jb){
if(n0){
if(s.equals(tfyanzheng.getText())&&c.getZhanghao().equals(tfName.getText())&&c.getMima().equals(tfPassword.getText())){
JOptionPane.showMessageDialog(f, "登录成功");
}
else{
JOptionPane.showMessageDialog(f, "登陆失败");
}
}
else if(n1){
c.setch(tfName.getText(), tfPassword.getText());
JOptionPane.showMessageDialog(f, "注册成功");
n=0;
f.setTitle("请登陆");
lyanzheng.setBounds(20,110,300,30);
yanzheng.setBounds(200,110,100,30);
tfyanzheng.setBounds(60,110,120,30);
wj.setBounds(200, 70, 100, 30);
zhuce.setBounds(120,150,60,30);
}
}
else if(e.getSource()yanzheng){
Random ra=new Random();
String a="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
StringBuffer sb=new StringBuffer();
for(int i=0;i<4;i++){
int number=ra.nextInt(62);
sb.append(a.charAt(number));
}
s = new String(sb);
yanzheng.setText(s);
}
else if(e.getSource()==zhuce){
n=1;
System.out.println("1");
f.setTitle("请注册");
lyanzheng.setBounds(0,0,0,0);
yanzheng.setBounds(0,0,0,0);
tfyanzheng.setBounds(0,0,0,0);
wj.setBounds(0,0,0,0);
zhuce.setBounds(0,0,0,0);
}
}
public static void main(String[] args)
{
test t=new test();
t.designFrame();
}
}
ch.java:
package tuxing;
public class ch {
private String zhanghao;
private String mima;
public ch(String zh,String mm)
{
zhanghao=zh;
mima=mm;
}
public ch()
{
}
public void setch(String zh,String mm)
{
zhanghao=zh;
mima=mm;
}
public String getZhanghao() {
return zhanghao;
}
public void setZhanghao(String zhanghao) {
this.zhanghao = zhanghao;
}
public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
}
功能实现:
将界面的设计放入函数designFrame();用户的账号信息由类ch储存.随机数方面先用String储存所有的小写大写字母与09的数字(共62个字符),随后生成一个从061的随机数,随机数为多少,就用一个新的String类型储存与之相对应的字符,重复4遍得到一个包含4个字符的随机生成的验证码。