java 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";
}
黑暗。css如下所示:
.root{
-fx-text-color: #EDEEC0;
}
.label{
-fx-text-fill: -fx-text-color;
}
我的问题是,因为我有一个透明的舞台和透明的背景,在那里添加标签会改变我窗口的背景。看看——
我只想改变标签的颜色,保持透明的背景。我试着用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
课?为什么.root
中的-fx-text-fill
不能继承到每个组件
编辑:我让它与Text
而不是Label
一起工作。我用错了类名.Text
而不是.text
。然而,我仍然想知道为什么Label
也会改变窗口的颜色,如果有人对此有答案的话
共 (0) 个答案