日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

创建登录界面及简易验证码

發(fā)布時間:2023/12/13 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 创建登录界面及简易验证码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一.程序設(shè)計思路: 1.顯示登錄,注冊頁面,以及主頁面

2.可以在表里存儲數(shù)據(jù),即注冊用戶

3.可以在圖形框中輸入數(shù)據(jù)

4.在后端檢索所有數(shù)據(jù),查看是否存在數(shù)據(jù)

5.隨機(jī)生成驗(yàn)證碼,并且要求輸入的驗(yàn)證碼與顯示的對應(yīng)

6.完成登錄

二.流程圖

三.源代碼

/*本類將作為上網(wǎng)時的登錄頁面

需要實(shí)現(xiàn)的功能:

1.顯示登錄,注冊頁面,以及主頁面

2.可以在表里存儲數(shù)據(jù),即注冊用戶

3.可以在圖形框中輸入數(shù)據(jù)

4.在后端檢索所有數(shù)據(jù),查看是否存在數(shù)據(jù)

5.隨機(jī)生成驗(yàn)證碼,并且要求輸入的驗(yàn)證碼與顯示的對應(yīng)

6.完成登錄

* */

package class3Login;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class Interface extends JFrame

{

private static final long serialVersionUID = 1L;

private static String record;

public static void main(String[] args)

{

new Interface();

}

public Interface()

{

// 創(chuàng)建 JFrame 實(shí)例

JFrame frame = new JFrame("請登錄:");

// 設(shè)置frame大小

frame.setLocation(500,300);

frame.setSize(400, 200);

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//創(chuàng)建面板

JPanel panel = new JPanel();

// 添加面板

frame.add(panel);

//根據(jù)需要設(shè)置面板的具體形式

placeComponents(panel);

// 設(shè)置界面可見

frame.setVisible(true);

}

public void placeComponents(JPanel panel)

{

//清空面板

panel.setLayout(null);

// 創(chuàng)建 用戶使用的組件

JLabel userLabel = new JLabel("用戶名:");

userLabel.setBounds(10,20,80,25);

panel.add(userLabel);

// 創(chuàng)建文本域用于用戶輸入

JTextField userText = new JTextField(20);

userText.setBounds(100,20,165,25);

panel.add(userText);

// 創(chuàng)建 密碼使用的組件

JLabel passwordLabel = new JLabel("密碼:");

passwordLabel.setBounds(10,50,80,25);

panel.add(passwordLabel);

// 創(chuàng)建密碼的文本域

JPasswordField passwordText = new JPasswordField(20);

passwordText.setBounds(100,50,165,25);

panel.add(passwordText);

// 創(chuàng)建 驗(yàn)證碼使用的組件

JLabel verificationLabel = new JLabel("驗(yàn)證碼:");

verificationLabel.setBounds(10,80,80,25);

panel.add(verificationLabel);

// 創(chuàng)建驗(yàn)證碼的文本域

JTextField verificationText = new JTextField(20);

verificationText.setBounds(100,80,80,25);

panel.add(verificationText);

//創(chuàng)建一個隨機(jī)字符串

String result = "";

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

{

int intVal = (int)(Math.random() * 26 + 97);

result = result + (char)intVal;

}

record = result;

// 創(chuàng)建驗(yàn)證碼使用的顯示的組件

JLabel verificationShowLabel = new JLabel(result);

verificationShowLabel.setBounds(200,80,80,25);

panel.add(verificationShowLabel);

// 創(chuàng)建登錄按鈕

JButton loginButton = new JButton("登錄");

loginButton.setBounds(10, 120, 80, 25);

panel.add(loginButton);

//對按鈕進(jìn)行監(jiān)視

ActionListener ourListener1 = new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == loginButton) //判斷是否點(diǎn)擊登錄按鈕

{

if(record.equalsIgnoreCase(verificationText.getText()))//判斷驗(yàn)證碼是否正確

{

if(StoreroomManager.datelist.size()==0)//判斷存儲空間是否有數(shù)據(jù)

{

JOptionPane.showMessageDialog(null, "你還沒有注冊");

userText.setText("");

passwordText.setText("");

}

else//對現(xiàn)有的date進(jìn)行賦值,并且檢索是否存在注冊過的用戶

{

Storeroom date= new Storeroom();

date.setUser(userText.getText());//獲取用戶輸入的用戶名

date.setPassword(passwordText.getText());//獲取用戶輸入的密碼

StoreroomManager.confirmDate(date);

passwordText.setText("");

verificationText.setText("");

}

}

else // 驗(yàn)證碼不正確

{

passwordText.setText("");

verificationText.setText("");

JOptionPane.showMessageDialog(null, "驗(yàn)證碼錯誤,請重新輸入");

//創(chuàng)建一個隨機(jī)字符串

String result = "";

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

{

int intVal = (int)(Math.random() * 26 + 97);

result = result + (char)intVal;

}

record = result;

verificationShowLabel.setText(result);

}

}

}

};

