是否可以在同一帧中打开一个新的fxml文件

0 java javafx javafx-2

我已经在锚窗格中创建了fxml文件,但每次我点击按钮我都会在新框架中获得下一个fxml我希望它在同一框架中打开

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

jew*_*sea 7

只需创建一个单独的Stage,当您想用新的fxml替换舞台的内容时,将新的fxml加载到一个新的Scene并调用stage.setScene.

这是一个戏剧比喻 - 想象你正在看一场戏 - 它是罗密欧与朱丽叶,窗帘升起,你看到第一个场景(维罗纳的一个广场有一个喷泉).后来,窗帘降下来,小男人跑来跑去改变东西,窗帘升起,你看到一个新的场景(通往朱丽叶卧室的阳台).场景已经改变,但舞台没有 - 只有一个舞台和多个场景.