【问题标题】:JavaFX MenuItem without autohide [closed]没有自动隐藏的 JavaFX MenuItem [关闭]
【发布时间】:2015-04-14 08:40:43
【问题描述】:

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

【问题讨论】:

  • 这个问题“太宽泛”了???再说了,在它被搁置之前我已经回答过了。

标签: java user-interface javafx javafx-8


【解决方案1】:

在构造函数中使用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);
    }
}

【讨论】:

  • 耶,这很好用,除了 CheckBox 没有扩展到菜单的整个宽度(我还有一些其他长度不同的项目)。有没有办法填充菜单的宽度?
猜你喜欢
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多