如何设置菜单按钮和菜单项的样式

时间:2012-09-06 11:34:30

标签: css javafx javafx-2 javafx-8 menuitem

我尝试在菜单按钮中更改样式。我可以更改菜单按钮样式,但不能更改其菜单项。 无论我尝试什么,菜单按钮内的菜单项都保持不变。

.menu-button {
 -fx-background-color:black;
}

.menu-button .label {
 -fx-background-color:black; }

Output looks like this

现在我怎样才能改变遗漏的颜色?

2 个答案:

答案 0 :(得分:23)

MenuButton在内部使用Menu,并且具有类似的API。这样MenuButton包含MenuItem列表就像Menu一样。所以我认为你需要尝试在caspian.css中使用.menu.menu-button.menu-item CSS选择器。更具体地说,.menu-item

编辑:您似乎也需要更改.context-menu,因为menuButton的弹出式菜单是ContextMenu。

.menu-item .label {
    -fx-text-fill: white;
}

.menu-item:focused {
     -fx-background-color: darkgray;
}

.menu-item:focused .label {
    -fx-text-fill: blue;
}

.context-menu {
    -fx-skin: "com.sun.javafx.scene.control.skin.ContextMenuSkin";
    -fx-background-color: black;
    -fx-background-insets: 0, 1, 2;
    -fx-background-radius: 0 6 6 6, 0 5 5 5, 0 4 4 4;
/*    -fx-padding: 0.666667em 0.083333em 0.666667em 0.083333em;  8 1 8 1 */
    -fx-padding: 0.333333em 0.083333em 0.666667em 0.083333em; /* 4 1 8 1 */
}

答案 1 :(得分:5)

这也被问到herehere,所以我决定为样式菜单栏编写一个CSS模板。

使用此CSS模板可以非常轻松地为MenuBar,其顶级MenuButton条目以及每个MenuButton MenuItem个孩子设置样式,即"整个菜单栏"。

唯一需要做的就是根据个人需要调整四个变量:

  • -fx-my-menu-color:菜单栏的默认背景颜色(即未悬停/选择项目时)
  • -fx-my-menu-color-highlighted:项目的背景颜色,如果它被悬停/选中。
  • -fx-my-menu-font-color:菜单栏的默认字体颜色(即未悬停/选择项目时)
  • -fx-my-menu-font-color-highlighted:项目的字体颜色,如果它被悬停/选中。

注释完整的CSS文件以解释每个定义的规则:

/* VARIABLE DEFINITIONS: Only these 4 variables have to be adjusted, the rest is copy-paste */
* {
    -fx-my-menu-color: #263238;                  /* Change according to your needs */
    -fx-my-menu-color-highlighted: #455a64;      /* Change according to your needs */
    -fx-my-menu-font-color: #FFFFFF;             /* Change according to your needs */
    -fx-my-menu-font-color-highlighted: #FFFFFF; /* Change according to your needs */
}

/* MENU BAR + Top-level MENU BUTTONS */
/*** The menu bar itself ***/    
.menu-bar {
    -fx-background-color: -fx-my-menu-color;
}

/*** Top-level menu itself (not selected / hovered) ***/
.menu-bar > .container > .menu-button {
    -fx-background-color: -fx-my-menu-color;
}

/*** Top-level menu's label (not selected / hovered) ***/
.menu-bar > .container > .menu-button > .label {
    -fx-text-fill: -fx-my-menu-font-color;
}

/*** Top-level menu's label (disabled) ***/
.menu-bar > .container > .menu-button > .label:disabled {
    -fx-opacity: 1.0;
}

/*** Top-level menu itself (selected / hovered) ***/
.menu-bar > .container > .menu-button:hover,
.menu-bar > .container > .menu-button:focused,
.menu-bar > .container > .menu-button:showing {
    -fx-background-color: -fx-my-menu-color-highlighted;
}

/*** Top-level menu's label (selected / hovered) ***/
.menu-bar > .container > .menu-button:hover > .label,
.menu-bar > .container > .menu-button:focused > .label,
.menu-bar > .container > .menu-button:showing > .label {
    -fx-text-fill: -fx-my-menu-font-color-highlighted;
}

/* MENU ITEM (children of a MENU BUTTON) */
/*** The item itself (not hovered / focused) ***/    
.menu-item {
    -fx-background-color: -fx-my-menu-color; 
}

/*** The item's label (not hovered / focused) ***/   
.menu-item .label {
    -fx-text-fill: -fx-my-menu-font-color;
}

/*** The item's label (disabled) ***/   
.menu-item .label:disabled {
    -fx-opacity: 1.0;
}    

/*** The item itself (hovered / focused) ***/    
.menu-item:focused, .menu-item:hovered {
    -fx-background-color: -fx-my-menu-color-highlighted; 
}

/*** The item's label (hovered / focused) ***/  
.menu-item:focused .label, .menu-item:hovered .label {
    -fx-text-fill: -fx-my-menu-font-color-highlighted;
}

/* CONTEXT MENU */
/*** The context menu that contains a menu's menu items ***/  
.context-menu {
    -fx-background-color: -fx-my-menu-color; 
}
相关问题