使用 FXML/CSS 在 JavaFX 中更改菜单控件的文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11051936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 04:12:20  来源:igfitidea点击:

changing text color of a Menu control in JavaFX with FXML/CSS

cssmenujavafx-2fxml

提问by XXL

I want to change the text color of the Menucontrol in JavaFX. Currently, the background color of the whole Menu Baris set to white and the default text color for displaying Menu-s is also white, so I cannot see the actual control, therefore I want to set the text color of the Menu("File") to black. How do I do that?

我想更改JavaFX 中Menu控件的文本颜色。目前整个Menu Bar的背景色设置为白色,显示Menu-s的默认文字颜色也是白色,所以看不到实际的控件,所以想设置Menu的文字颜色("File ") 为黑色。我怎么做?

Here's the FXMLportion:

这是FXML部分:

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <MenuBar id="modBar" layoutX="176.0" layoutY="122.0" styleClass="modBar">
      <menus>
        <Menu id="modItem" mnemonicParsing="false" styleClass="modItem" text="File" />
      </menus>
      <stylesheets>
        <URL value="test.css" />
      </stylesheets>
    </MenuBar>
  </children>
</AnchorPane>

Here's the CSSpart:

这是CSS部分:

.modBar
{
    -fx-background-color: white;
}
.modItem
{
    -fx-color: black;
}

This doesn't work ("File" still remains white). What am I doing wrong? Also, another thing is that I cannot seem to apply anythingwith CSS to .modItem- it sort-of works in Scene Builder, but disappears once previewed (also the "Stylesheets" selector is missing on all Menu-s in SB).

这不起作用(“文件”仍然是白色的)。我究竟做错了什么?另外,另一件事是我似乎无法将任何CSS 应用到.modItem- 它在Scene Builder 中工作,但在预览后消失(SB 中的所有Menu-s上也缺少“样式表”选择器)。

回答by XXL

OK, think I have found the answer. What I did was extract caspian.cssout of jfxrt.jar(the default CSStheme JavaFXuses) and inspect everything related to Menu-s:

好的,我想我已经找到了答案。我所做的是从jfxrt.jarJavaFX使用的默认CSS主题)中提取caspian.css并检查与Menu-s相关的所有内容:

.menu .label
{
    -fx-text-fill: black;
}

This will influence allMenucontrols.

这将影响所有菜单控件。



By the way, there was a particular buildof Scene Builderthat might come of interest - b42, this had an additional CSSmenu that exposed internal styles of controls/elements, so customizing turns into a straightforward operation (without the need of prior manual extraction of the applied style).

顺便说一句,有一个特殊构建场景生成器可能出现利益- B42,这有一个额外的CSS菜单暴露内部风格控件/元素,所以定制变成了简单的操作(无需事先手工提取的的应用风格)。

回答by dzim

I'm not sure, but you set the id attribute - doesn't that mean you can only access them via #modBar or #modItem ???

我不确定,但是您设置了 id 属性 - 这是否意味着您只能通过 #modBar 或 #modItem 访问它们???

I'm also quite new to JFX2 (about a month) and unfortunatelly in all my years as a Java developer I never needed to play around with css, so it's just an assumption.

我对 JFX2 也很陌生(大约一个月),不幸的是,在我作为 Java 开发人员的这些年里,我从来不需要玩 css,所以这只是一个假设。