1. 程式人生 > >實現輸入驗證碼的功能

實現輸入驗證碼的功能

有關 operation password 登陸 註冊 def you != 思路

實驗題目:

技術分享

實驗思路:

首先對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

}
}

結果截圖:

技術分享

實驗總結:

這個程序 有關圖形界面設計和事件處理,而且類似這個程序的程序和我們的生活十分緊密,所以對這一塊還要熟練掌握呀。這一塊掌握的還不是很熟,還要努力。把這次出錯誤的地方要高度註意。

實現輸入驗證碼的功能