一.程序設計思路: 1.顯示登錄,注冊頁面,以及主頁面
2.可以在表里存儲數據,即注冊用戶
3.可以在圖形框中輸入數據
4.在后端檢索所有數據,查看是否存在數據
5.隨機生成驗證碼,並且要求輸入的驗證碼與顯示的對應
6.完成登錄
二. 流程圖
三.源代碼
/*本類將作為上網時的登錄頁面
需要實現的功能:
1.顯示登錄,注冊頁面,以及主頁面
2.可以在表里存儲數據,即注冊用戶
3.可以在圖形框中輸入數據
4.在后端檢索所有數據,查看是否存在數據
5.隨機生成驗證碼,並且要求輸入的驗證碼與顯示的對應
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()
{
// 創建 JFrame 實例
JFrame frame = new JFrame("請登錄:");
// 設置frame大小
frame.setLocation(500,300);
frame.setSize(400, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//創建面板
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
//根據需要設置面板的具體形式
placeComponents(panel);
// 設置界面可見
frame.setVisible(true);
}
public void placeComponents(JPanel panel)
{
//清空面板
panel.setLayout(null);
// 創建 用戶使用的組件
JLabel userLabel = new JLabel("用戶名:");
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
// 創建文本域用於用戶輸入
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
// 創建 密碼使用的組件
JLabel passwordLabel = new JLabel("密碼:");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
// 創建密碼的文本域
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
// 創建 驗證碼使用的組件
JLabel verificationLabel = new JLabel("驗證碼:");
verificationLabel.setBounds(10,80,80,25);
panel.add(verificationLabel);
// 創建驗證碼的文本域
JTextField verificationText = new JTextField(20);
verificationText.setBounds(100,80,80,25);
panel.add(verificationText);
//創建一個隨機字符串
String result = "";
for(int i = 0 ; i < 6 ; i ++)
{
int intVal = (int)(Math.random() * 26 + 97);
result = result + (char)intVal;
}
record = result;
// 創建驗證碼使用的顯示的組件
JLabel verificationShowLabel = new JLabel(result);
verificationShowLabel.setBounds(200,80,80,25);
panel.add(verificationShowLabel);
// 創建登錄按鈕
JButton loginButton = new JButton("登錄");
loginButton.setBounds(10, 120, 80, 25);
panel.add(loginButton);
//對按鈕進行監視
ActionListener ourListener1 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == loginButton) //判斷是否點擊登錄按鈕
{
if(record.equalsIgnoreCase(verificationText.getText()))//判斷驗證碼是否正確
{
if(StoreroomManager.datelist.size()==0)//判斷存儲空間是否有數據
{
JOptionPane.showMessageDialog(null, "你還沒有注冊");
userText.setText("");
passwordText.setText("");
}
else//對現有的date進行賦值,並且檢索是否存在注冊過的用戶
{
Storeroom date= new Storeroom();
date.setUser(userText.getText());//獲取用戶輸入的用戶名
date.setPassword(passwordText.getText());//獲取用戶輸入的密碼
StoreroomManager.confirmDate(date);
passwordText.setText("");
verificationText.setText("");
}
}
else // 驗證碼不正確
{
passwordText.setText("");
verificationText.setText("");
JOptionPane.showMessageDialog(null, "驗證碼錯誤,請重新輸入");
//創建一個隨機字符串
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);
// 創建注冊按鈕
JButton registerButton = new JButton("注冊");
registerButton.setBounds(100, 120, 80, 25);
panel.add(registerButton);
//對按鈕進行監視
ActionListener ourListener2 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == registerButton)
{
userText.setText("");
passwordText.setText("");
verificationText.setText("");
new RegisterInterface();
}
}
};
registerButton.addActionListener(ourListener2);
// 創建刷新按鈕
JButton refreshButton = new JButton("刷新");
refreshButton.setBounds(250, 80, 80, 25);
panel.add(refreshButton);
//對按鈕進行監視
ActionListener ourListener3 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == refreshButton)
{
passwordText.setText("");
verificationText.setText("");
//創建一個隨機字符串
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);
}
}
//本文件相當於數據庫,用於存儲數據
package class3Login;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
//作為一個類,用來作為存儲數據的單元
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 //對數據進行操作的類
{
static List<Storeroom> datelist = new ArrayList<Storeroom>(0);
static void confirmDate(Storeroom date)//登錄,遍歷數據
{
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("輸入的數據:");
System.out.println("用戶名:"+date.getUser());
System.out.println("密碼:"+date.getpassword());
for(int i = 0;i<datelist.size();i++)
{
System.out.println("表中的"+(i+1)+"個數據:");
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)//登錄,遍歷數據
{
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類大致相同
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;
class RegisterInterface //作為注冊頁面
{
private static String record;
public RegisterInterface()
{
// 創建 JFrame 實例
JFrame frame = new JFrame("歡迎來到注冊頁面:");
// 設置frame大小
frame.setLocation(500,300);
frame.setSize(400, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//創建面板
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
//根據需要設置面板的具體形式
placeComponents(panel,frame);
// 設置界面可見
frame.setVisible(true);
}
public void placeComponents(JPanel panel,JFrame frame)
{
//清空面板
panel.setLayout(null);
// 創建 用戶使用的組件
JLabel userLabel = new JLabel("用戶名:");
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
// 創建文本域用於用戶輸入
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
// 創建密碼使用的組件
JLabel passwordLabel = new JLabel("密碼:");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
// 創建密碼的文本域
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
// 創建 驗證碼使用的組件
JLabel verificationLabel = new JLabel("驗證碼:");
verificationLabel.setBounds(10,80,80,25);
panel.add(verificationLabel);
// 創建驗證碼的文本域
JTextField verificationText = new JTextField(20);
verificationText.setBounds(100,80,80,25);
panel.add(verificationText);
//創建一個隨機字符串
String result = "";
for(int i = 0 ; i < 6 ; i ++)
{
int intVal = (int)(Math.random() * 26 + 97);
result = result + (char)intVal;
}
record = result;
// 創建驗證碼使用的顯示的組件
JLabel verificationShowLabel = new JLabel(result);
verificationShowLabel.setBounds(200,80,80,25);
panel.add(verificationShowLabel);
// 創建確認按鈕
JButton confirmButton = new JButton("確定");
confirmButton.setBounds(10, 120, 80, 25);
panel.add(confirmButton);
//對按鈕進行監視(確認,即注冊按鈕)
ActionListener ourListener1 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == confirmButton)
{
if(record.equalsIgnoreCase(verificationText.getText()))
{
Storeroom date = new Storeroom();
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, "驗證碼錯誤,請重新輸入");
verificationText.setText("");
passwordText.setText("");
//創建一個隨機字符串
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);
}
}
}
};
confirmButton.addActionListener(ourListener1);
// 創建取消按鈕
JButton cancelButton = new JButton("取消");
cancelButton.setBounds(100, 120, 80, 25);
panel.add(cancelButton);
//對按鈕進行監視
ActionListener ourListener2 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == cancelButton)
{
JOptionPane.showMessageDialog(null, "are you kidding me?!");
frame.dispose();
}
}
};
cancelButton.addActionListener(ourListener2);
// 創建刷新按鈕
JButton refreshButton = new JButton("刷新");
refreshButton.setBounds(250, 80, 80, 25);
panel.add(refreshButton);
//對按鈕進行監視
ActionListener ourListener3 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == refreshButton)
{
passwordText.setText("");
verificationText.setText("");
//創建一個隨機字符串
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);
}
}
四. 結果截圖