java JavaFX 下拉按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39163881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX drop down button
提问by user3164187
How can I create a "Drop down button" in JavaFX?
如何在 JavaFX 中创建“下拉按钮”?
So far i was using a ChoiceBox
for the purpose , now that I have to assign an image to the ChoiceBox
,
到目前为止,我一直在使用 a ChoiceBox
,现在我必须将图像分配给ChoiceBox
,
ChoiceBox.setGraphic() // is not available as like
Buttons
ChoiceBox.setGraphic() // 不可用
Buttons
So I am planning to change it to a drop down button. So that i can set an icon to it.
所以我打算把它改成一个下拉按钮。这样我就可以为它设置一个图标。
Am using SceneBuilder
for designing UI.
我使用SceneBuilder
的设计UI。
No help in searching how to create a drop down button using JavaFX.
搜索如何使用 JavaFX 创建下拉按钮没有帮助。
回答by DVarga
If you do not have to store the selection like in a ChoiceBox
you can use for example the MenuButton
control.
如果您不必像在 a 中那样存储选择,则ChoiceBox
可以使用例如MenuButton
控件。
MenuButton is a button which, when clicked or pressed, will show a ContextMenu.
MenuButton 是一个按钮,当单击或按下时,将显示 ContextMenu。
A MenuButton
has the graphicProperty
mentioned by you as its base class is Labeled
.
AMenuButton
具有graphicProperty
您提到的作为其基类的Labeled
.
Simple example
简单的例子
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"));
This will produce a button like:
这将产生一个按钮,如:
Note:If you need the button also to act like a real button, you can switch from MenuButton
to SplitMenuButton
. The only difference is that the control field is splitted into a button part and a drop down part (so you can assign an action also for the header):
注意:如果您还需要该按钮充当真正的按钮,您可以从 切换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.
SplitMenuButton 与 MenuButton 一样,与从菜单中选择 MenuItem 的概念密切相关。与 MenuButton 不同,SplitMenuButton 分为两部分,“操作”区域和“菜单打开”区域。
如果用户在操作区域中单击,SplitMenuButton 的行为将类似于 Button,触发与 ButtonBase.onAction 属性关联的任何内容。