Java 只有在正确的情况下才能在其他框架中登录

Java login in other frame only if it is correct

你好,我有一个代码,我可以在其中注册帐户,然后让注册帐户输入我的代码以使用它,但现在使用 ActionPerformed,即使信息无效,代码也会打开我想要限制的新框架没有帐户。我不知道如何将我的 if e.getSource() 设置为仅在登录有效时才打开,没有其他问题提前感谢您的帮助

编辑:MathoQuest 框架在同一个文件夹中同时弹出两个我不知道为什么

代码如下:

    public void actionPerformed(ActionEvent e){
     if(e.getSource()==btsubmit){
         String uname=txtnamereg.getText();
         String passw=new String(txtpasswordreg.getPassword());
         if(!checkBlank(uname,passw, lblnamereg,lblpasswordreg)){
             if(!checkExist("init.txt",uname)){
                 passw=new String(encrypt(passw));
                 String accinfo=uname+"-"+passw;
                 saveToFile("init.txt",accinfo);
             }
         }
     }
     else if(e.getSource()==btlogin){
        String uname=txtname.getText();
        String passw=new String(txtpassword.getPassword());
        if(!checkBlank(uname,passw,lblname,lblpassword))
            validateUser("init.txt",uname,passw);     
     }
     if (e.getSource() == btlogin ) {
         MathoQuest math = new MathoQuest();
         math.getContentPane();
         math.setVisible(true);
     }
   }

如果你想看看它是如何验证的,我会把它包括在这里,这样你也可以有一个想法:

public void validateUser(String filename, String name, String password){
       FileReader fr;
       BufferedReader br;
       boolean valid=false;
       String accinfo;
       try{ 
           fr=new FileReader(filename);
           br=new BufferedReader(fr);
           while ((accinfo=br.readLine())!=null){

               if(check(accinfo,name,password)){

                   showMess("Login valide",lblmess);
                   valid=true;
                   break;                  
               }
           }

validateUser 修改为 return 一个布尔值,true 如果登录有效,false 否则。

然后根据return编辑的信息决定是否打开相框。

例如:

public boolean validateUser(String filename, String name, String password) {
    ...
    if(check(accinfo, name, password)) {
        return true;
    }
    ...
    return false
}

然后你可以这样称呼它:

boolean valid = validateUser("init.txt",uname,passw);
if(valid) {
     ...
}