加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JavaFX下拉按钮

发布时间:2020-12-15 04:35:44 所属栏目:Java 来源:网络整理
导读:如何在 JavaFX中创建“下拉按钮”? 到目前为止,我正在使用ChoiceBox,现在我必须为ChoiceBox分配一个图像, ChoiceBox.setGraphic() // is not available as like Buttons 所以我打算把它改成一个下拉按钮.这样我就可以设置一个图标了. 我使用SceneBuilder设
如何在 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,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.唯一的区别是控制字段被分为按钮部分和下拉部分(因此您也可以分配操作为标题):

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读