简体   繁体   English

JavaFX 使用 FXML 文件构建 borderPane

[英]JavaFX building borderPane with FXML files

Using a BorderPane layout, can you populate each part of it (top, left, center, right and bottom) using separate FXML files?使用BorderPane布局,您能否使用单独的 FXML 文件填充它的每个部分(顶部、左侧、中间、右侧和底部)?

So I would have a main.fxml like:所以我会有一个 main.fxml 像:

<BorderPane fx:controller="main.mainController"  xmlns:fx="http://javafx.com/fxml" >

    <top>
        reads from top.fxml
    </top>

    <left>
        reads from left.fxml
    </left>

    <center>
        reads from center.fxml
    </center>

    <right>
        reads from right.fxml
    </right>

    <bottom>
        reads from bottom.fxml
    </bottom>

</BorderPane>

There are 2 ways to do it:有两种方法可以做到:

add it in java在java中添加它

After you load the BorderPane , you can load other FXML files and put them into the BorderPane .加载BorderPane ,您可以加载其他 FXML 文件并将它们放入BorderPane

Eg例如

BorderPane root=FXMLLoader.load(this.getClass().getResource("root.fxml");//maybe this.getClass().getClassLoader().getResource("root.fxml"), depending on project structure
AnchorPane center=FXMLLoader.load(this.getClass().getResource("center.fxml");//maybe this.getClass().getClassLoader().getResource("center.fxml"), depending on project structure
root.setCenter(center);
stage.setScene(new Scene(root));

inside FXML内部 FXML

As @ Sedrick points out in the comments, you can also use fx:include :正如@ Sedrick在评论中指出的,您还可以使用fx:include

<center>
    <fx:include source="center.fxml"/>
</center>

In both options, it works the same with top , bottom , left and right .在这两个选项中,它的作用与topbottomleftright

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM