我的JFrame和JDialog登录系统的错误行为

时间:2019-03-15 19:52:23

标签: java jframe joptionpane jdialog

我已经实现了一个基本的登录系统,该系统可以拆分文本文件(名字:姓氏:城市:用户名:密码)

  

Harold:Fisher:旧金山:hf45:1234

当用户输入他/她的用户凭据时,如果正确输入了凭据,它将显示一个JFrame,否则将显示JOptionPane而不是通知用户输入的凭据不正确。

这是我的代码:

onDestroy

我现在面临的问题是,当用户正确输入其凭据时,两者都会显示JFrame和JOptionPane。另外,当用户输入错误的凭据时,JFrame不会显示(按预期),但是JOptionPane会出现两次,而不是一次。

1 个答案:

答案 0 :(得分:1)

我观察到您解释的UserDetails.txt文件有两行时的行为。例如

  

Harold:Fisher:旧金山:hf45:1234
  约翰:斯诺:旧金山:js45:5678

在上面的文件有两行的情况下,在“有问题的程序”下面输出您所解释的行为(打印“显示JFrame”和“显示对话框”)。

问题在于,在while循环中,您尝试为文件中的每一行显示JFrame或对话框。

在“固定程序”下尝试。这样,我使用了boolean变量matched来存储是否找到匹配项。然后在JFrame循环之后显示while或对话框。

出现问题的程序

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LoginFrame
{
  // Hardcode user entered username and password to simplify the program
  String username = "hf45";
  String password = "1234";

  public static void main(String[] args)
  {
    new LoginFrame().verifyLogin();
  }

  public void verifyLogin()
  {
    try {
      File f = new File("UserDetails.txt");
      Scanner fileRead = new Scanner(f);

      while(fileRead.hasNextLine())
      {
        String textLine = fileRead.nextLine();
        String[] userDetails = textLine.split(" : ");
        String tempUsername = userDetails[3];
        String tempPassword = userDetails[4];

        if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim()))
        {
          //new LibraryCatalogFrame().setVisible(true);
          System.out.println("Show JFrame");
        }
        else
        {
          System.out.println("Show dialog");
          //JOptionPanes.messageBox("Please re-enter your user details", "Incorrect Username or Password");
        }
      }
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
      //JOptionPanes.messageBox("Error", "FileNotFound");
    }
  }
}

输出:
显示JFrame
显示对话框

固定程序:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LoginFrame
{
  // Hardcode user entered username and password to simplify the program
  String username = "hf45";
  String password = "1234";

  public static void main(String[] args)
  {
    new LoginFrame().verifyLogin();
  }

  public void verifyLogin()
  {
    try {
      File f = new File("UserDetails.txt");
      Scanner fileRead = new Scanner(f);

      boolean matched = false;
      while(fileRead.hasNextLine())
      {
        String textLine = fileRead.nextLine();
        String[] userDetails = textLine.split(" : ");
        String tempUsername = userDetails[3];
        String tempPassword = userDetails[4];

        if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim()))
        {
          matched = true;
          break;
        }
      }

      if (matched)
      {
        //new LibraryCatalogFrame().setVisible(true);
        System.out.println("Show JFrame");
      }
      else
      {
        System.out.println("Show dialog");
        //JOptionPanes.messageBox("Please re-enter your user details", "Incorrect Username or Password");
      }
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
      //JOptionPanes.messageBox("Error", "FileNotFound");
    }
  }
}