實驗題目:

實驗思路:
首先對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
}
}
結果截圖:
實驗總結:
這個程式 有關圖形界面設計和事件處理,而且類似這個程式的程式和我們的生活十分緊密,是以對這一塊還要熟練掌握呀。這一塊掌握的還不是很熟,還要努力。把這次出錯誤的地方要高度注意。