[英]Compare User Input and Initialized Array from JFrame Form JAVA
I was working on a login form using JAVA.我正在使用 JAVA 处理登录表单。 I get it to worked but no matter what I input, it will link to another JFrame and in the same time come out with the error message, no matter what I input is in the initialized array or not.我让它工作,但无论我输入什么,它都会链接到另一个 JFrame 并同时出现错误消息,无论我输入的内容是否在初始化数组中。 Is it my loop for checking is wrong?是不是我的检查循环错了? Hope someone can help, thanks!希望有人能帮忙,谢谢!
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);
}
}
}
I do the coding in the LOGIN button.我在登录按钮中进行编码。
Thanks.谢谢。
I don't understand your nested loops.我不明白你的嵌套循环。 Something like this:像这样的东西:
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;
}
Of course you are storing plain passwords in memory, but I assume this application doesn't need outstanding security...当然,您将普通密码存储在内存中,但我认为此应用程序不需要出色的安全性...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.