JavaFX|改变窗口背景颜色的标签

问题描述

我正在尝试为我的应用程序编写两个 CSS 样式表(浅色主题、深色主题)。我有一个现有的样式表,我在其中声明了全局字体,在那之后,我设置了初始主题。基本上就是这个样子。

 primaryStage.initStyle(StageStyle.TRANSPARENT);
 Scene scene = new Scene(root,414,896);
        scene.getStylesheets().add(getClass().getResource("css/font/globalFont.css").toExternalForm()); //sets the font
        scene.getStylesheets().add((getClass().getResource("css/theme/dark.css").toExternalForm()));
        scene.setFill(Color.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();

globalFont.css 看起来像:

@font-face {
    font-family: "Inter V";;
    src: url("Inter.ttf");
}

.root{
    -fx-font-family: "Inter V";
}

和dark.css如下:

.root{
    -fx-text-color: #EDEEC0;
}

.label{
    -fx-text-fill: -fx-text-color;
}

我的问题是,因为我有一个透明的舞台和透明的背景,在那里添加一个标签会改变我窗口的背景。看看 -

Before adding label

After adding label

我只想更改标签的颜色并保持透明背景。我尝试使用 Text 而不是 Label 但颜色不受影响。

fxml 文件如下所示:

<StackPane prefheight="896.0" prefWidth="414.0" stylesheets="@css/theme/general.css" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.rtkay.App">
   <children>
      <borderpane prefheight="200.0" prefWidth="200.0">
         <top>
            <HBox prefheight="0.0" prefWidth="414.0" spacing="24.0" borderpane.alignment="CENTER">
               <children>
                  <FontIcon iconLiteral="fltrmz-options-24" iconSize="24">
                     <cursor>
                        <Cursor fx:constant="HAND" />
                     </cursor>
                  </FontIcon>
                  <FontIcon iconLiteral="fltral-badge-24" iconSize="24">
                     <cursor>
                        <Cursor fx:constant="HAND" />
                     </cursor>
                  </FontIcon>
                  <HBox alignment="TOP_RIGHT" prefheight="68.0" prefWidth="303.0" spacing="24.0">
                     <children>
                        <FontIcon iconLiteral="fltral-dark-theme-24" iconSize="24">
                           <cursor>
                              <Cursor fx:constant="HAND" />
                           </cursor>
                        </FontIcon>
                        <FontIcon iconLiteral="fltral-broad-activity-Feed-24" iconSize="24">
                           <cursor>
                              <Cursor fx:constant="HAND" />
                           </cursor>
                        </FontIcon>
                     </children>
                  </HBox>
               </children>
               <padding>
                  <Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
               </padding>
            </HBox>
         </top>
      </borderpane>
   </children>
</StackPane>

general.css一个类来修改标签的大小(工作得很好)。

也在dark.css中,为什么我必须写出.label类?为什么 -fx-text-fill 中的 .root 没有被继承到每个组件?

编辑:我让它与 Text 而不是 Label 一起工作。我使用了错误的类名。 .Text 而不是 .text。但是,如果有人对此有答案,我仍然想知道为什么 Label 也会更改窗口颜色。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)