没有自动隐藏的JavaFX MenuItem [英] JavaFX MenuItem without autohide

查看:127
本文介绍了没有自动隐藏的JavaFX MenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个MenuItem(更具体的CheckMenuItem),它不会在点击时自动隐藏。我知道CustomMenuItem具有此功能,但它应该是CheckMenuItem。

I want a MenuItem (more specific a CheckMenuItem) which does not auto hide on click. I know that CustomMenuItem has this functionality, but it should be a CheckMenuItem.

推荐答案

使用 CustomMenuItem setHideOnClick 和构造函数中的CheckBox。

Use a CustomMenuItem, setHideOnClick and a CheckBox in the constructor.

编辑:

我刚注意到它在JavaFX 8u40中搞砸了。菜单项文本颜色与背景颜色相同,因此您看不到任何文本。

I just noticed that it's messed up in JavaFX 8u40. The menu item text color is the same as the background color, so you don't see any text.

快速解决此问题的方法是设置文本样式,e 。 g。

A quick workaround to this is to set the text style, e. g.

cb.setStyle("-fx-text-fill: -fx-text-base-color");

以下是一个完整的例子:

Here's a full example:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

            VBox root = new VBox();

             MenuBar menuBar = new MenuBar();

            final Menu menu = new Menu( "Items");

            for( int i=0; i < 10; i++) {

                CheckBox cb = new CheckBox( "Item " + i);

                // workaround: the color of the labels is wrong (white text on white background), we have to set it explicitly
                cb.setStyle("-fx-text-fill: -fx-text-base-color");

                CustomMenuItem cmi = new CustomMenuItem( cb);
                cmi.setHideOnClick(false);

                menu.getItems().add( cmi);

            }

            menu.getItems().add( new MenuItem( "This one doesn't stay open"));

            menuBar.getMenus().add( menu);


            root.getChildren().add( menuBar);

            Scene scene = new Scene(root,400,400);

            primaryStage.setScene(scene);
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于没有自动隐藏的JavaFX MenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