简体   繁体   English

满足条件时不显示 JOptionPane

[英]Doesn't show the JOptionPane when a condition is met

Im trying to make a Login Gui where it searches a text file for the password and username and if it doesn't find any that match the input then they display a JOptionPane but for some reason it never shows it.我试图制作一个登录 Gui,它在文本文件中搜索密码和用户名,如果找不到与输入匹配的任何内容,则它们会显示JOptionPane但由于某种原因它从未显示过。 This is the code:这是代码:

int determine=0;
try{
    String p1;
    String p2;
    String password =PasswordEntered.getText();
    String username =UsernameEntered.getText();
    FileReader reader=new FileReader("LoginPages.txt");
    FileReader read=new FileReader("LoginPages.txt");
    BufferedReader cr=new BufferedReader(read);
    BufferedReader br=new BufferedReader(reader);
   
    ArrayList<String> numbs = new ArrayList<String>();
    while (cr.readLine()!=null){
        numbs.add(br.readLine());
    }
    for (int i=0;i<=numbs.size();i=i+2){
        p1=numbs.get(i);
        p2=numbs.get(i+1);
        if(password.equals(p2)&&(username.equals(p1))) {
            determine=i;
            MainGUI x=new MainGUI();
            x.setVisible(true);
            dispose();
        } 
    }
    if (determine==numbs.size()) {
        JOptionPane.showMessageDialog(this, "try again");
    }
} catch(IOException ex){
    JOptionPane.showMessageDialog(this, "File Not Found");
}

It was supposed to pop up saying "try again" but instead it doesn't do anything.它应该弹出说“再试一次”,但它什么也没做。 Please help or explain anyone.请帮助或解释任何人。

The file is found, otherwise a JOptionPane would have shown up.该文件已找到,否则将显示 JOptionPane。 You can test that by renaming the file and running the code again.您可以通过重命名文件并再次运行代码来测试它。

After that, the condition to display a JOptionPane would be determine==numbs.size() , but determine is initialized to zero and only set on successful login to the index at which it was found.之后,显示 JOptionPane 的条件将是determine==numbs.size() ,但determine被初始化为零,并且仅在成功登录到找到它的索引时设置。 Even if the last entry (pair of user/password) were found, it would match with i=size-2, so the condition never applies.即使找到最后一个条目(用户/密码对),它也会与 i=size-2 匹配,因此条件永远不会适用。

Change your code into something like将您的代码更改为类似

boolean found = false;
for (int i=0;i<=numbs.size();i=i+2){
    p1=numbs.get(i);
    p2=numbs.get(i+1);
    if(password.equals(p2)&&(username.equals(p1))) {
        found = true;
        break;
    } 
}
if (found) {
    MainGUI x=new MainGUI();
    x.setVisible(true);
    dispose();
} else {
    JOptionPane.showMessageDialog(this, "try again");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这个程序中,为什么JOptionPane满足条件时不显示? - In This Program, Why Doesn't A JOptionPane Show Up When Its Conditions Have Been Met? 即使符合条件,我的While循环也不会结束 - My While loop doesn't end even when the condition is met 异常不起作用(不显示JOptionPane消息) - Exception is not working (doesn't show the JOptionPane message) JOptionPane.showMessageDialog(null,ExNull)部署到Weblogic服务器时不显示 - JOptionPane.showMessageDialog(null,ExNull) doesn't show when deployed to Weblogic server 当满足while条件时,为什么第一次do while循环没有结束? - Why doesn't the first do while loop end when the while condition is met? 如果不满足条件? - If condition isn't met? 不满足条件时,while循环不会中断 - While loop not breaking when its condition isn't met Spring 使用动态 URL 启动重新加载页面 Controller @PostMapping 当@Valid 输入约束不满足时,在网页上显示错误 - Spring Boot reloading page with dynamic URL in the Controller @PostMapping when @Valid constraints of input doesn't met, to show errors on webpage 直到下一个JOptionPane出现,JOptionPane才会消失 - JOptionPane doesn't disappear until next JOptionPane appears 读取InputStream后,JOptionPane.showMessageDialog不显示。 【JAVA] - JOptionPane.showMessageDialog doesn't show up after reading an InputStream. [Java]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM