比较来自JFrame表单JAVA的用户输入和初始化数组

时间:2016-04-18 10:36:14

标签: java arrays jframe

我正在使用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);
      }
  }
}    

我在LOGIN按钮中进行编码。

感谢。

1 个答案:

答案 0 :(得分:2)

我不明白你的嵌套循环。像这样:

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;
}

当然,您将普通密码存储在内存中,但我认为此应用程序不需要出色的安全性......