JavaFX 使用 FXML 文件构建 borderPane

标签 java javafx fxml borderpane

使用 BorderPane 布局,您可以使用单独的 FXML 文件填充它的每个部分(顶部、左侧、中心、右侧和底部)吗?

所以我会有一个像这样的 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>

最佳答案

有两种方法可以做到这一点:

在java中添加

加载BorderPane后,您可以加载其他FXML文件并将它们放入BorderPane中。

例如

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));

FXML 内部

如@ Sedrick评论中指出,您还可以使用fx:include :

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

在这两个选项中,它与顶部底部的工作方式相同。

关于JavaFX 使用 FXML 文件构建 borderPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59844744/

相关文章:

java - 如何一次销毁运行时中的所有进程?

java - 如果使用 Azure APIM,应如何在 REST API 中添加授权?

java - 如何在JavaFX中将复选框绑定(bind)到变量?

java - 在JAVA中添加分数时如何避免舍入错误

java - 找到成对产品的最大和

javafx场景中的javafx线程异常

java - 在文本字段中按 Enter 键时 JVM 崩溃

带有类选择器的javaFX css id选择器不起作用

java - 在 JavaFX FXML 中启动最大化的应用程序窗口无法正常工作

javafx - 如何调整 Javafx 未装饰阶段的大小?