loginButton.addActionListener(ourListener1);

// 創(chuàng)建注冊按鈕

JButton registerButton = new JButton("注冊");

registerButton.setBounds(100, 120, 80, 25);

panel.add(registerButton);

//對按鈕進(jìn)行監(jiān)視

ActionListener ourListener2 = new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == registerButton)

{

userText.setText("");

passwordText.setText("");

verificationText.setText("");

new RegisterInterface();

}

}

};

registerButton.addActionListener(ourListener2);

// 創(chuàng)建刷新按鈕

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

refreshButton.setBounds(250, 80, 80, 25);

panel.add(refreshButton);

//對按鈕進(jìn)行監(jiān)視

ActionListener ourListener3 = new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == refreshButton)

{

passwordText.setText("");

verificationText.setText("");

//創(chuàng)建一個隨機(jī)字符串

String result = "";

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

{

int intVal = (int)(Math.random() * 26 + 97);

result = result + (char)intVal;

}

record = result;

verificationShowLabel.setText(result);

}

}

};

refreshButton.addActionListener(ourListener3);

}

}

//本文件相當(dāng)于數(shù)據(jù)庫,用于存儲數(shù)據(jù)

package class3Login;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JOptionPane;

//作為一個類,用來作為存儲數(shù)據(jù)的單元

class Storeroom

{

private String User;

private String password;

void setUser(String str)

{

User = str;

}

String getUser()

{

return User;

}

void setPassword(String str)

{

password = str;

}

String getpassword()

{

return password;

}

}

class StoreroomManager //對數(shù)據(jù)進(jìn)行操作的類

{

static List<Storeroom> datelist = new ArrayList<Storeroom>(0);

static void confirmDate(Storeroom date)//登錄,遍歷數(shù)據(jù)

{

int flag = 0;

for(int i = 0;i<datelist.size();i++)

{

if(date.getUser().equalsIgnoreCase(datelist.get(i).getUser()))

{

if(date.getpassword().equalsIgnoreCase(datelist.get(i).getpassword()))

{

JOptionPane.showMessageDialog(null, "登錄成功");

flag++;

}

}

}

System.out.println("輸入的數(shù)據(jù):");

System.out.println("用戶名:"+date.getUser());

System.out.println("密碼:"+date.getpassword());

for(int i = 0;i<datelist.size();i++)

{

System.out.println("表中的"+(i+1)+"個數(shù)據(jù):");

System.out.println("用戶名:"+datelist.get(i).getUser());

System.out.println("密碼:"+datelist.get(i).getpassword());

}

if(flag == 0)

{

JOptionPane.showMessageDialog(null, "用戶名或者密碼錯誤!");

}

}

static boolean searchDate(Storeroom date)//登錄,遍歷數(shù)據(jù)

{

int flag = 0;

for(int i = 0;i<datelist.size();i++)

{

if(date.getUser().equals(datelist.get(i).getUser()))

if(date.getpassword().equals(datelist.get(i).getpassword()))

flag++;

}

if(flag==0)

return false;

else

return true;

}

}

//本類文件與Interface類大致相同

packageclass3Login;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JPasswordField;

importjavax.swing.JTextField;

class RegisterInterface //作為注冊頁面

{

privatestaticString record;

publicRegisterInterface()

{

// 創(chuàng)建 JFrame 實(shí)例

JFrame frame= newJFrame("歡迎來到注冊頁面:");

// 設(shè)置frame大小

frame.setLocation(500,300);

frame.setSize(400, 200);

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

//創(chuàng)建面板

JPanel panel= newJPanel();

// 添加面板

frame.add(panel);

//根據(jù)需要設(shè)置面板的具體形式

placeComponents(panel,frame);

// 設(shè)置界面可見

frame.setVisible(true);

}

publicvoidplaceComponents(JPanel panel,JFrame frame)

{

//清空面板

panel.setLayout(null);

// 創(chuàng)建 用戶使用的組件

JLabel userLabel= newJLabel("用戶名:");

userLabel.setBounds(10,20,80,25);

panel.add(userLabel);

// 創(chuàng)建文本域用于用戶輸入

JTextField userText= newJTextField(20);

userText.setBounds(100,20,165,25);

panel.add(userText);

// 創(chuàng)建密碼使用的組件

JLabel passwordLabel= newJLabel("密碼:");

passwordLabel.setBounds(10,50,80,25);

panel.add(passwordLabel);

// 創(chuàng)建密碼的文本域

JPasswordField passwordText= newJPasswordField(20);

passwordText.setBounds(100,50,165,25);

panel.add(passwordText);

// 創(chuàng)建 驗(yàn)證碼使用的組件

JLabel verificationLabel= newJLabel("驗(yàn)證碼:");

verificationLabel.setBounds(10,80,80,25);

panel.add(verificationLabel);

// 創(chuàng)建驗(yàn)證碼的文本域

JTextField verificationText= newJTextField(20);

verificationText.setBounds(100,80,80,25);

panel.add(verificationText);

//創(chuàng)建一個隨機(jī)字符串

String result= "";

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

{

intintVal= (int)(Math.random() * 26 + 97);

result= result+ (char)intVal;

}

record= result;

// 創(chuàng)建驗(yàn)證碼使用的顯示的組件

JLabel verificationShowLabel= newJLabel(result);

verificationShowLabel.setBounds(200,80,80,25);

panel.add(verificationShowLabel);

// 創(chuàng)建確認(rèn)按鈕

JButton confirmButton= newJButton("確定");

confirmButton.setBounds(10, 120, 80, 25);

panel.add(confirmButton);

//對按鈕進(jìn)行監(jiān)視(確認(rèn),即注冊按鈕)

ActionListener ourListener1= newActionListener()

{

publicvoidactionPerformed(ActionEvent e)

{

if(e.getSource() == confirmButton)

{

if(record.equalsIgnoreCase(verificationText.getText()))

{

Storeroom date= newStoreroom();

date.setUser(userText.getText());

date.setPassword(passwordText.getText());

if(StoreroomManager.searchDate(date))

frame.dispose();

else

{

StoreroomManager.datelist.add(date);

frame.dispose();

}

}

else

{

JOptionPane.showMessageDialog(null, "驗(yàn)證碼錯誤,請重新輸入");

verificationText.setText("");

passwordText.setText("");

//創(chuàng)建一個隨機(jī)字符串

String result= "";

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

{

intintVal= (int)(Math.random() * 26 + 97);

result= result+ (char)intVal;

}

record= result;

verificationShowLabel.setText(result);

}

}

}

};

confirmButton.addActionListener(ourListener1);

// 創(chuàng)建取消按鈕

JButton cancelButton= newJButton("取消");

cancelButton.setBounds(100, 120, 80, 25);

panel.add(cancelButton);

//對按鈕進(jìn)行監(jiān)視

ActionListener ourListener2= newActionListener()

{

publicvoidactionPerformed(ActionEvent e)

{

if(e.getSource() == cancelButton)

{

JOptionPane.showMessageDialog(null, "are you kidding me?!");

frame.dispose();

}

}

};

cancelButton.addActionListener(ourListener2);

// 創(chuàng)建刷新按鈕

JButton refreshButton= newJButton("刷新");

refreshButton.setBounds(250, 80, 80, 25);

panel.add(refreshButton);

//對按鈕進(jìn)行監(jiān)視

ActionListener ourListener3= newActionListener()

{

publicvoidactionPerformed(ActionEvent e)

{

if(e.getSource() == refreshButton)

{

passwordText.setText("");

verificationText.setText("");

//創(chuàng)建一個隨機(jī)字符串

String result= "";

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

{

intintVal= (int)(Math.random() * 26 + 97);

result= result+ (char)intVal;

}

record= result;

verificationShowLabel.setText(result);

}

}

};

refreshButton.addActionListener(ourListener3);

}

}

四.結(jié)果截圖

總結(jié)

以上是生活随笔為你收集整理的创建登录界面及简易验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。