我在将VBox作为子属性插入MenuButton时遇到问题,因为我必须在分配中执行此操作。到目前为止,我已经提出了这个建议:
<MenuButton fx:id="mb" mnemonicParsing="false" text="" GridPane.columnIndex="1" GridPane.rowIndex="3">
<items>
<VBox prefHeight="200.0" prefWidth="100.0" >
<Button text="Button" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<RadioButton text="RadioButton" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Button text="Click Me" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<ComboBox GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Slider GridPane.columnIndex="0" GridPane.rowIndex="2" />
<CheckBox text="CheckBox" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField prefWidth="200.0" GridPane.columnIndex="0" GridPane.rowIndex="3" />
</VBox>
</items>
</MenuButton>
但是我收到以下错误:
Caused by: java.lang.IllegalArgumentException: Unable to coerce VBox@13f6e35e to class javafx.scene.control.MenuItem.
有人可以澄清我的错误或帮助我解决它吗?感谢您的帮助。
答案 0 :(得分:7)
您正在尝试向VBox
添加MenuButton
,而MenuItem
仅接受CustomMenuItem
作为孩子。
您可以改用CustomMenuItem
来解决此问题。 Node
允许您添加任意graphic
作为其<?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>
属性,包括VBox。
所以您可以改为执行以下操作:
FXML:
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:
IllegalArgumentException
顺便提一句,只要您看到
MenuItem
,您的第一站应该是所讨论类的JavaDocs(在本例中为mergeMap()
)。