同一阶段,不同的Fxml - Javafx

时间:2014-06-05 09:01:35

标签: java javafx stage scene

我是JavaFX的新手。我有一个装有垂直分割窗格的窗口。在拆分页面的左侧,我有几个按钮。在每个按钮上单击我需要在拆分窗格的右侧加载单独的fxml。所以我在这里粘贴屏幕截图。

enter image description here

到目前为止,我在单独的舞台上打开,当搜索按钮被击中时,单独的场景。现在我需要在baselayout窗口的右侧加载Searcher。这是一些加载baseLayout的代码。

@Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Base Layout");
        BaseController.setMlTool(this);
        FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Base.fxml"));
        baseLayout = (AnchorPane) loader.load();
        Scene scene = new Scene(baseLayout);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

以下是一些按钮点击时加载Searcher的代码。

@FXML
    private void initialize(){
        System.out.println("Testing");
    }

    @FXML
    private void handleSearchButton(){
        System.out.println("Handle Button Called");

        Stage search = new Stage();
        FXMLLoader loader = new FXMLLoader(MLTool.class.getResource("view/Searcher.fxml"));
        search.setTitle("Searcher");
        try {
            AnchorPane searcherPage = (AnchorPane) loader.load();
            Scene scene = new Scene(searcherPage);
            search.setScene(scene);
            search.show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

此处如何在BaseLayout的分割窗格的右侧加载搜索器。 希望我的问题很明确。感谢您的期待。

3 个答案:

答案 0 :(得分:4)

您可以在右侧拍摄窗格,然后将fxml加载为该窗格的子节点。舞台始终相同,您可以在窗格中加载fxml。 例如:MainWindow.fxml是perent窗口。你可以在舞台上加载。

Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

现在在MainWindow.fxml中你有分割窗格。将anchorPan放在要在按钮单击时加载fxml的右侧。然后在anchorpane中添加fxml作为子项。

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxml));
Pane cmdPane = (Pane) fxmlLoader.load();
try {
anchCmdController.getChildren().clear();
anchCmdController.getChildren().add(cmdPane);
fadeIn.playFromStart();
} catch (Exception e) {
e.printStackTrace();
}

答案 1 :(得分:2)

在你的处理程序方法中试试这个:

首先声明anchorpane。在fxml右侧窗格中,将anchorpane与anchCmdController名称放在一起。

 @FXML
 private AnchorPane anchCmdController;

然后更新你的handleSearchButton事件。

@FXML
private void handleSearchButton() throws IOException {
System.out.println("Handle Button Called");

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("view/Searcher.fxml"));
Pane cmdPane = (Pane) fxmlLoader.load();
try {
/* NEED TO DECLARE ANCHOR PANE" */
anchCmdController.getChildren().clear();
anchCmdController.getChildren().add(cmdPane);
} catch (Exception e) {
e.printStackTrace();
}
}

答案 2 :(得分:1)

尝试收听按钮点击,点击后点击使用' FXMLLoader.load'根据根元素加载,类型转换为面板并指定右侧内容。