CSS 如何设置菜单按钮和菜单项的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12299162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to style menu button and menu items
提问by Vinod CG
I tried to change styles in menu button. I could change menu button style but not its menu item. No matter what i try menu item inside menu-button remains unchanged.
我试图改变菜单按钮的样式。我可以更改菜单按钮样式,但不能更改其菜单项。无论我尝试什么菜单按钮内的菜单项都保持不变。
.menu-button {
-fx-background-color:black;
}
.menu-button .label {
-fx-background-color:black; }
Now how can i change color that is left out??
现在我怎样才能改变被遗漏的颜色?
回答by Uluk Biy
MenuButton
uses Menu
internally and has a similar API. In such way that MenuButton
contains MenuItem
s list just like Menu
. So I think you need to try to play with .menu
, .menu-button
and .menu-item
CSS selectors in caspian.css. More specifically with .menu-item
.
MenuButton
在Menu
内部使用并具有类似的 API。以这种方式MenuButton
包含MenuItem
s 列表就像Menu
. 所以我认为你需要尝试在 caspian.css 中使用.menu
,.menu-button
和.menu-item
CSS 选择器。更具体地说,与.menu-item
.
EDIT:It seems you need to change the .context-menu
too, because the popped up menu of the menuButton is ContextMenu.
编辑:似乎您也需要更改.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 */
}
回答by Markus Weninger
This has also been asked hereand here, so I decided to write a CSS template for styling menu bars.
这里和这里也有人问过这个问题,所以我决定写一个 CSS 模板来设置菜单栏的样式。
Using this CSS template is a very easy way to style a MenuBar
, its top-level MenuButton
entries, and each MenuButton
's MenuItem
children, i.e, "the whole menu bar".
使用这个 CSS 模板是一种非常简单的方式来设计 a MenuBar
、它的顶级MenuButton
条目和 eachMenuButton
的MenuItem
子元素,即“整个菜单栏”。
The only thing that has to be done is to adjust four variables according to one's needs:
唯一需要做的就是根据需要调整四个变量:
-fx-my-menu-color
: The menu bar's default background color (i.e., when item is not hovered / selected)-fx-my-menu-color-highlighted
: An item's background color if it is hovered / selected.-fx-my-menu-font-color
: The menu bar's default font color (i.e., when item is not hovered / selected)-fx-my-menu-font-color-highlighted
: An item's font color if it is hovered / selected.
-fx-my-menu-color
: 菜单栏的默认背景颜色(即,当项目未被悬停/选中时)-fx-my-menu-color-highlighted
: 悬停/选中的项目的背景颜色。-fx-my-menu-font-color
:菜单栏的默认字体颜色(即,当项目没有悬停/选择时)-fx-my-menu-font-color-highlighted
:如果悬停/选择项目的字体颜色。
The complete CSS file is commented to explain each defined rule:
完整的 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;
}