Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        5年前关闭。
                    
                
        

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

最佳答案

在构造函数中使用CustomMenuItemsetHideOnClick和CheckBox。



编辑:

我只是注意到JavaFX 8u40中将其弄乱了。菜单项的文本颜色与背景颜色相同,因此看不到任何文本。

一个快速的解决方法是设置文本样式,例如。 G。

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


这是一个完整的示例:

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);
    }
}

09-04 06:43