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

Vin*_* CG 17 css javafx menuitem javafx-2 javafx-8

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

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

.menu-button .label {
 -fx-background-color:black; }
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样

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

Ulu*_*Biy 23

MenuButtonMenu内部使用并具有类似的API.以这种方式MenuButton包含MenuItems列表就像Menu.因此,我认为你需要尝试一起玩.menu,.menu-button.menu-item在caspian.css 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 */
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ger 7

这里这里也有人问这个问题,所以我决定写一个CSS模板来样式化菜单栏。

使用这个CSS模板是一个非常简单的方法,风格MenuBar,其顶级MenuButton条目,每个MenuButtonMenuItem儿童,即‘整个菜单栏’。

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

  • -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; 
}
Run Code Online (Sandbox Code Playgroud)