is it possible to open a new fxml file in the same

2020-04-30 16:37发布

问题:

i've created fxml files in anchor pane but everytime i click on the button i get the next fxml in a new frame i want it to open in the same frame

public void baropen(ActionEvent event) {
    // handle the event here
    BorderPane bp = new BorderPane();
    bp.setPadding(new Insets(10, 50, 50, 50));

    Stage stage = new Stage();
    Scene scene ;
    // scene= new Scene(root);
    scene = new Scene(bp);
    stage.setScene(scene);
    stage.show(); 
    try {
        new RecBar().start(stage);
    } catch (Exception ex) {
        Logger.getLogger(RecController.class.getName()).log(Level.SEVERE, null,ex);
    }
}

回答1:

Just create a single Stage and when you want to replace the content of the stage with a new fxml, load the new fxml into a new Scene and call stage.setScene.

It's a theater metaphor - imagine you are watching a play - it's Romeo and Juliet, the curtain raises and you see the first scene (a plaza in Verona with a fountain). Later on, the curtain lowers, little men run around and change stuff, the curtain rises and you see a new scene (the balcony to Juliet's bedroom). The scene has changed, but the stage has not - there is just one stage and multiple scenes.