2015-04-14 36 views
0

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

+0

什么是 “过于宽泛” 这个问题???此外,我在答复之前已经回答了。 – Roland

回答

2

在构造函数中使用CustomMenuItem,setHideOnClick和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); 
    } 
} 
+0

Jeah,这个工程很好,除了CheckBox没有扩展到菜单的全部宽度(我有一些其他项目在不同的长度)。有没有办法填补菜单的宽度? –