天天看点

实现输入验证码的功能

实验题目:

实现输入验证码的功能

实验思路:

      首先对java的图形界面设计和时间处理这两方面的内容熟悉掌握,一步一步编出各个框  进行输入 ,调位置,运用事件处理的相关知识进行按钮的选择,运用图形界面设计的相关知识进行对框架 标签等的处理。对于随机获取验证码方面的操作,自定义一个字符串库KeyString ,然后通过Math.random()方法获取KeyString长度内的一个随机数,接着再获取该随机数对应KeyString中相应位置的一个字符,最后将随机获取并组装好的字符串返回。

实验流程图:

实现输入验证码的功能

源程序:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.Container;

public class App14_6 extends JFrame implements ActionListener

{

JButton shuaxin=new JButton("刷新");

JButton loginBtn=new JButton("快速注册");

JButton forgetBtn=new JButton("登录");

JLabel maBtn=new JLabel("123456", JLabel.CENTER);//具体验证码按钮

private JLabel[] jlArray= {new JLabel("用户名"),new JLabel("密 码"),new JLabel("验证码")};

private JTextField jName=new JTextField();//用户名

private JPasswordField jPassword=new JPasswordField();//密码

private JTextField jPassword1=new JTextField();//输入验证码的地方

private String shell;

public App14_6()

{

String KeyString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

StringBuffer sb = new StringBuffer();

int len = KeyString.length();

for (int i = 0; i < 6; i++)

{

sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));//随机产生六位数

}//生成六位随机码

String x=sb.toString();

String y=String.valueOf(x);

maBtn.setText(y);//验证码那一栏

maBtn.setForeground(Color.BLUE);

maBtn.setBounds(170,60,90,25);

shuaxin.setBounds(265,60,70,25);

loginBtn.setBounds(100,180,120,25);

forgetBtn.setBounds(100,140,120,25);

this.setLayout(null);

jlArray[0].setBounds(20,10,50,25);

jName.setBounds(70,10,170,25);

jlArray[1].setBounds(20,35,50,25);

jPassword.setBounds(70,35,170,25);

jlArray[2].setBounds(20,60,50,25);

jPassword1.setBounds(70,60,100,25);

this.add(jlArray[0]);

this.add( jlArray[1]);

this.add( jlArray[2]);

this.add(jName);

this.add(jPassword);

this.add(jPassword1);

this.add(maBtn);

this.add(shuaxin);

this.add(loginBtn);

this.add(forgetBtn);

shuaxin.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String KeyString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

StringBuffer sb = new StringBuffer();

int len = KeyString.length();

for (int i = 0; i < 6; i++)

{

sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));//随机产生六位数

}//生成六位随机码

String x=sb.toString();

String y=String.valueOf(x);

maBtn.setText(y);

}

});//对刷新按钮进行监听

forgetBtn.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

if(jPassword1.getText().equals(maBtn.getText())&&jName.getText()!=null&&!String.valueOf(jPassword.getPassword()).equals(null))

{JOptionPane.showConfirmDialog(null, "登陆成功!","登陆结果",JOptionPane.CLOSED_OPTION);System.exit( 0 );}

else

JOptionPane.showConfirmDialog(null, "登陆失败!请重新登录!","登陆结果",JOptionPane.CLOSED_OPTION);

}

});//对登录按钮进行监听

loginBtn.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JOptionPane.showConfirmDialog(null, "待开发,抱歉!","登陆结果",JOptionPane.CLOSED_OPTION);

}

});//对快速注册按钮进行监听

this.setTitle("登录窗口");

this.setResizable(false);

this.setBounds(300,150,360,320);

}

public static void main(String[] args)

{

App14_6 frm=new App14_6();

Image im=(new ImageIcon("123.jpg")).getImage();

frm.setIconImage(im);

frm.setVisible(true);

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override

public void actionPerformed(ActionEvent e)

{

// TODO Auto-generated method stub

}

}

结果截图:

实验总结:

这个程序 有关图形界面设计和事件处理,而且类似这个程序的程序和我们的生活十分紧密,所以对这一块还要熟练掌握呀。这一块掌握的还不是很熟,还要努力。把这次出错误的地方要高度注意。