如何在 CSS 中设置 JavaFX 菜单及其项目的样式? [英] How can I style a JavaFX menu and its items in CSS?
问题描述
我有一个在 FXML 中设置如下的 MenuBar:
I've got a MenuBar that is setup as follows in FXML:
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="true" text="_File">
<items>
<MenuItem mnemonicParsing="true" text="_New Project"/>
<MenuItem mnemonicParsing="true" text="_Open…"/>
<MenuItem mnemonicParsing="false" text="Quit"/>
</items>
</Menu>
</menus>
</MenuBar>
这会产生如下菜单:
我已经使用以下 CSS 成功地设置了 MenuBar
和 Menu
File 的样式:
I've successfully styled the MenuBar
and the Menu
File with the following CSS:
.menu-bar { /* The menu bar itself */ }
.menu { /* The File menu item */ }
.menu:showing { /* menu when it's being shown (activated) */ }
.menu .label { /* Styles the text on a menu item */ }
.menu:showing .label { /* Styles the text on a menu item when activated */ }
但是,我一直无法为显示的菜单设置样式.
However, I've been unable to style the menu that is displayed.
我尝试将其视为 ContextMenu:
I've tried treating it as a ContextMenu:
.context-menu {
-fx-background-color: red;
}
什么都不做(它不是 ContextMenu,所以这里没什么大的惊喜).
Doesn't do anything (it's not a ContextMenu, so no big surprise here).
我尝试了样式 menu-item
和 menu-button
:
I've tried styling menu-item
and menu-button
:
.menu-button,
.menu-item {
-fx-background-color: red;
}
这会更改菜单(文件),但不会更改菜单项或显示的菜单.
This changes the menu (File), but not the menu items or the menu that is displayed.
我尝试选择一个名为 .items
的子结构,但它似乎不存在.
I've tried selecting a substructure called .items
but that doesn't seem to exist.
- 如何选择/设置菜单样式(包含新建项目、打开...、退出的容器)?
- 如何选择/设置菜单中每个单独的 MenuItem 的样式?
澄清
为了帮助阐明我希望设计哪些元素,我添加了这张图片,其中概述了我希望设计的组件:
Clarification
To help clarify which elements I'm looking to style, I've added this image which outlines the components I'm wishing to style:
推荐答案
我想你忘记了 .context-menu
中的 -fx-skin
属性.
按照如何设置菜单按钮和菜单项的样式.
I think you forgot the -fx-skin
property in .context-menu
.
Follow the How to style menu button and menu items.
这篇关于如何在 CSS 中设置 JavaFX 菜单及其项目的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!