将VBox添加到菜单按钮JavaFX

将VBox添加到菜单按钮JavaFX,javafx,fxml,Javafx,Fxml,我在将VBox作为子属性插入菜单按钮时遇到问题,因为我必须在赋值中这样做。到目前为止,我已经得出了以下结论: 您正在尝试将VBox添加到菜单按钮,该按钮仅接受作为子菜单项的菜单项 您可以使用CustomMenuItem来解决这个问题。CustomMenuItem允许您添加任意节点作为其图形属性,包括一个VBox 所以你可以这样做: FXML: <?import javafx.scene.control.*?> <?import javafx.scene.layout.VBox

我在将VBox作为子属性插入菜单按钮时遇到问题,因为我必须在赋值中这样做。到目前为止,我已经得出了以下结论:


您正在尝试将
VBox
添加到
菜单按钮
,该按钮仅接受作为子菜单项的
菜单项

您可以使用
CustomMenuItem
来解决这个问题。
CustomMenuItem
允许您添加任意
节点
作为其
图形
属性,包括一个VBox

所以你可以这样做:

FXML:

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
      prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <MenuButton mnemonicParsing="false" text="MenuButton">
        <items>
            <CustomMenuItem mnemonicParsing="false" text="Unspecified Action">
                <graphic>
                    <VBox prefHeight="200.0" prefWidth="100.0" spacing="5.0">
                        <Button mnemonicParsing="false" text="Button"/>
                        <RadioButton mnemonicParsing="false" text="RadioButton"/>
                        <Button mnemonicParsing="false" text="Click Me"/>
                        <ComboBox prefWidth="150.0"/>
                        <Slider/>
                        <CheckBox text="Check Box"/>
                        <TextField/>
                    </VBox>
                </graphic>
            </CustomMenuItem>
        </items>
    </MenuButton>
</VBox>
    MenuButton menuButton = new MenuButton();

    VBox menuVbox = new VBox();
    menuVbox.getChildren().addAll(
            new Button("Button"),
            new RadioButton("RadioButton"),
            new Button("Click Me"),
            new ComboBox<>(),
            new Slider(),
            new CheckBox("CheckBox"),
            new TextField()
    );
    CustomMenuItem vboxMenuItem = new CustomMenuItem(menuVbox);

    menuButton.getItems().add(vboxMenuItem);

Java:

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
      prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <MenuButton mnemonicParsing="false" text="MenuButton">
        <items>
            <CustomMenuItem mnemonicParsing="false" text="Unspecified Action">
                <graphic>
                    <VBox prefHeight="200.0" prefWidth="100.0" spacing="5.0">
                        <Button mnemonicParsing="false" text="Button"/>
                        <RadioButton mnemonicParsing="false" text="RadioButton"/>
                        <Button mnemonicParsing="false" text="Click Me"/>
                        <ComboBox prefWidth="150.0"/>
                        <Slider/>
                        <CheckBox text="Check Box"/>
                        <TextField/>
                    </VBox>
                </graphic>
            </CustomMenuItem>
        </items>
    </MenuButton>
</VBox>
    MenuButton menuButton = new MenuButton();

    VBox menuVbox = new VBox();
    menuVbox.getChildren().addAll(
            new Button("Button"),
            new RadioButton("RadioButton"),
            new Button("Click Me"),
            new ComboBox<>(),
            new Slider(),
            new CheckBox("CheckBox"),
            new TextField()
    );
    CustomMenuItem vboxMenuItem = new CustomMenuItem(menuVbox);

    menuButton.getItems().add(vboxMenuItem);
MenuButton MenuButton=new MenuButton();
VBox菜单框=新的VBox();
menuVbox.getChildren().addAll(
新按钮(“按钮”),
新RadioButton(“RadioButton”),
新建按钮(“单击我”),
新建组合框(),
新滑块(),
新复选框(“复选框”),
新文本字段()
);
CustomMenuItem vboxMenuItem=新的CustomMenuItem(菜单框);
menuButton.getItems().add(vboxMenuItem);
另一方面,每当您看到一个
IllegalArgumentException
,您的第一站应该是相关类的JavaDocs(在本例中为
MenuItem


一个相当奇怪的聚会。您确定不应该添加多个
MenuItem
子类型,如
CheckMenuItem
ect吗?顺便说一句,如果节点不是
gridpane
的子节点,那么gridpane索引是没有用的。是的,这有点奇怪,但它的主要目标是将布局缩小到menubutton的menuitem中,这很有意义。因此,缩小时,它必须在菜单按钮中具有相同的布局。