gpt4 book ai didi

JavaFX 下拉按钮

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:14 26 4
gpt4 key购买 nike

如何在 JavaFX 中创建“下拉按钮”?

到目前为止,我一直在使用 ChoiceBox,现在我必须将图像分配给 ChoiceBox

ChoiceBox.setGraphic() // is not available as like Buttons

所以我打算将其更改为下拉按钮。这样我就可以为它设置一个图标。

我正在使用 SceneBuilder 设计 UI。

没有帮助搜索如何使用 JavaFX 创建下拉按钮。

最佳答案

如果您不必像在 ChoiceBox 中那样存储选择,您可以使用例如 MenuButton控制。

MenuButton is a button which, when clicked or pressed, will show a ContextMenu.

MenuButton 具有 graphicProperty你提到它的基类是 Labeled

简单示例

final Image image = new Image(getClass().getResource("cross_red.png").toExternalForm(), 20, 20, true, true);
MenuButton menuButton = new MenuButton("Don't touch this");
menuButton.setGraphic(new ImageView(image));
menuButton.getItems().addAll(new MenuItem("Really"), new MenuItem("Do not"));

这将产生一个如下所示的按钮:

enter image description here

注意:如果您需要按钮也像一个真正的按钮,您可以从 MenuButton 切换到 SplitMenuButton .唯一的区别是控制字段被分成按钮部分和下拉部分(因此您也可以为标题分配一个 Action ):

The SplitMenuButton, like the MenuButton is closely associated with the concept of selecting a MenuItem from a menu. Unlike MenuButton, the SplitMenuButton is broken into two pieces, the "action" area and the "menu open" area.

If the user clicks in the action area, the SplitMenuButton will act similarly to a Button, firing whatever is associated with the ButtonBase.onAction property.

关于JavaFX 下拉按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com