6

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

这是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>

这是CSS部分:

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

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

4

2 回答 2

17

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

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

这将影响所有 菜单控件。


By the way, there was a particular build of Scene Builder that might come of interest - b42, this had an additional CSS menu 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).

于 2012-06-16T14:53:19.130 回答
0

I'm not sure, but you set the id attribute - doesn't that mean you can only access them via #modBar or #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.

于 2012-06-18T08:24:04.040 回答