FXML 中的拆分菜单按钮

SplitMenuButton in FXML

我想使用 FXML 创建一个 SplitMenuButton。我可以在 java 中找到有关如何执行此操作的文档,但在 xml 文件中找不到。我该怎么做?

此外,如果有 FXML 文档的良好来源,请指出。

你可以做到

<SplitMenuButton fx:id="smButton" text="Text">
    <items>
        <MenuItem text="Choice 1" onAction="#action1" />
        <MenuItem text="Choice 2" onAction="#action2" />
    </items>
</SplitMenuButton>

有一个"Introduction to FXML" document that describes how FXML works in general. However, for use cases such as this, you really just need the Javadocs。以大写字母开头的元素对应于 class 个名称,即它们是实例化 class 的指令。属性对应属性,所以

<SplitMenuButton fx:id="smButton" text="Text"/>

本质上是指

SplitMenuButton smButton = new SplitMenuButton();
smButton.setText("Text");

这里唯一棘手的是 <items> 元素,它是上述 FXML 简介中描述的 Read Only List Property