简体   繁体   English

如何在Javafx中创建此菜单按钮?

[英]How can I create this menu button in Javafx?

该图显示了我想在项目中实现的菜单按钮

I'm working on a college project, and a I would like to create menu button to allow the users to decide whether to search by id or search by date, but I cannot find out how, or if it's even possible. 我正在一个大学项目上,我想创建一个菜单按钮,以允许用户决定是按ID进行搜索还是按日期进行搜索,但我无法确定如何进行搜索,甚至无法实现。

I would really appreciate if you could tell me if it is possible to create such a button as displayed in the image within the black circle, and how. 如果您能告诉我是否有可能创建一个如黑色圆圈中图像中所示的按钮,以及如何创建,将不胜感激。

Thanks in advance. 提前致谢。

you can use the following code: 您可以使用以下代码:

        CheckMenuItem checkMenuItem1 = new CheckMenuItem("ID Search");
        CheckMenuItem checkMenuItem2 = new CheckMenuItem("Date Search");
        MenuButton menuButton = new MenuButton();
        menuButton.setGraphic(new ImageView(new Image(getClass().getResource("path.fileExtension").toExternalForm())));
        menuButton.getItems().addAll(checkMenuItem1,checkMenuItem2);

Alternatively, you can use CustomMenuItem : 另外,您可以使用CustomMenuItem

        CustomMenuItem idItem = new CustomMenuItem(new CheckBox("ID Search"));
        CustomMenuItem dateItem = new CustomMenuItem(new CheckBox("Date Search"));
        MenuButton menuButton = new MenuButton();
        menuButton.setGraphic(new ImageView(new Image(getClass().getResource("path.fileExtension").toExternalForm())));
        menuButton.getItems().addAll(idItem,dateItem)

Update: If The 2 search types should be mutually exclusive, you can replace CheckMenuItem by RadioMenuItem : 更新:如果2个搜索类型应该互斥,则可以用CheckMenuItem替换RadioMenuItem

        RadioMenuItem radioMenuItem1 = new RadioMenuItem("ID Search");
        RadioMenuItem radioMenuItem2 = new RadioMenuItem("Date Search");
        ToggleGroup toggleGroup = new ToggleGroup();
        toggleGroup.getToggles().addAll(radioMenuItem1, radioMenuItem2);  

You can create a simple menu, create on your own the icon (cutting that from the screenshot for example) and adding it to your menu. 您可以创建一个简单的菜单,自行创建图标(例如,从屏幕截图中删除图标),然后将其添加到菜单中。 I think it works. 我认为它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM