Javafx 单击按钮显示新阶段时如何关闭当前阶段

Javafx 单击按钮显示新阶段时如何关闭当前阶段,javafx,stage,scene,Javafx,Stage,Scene,所以我正在开发一个密码管理应用程序,这是我第一次尝试用Java开发GUI。当用户单击登录按钮并仅显示主应用程序窗口时,我会让应用程序关闭登录窗口。每当我尝试这样做时,我的setPrevStage方法都会存储空值,而prevStage.close方法会导致程序因NullPointerException而崩溃。我不确定我做错了什么,所以非常感谢您的建议。这是用于此操作的.java文件 package pwmanager; import java.io.IOException;

所以我正在开发一个密码管理应用程序,这是我第一次尝试用Java开发GUI。当用户单击登录按钮并仅显示主应用程序窗口时,我会让应用程序关闭登录窗口。每当我尝试这样做时,我的setPrevStage方法都会存储空值,而prevStage.close方法会导致程序因NullPointerException而崩溃。我不确定我做错了什么,所以非常感谢您的建议。这是用于此操作的.java文件

    package pwmanager;

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.event.EventType;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.GridPane;
    import javafx.stage.Stage;


    /**
     *
     * @author 176878
     */
    public class FXMLDocumentController implements Initializable {

    @FXML
    private Button loginButton;


    @FXML
    Stage prevStage;
    Stage currentStage;
    public void setPrevStage(Stage stage){
        prevStage = stage;
        if (stage == null){
            System.out.println("Stage NULL");
        }

       }

    @FXML
    public void getPrevStage(Stage stage){
        currentStage = prevStage;

    }

    @FXML
    public void loginButtonAction(ActionEvent event) throws IOException {
       System.out.println("You clicked me, logging in!");
        setPrevStage(prevStage);

        Stage stage = new Stage();


        FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));

        Parent mainScreen = (Parent)loader.load();
        Scene scene = new Scene(mainScreen);

        stage.setScene(scene);
        stage.setTitle("Password Manager");
        prevStage.close();
        stage.show();
    }    

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

我不明白你的问题。但我给出了我的解决方案,这里的代码可能会对你有所帮助

Stage st = (Stage) loginbutton.getScene().getWindow();
            st.hide();
            try 
                {
                  showFxml(filex);
                } 
            catch (Exception exp)
                {
                  System.out.println("file loading problem "+exp);
                }
下面是showFxml

public void showFxml(String filen)throws Exception
 {
   FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(""+filen+""));
   Parent root1 = (Parent) fxmlLoader.load();
   Stage stage = new Stage();
   stage.initModality(Modality.APPLICATION_MODAL);
   stage.setTitle("Password Manager");
   stage.setScene(new Scene(root1)); 
   stage.show();
 }

通过使用此选项,您可以隐藏上一阶段并将其置于当前阶段的前面。

如果在上一阶段中有旧阶段,请尝试:

@FXML
public void loginButtonAction(ActionEvent event) throws IOException {
   System.out.println("You clicked me, logging in!");

    FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));

    Parent mainScreen = (Parent)loader.load();
    Scene scene = new Scene(mainScreen);

    prevStage.setScene(scene);
    prevStage.setTitle("Password Manager");
    prevStage.show();
}  
如果不是。。。在Start.class中,您定义了静态阶段,并在此阶段中设置了primarystage。请阅读以下示例:

public class Start extends Application {

    static Stage stage;

    @Override
    public void start(Stage primarystage) {
        stage=primarystage;

        ...

         stage.show();
    }

}
现在的代码是:

@FXML
public void loginButtonAction(ActionEvent event) throws IOException {
   System.out.println("You clicked me, logging in!");

    prevStage=Start.stage;   // Start.class

    FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));

    Parent mainScreen = (Parent)loader.load();
    Scene scene = new Scene(mainScreen);

    prevStage.setScene(scene);
    prevStage.setTitle("Password Manager");
    prevStage.show();
}    

前一阶段结束;程序的第行导致程序崩溃,出现NullPointerException错误,因此我的getPrevStage方法由于某种原因没有在prevStage变量中存储对象,因此当prevStage.close;调用时,程序崩溃,因为没有要关闭的对象/阶段。我仔细阅读了你的代码,但它并没有真正帮助我解决我的具体问题。