簡體   English   中英

比較來自 JFrame 表單 JAVA 的用戶輸入和初始化數組

[英]Compare User Input and Initialized Array from JFrame Form JAVA

我正在使用 JAVA 處理登錄表單。 我讓它工作,但無論我輸入什么,它都會鏈接到另一個 JFrame 並同時出現錯誤消息,無論我輸入的內容是否在初始化數組中。 是不是我的檢查循環錯了? 希望有人能幫忙,謝謝!

import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class Login extends javax.swing.JFrame {
private String [] loginid = {"1001","1002","1003","1004","1005"};
private String [] password = {"abc1","abc2","abc3","abc4","abc5"};

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  for(int times=0;times<loginid.length;times++)
  {
      for(int count=0;count<password.length;count++){
      loginid[times]=jTextField1.getText();
      password[times]=jPasswordField1.getText();

      if(loginid[times]==loginid[count] && password[times]==password[count])
      {
          jTextField1.setText("");
          jPasswordField1.setText("");
          this.setVisible(false);
          new FrontPage().setVisible(true);
      }
      else
         JOptionPane.showMessageDialog(null, "Information invalid. Please try again.");
         this.setVisible(false);
      }
  }
}    

我在登錄按鈕中進行編碼。

謝謝。

我不明白你的嵌套循環。 像這樣的東西:

private int failedAttempts = 0;    

public void tryLogin() {
    final String username = jTextField1.getText();
    final String password = jPasswordField1.getText();

    // if (failedAttempts >= 3) ...

    if (loginOK(username, password)) {
        jTextField1.setText("");
        jPasswordField1.setText("");
        this.setVisible(false);
        new FrontPage().setVisible(true);
    }
    else {
        if (++failedAttempts >= 3) {
            JOptionPane.showMessageDialog(null, "Too many errors. bye.");
            System.exit(0); // or something
        }

        JOptionPane.showMessageDialog(null, "Information invalid. Please try again.");
        this.setVisible(false);
    }
}

public boolean isLoginOK(String username, String password) {
    for (int i=0; i<loginid.length; i++)
        if (username.equals(loginid[i]) && password.equals(password[i]))
            return true;

    return false;
}

當然,您將普通密碼存儲在內存中,但我認為此應用程序不需要出色的安全性...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM