包含的 FXML 不适合父级

Included FXML does not fit parent correctly

我在一个主 FXML 视图中包括了几个 FXML,但问题是当我更改父级的高度时,它是一个 AnchorPane 包含的视图高度没有't change 而且我没有在文档中找到任何关于包含 FXML 大小的内容。有人知道吗?

<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
    <children>
        <fx:include fx:id="child" source="child.fxml" />
    </children>
</AnchorPane>

AnchorPane 将尝试根据您在这些 child 节点上设置的约束来调整其 children 的大小。来自 documentation:

The application sets anchor constraints on each child to configure the anchors on one or more sides. If a child is anchored on opposite sides (and is resizable), the anchor pane will resize it to maintain both offsets, otherwise the anchor pane will resize it to its preferred size. If in the former case (anchored on opposite sides) and the child is not resizable, then only the top/left anchor will be honored.

在您发布的代码中,您没有对 child 节点设置任何约束,因此它的大小只会调整到其首选大小。

例如,如果您希望 child 伸展以填充整个 AnchorPane,那么您可以这样做

<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
    <children>
        <fx:include fx:id="child" source="child.fxml" AnchorPane.topAnchor="0.0"
             AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
             AnchorPane.buttonAnchor="0.0" />
    </children>
</AnchorPane>

请注意,这还假设由 child.fxml 生成的任何节点都允许增长到 AnchorPane 的大小(例如,它是 maxWidthmaxHeight 属性具有适当的值),并且当 window 调整大小时 AnchorPane 也会增长,等等。您可能还想考虑 AnchorPane 是否是最佳选择,或者如果 some other layout 可能会更好。

简而言之,在布局方面使用 <fx:include> 没有什么特别之处:它的行为与任何其他场景图层次结构一样。