简体   繁体   English

JavaFx:CSS 元素无法继承其 ID 的属性

[英]JavaFx: CSS element unable to inherit properties of it's ID

I have created an Options menu with JavaFx and styled it with CSS. I have used FXML for designing the options window. In the options window, I have created a label at the top and given it ID "title".我用 JavaFx 创建了一个选项菜单,并用 CSS 设计了它的样式。我使用 FXML 来设计选项 window。在选项 window 中,我在顶部创建了一个 label 并为其指定了 ID“title”。 Following is my FXML code and CSS code:以下是我的 FXML 代码和 CSS 代码:

*{
 -fx-font-size: 14px;
    font-size: 14px;
 }

 
#title{
    -fx-font-size: 20px;
    font-size: 20px;
    -fx-text-fill: #35568c;
}

FXML for Label: Label 的 FXML:

<Label id="title" alignment="CENTER" contentDisplay="CENTER" layoutX="80.0" layoutY="14.0" prefHeight="31.0" prefWidth="200.0" styleClass="big" text="Options">
         <font>
            <Font name="Bell MT Bold" size="20.0" />
         </font>
      </Label>

The problem with my output is that the label should inherit properties from #title part of CSS but instead, it is inheriting font-size from the * part and text-fill from #title part.我的 output 的问题是 label 应该从 CSS 的#title 部分继承属性,但相反,它从 * 部分继承字体大小,从 #title 部分继承文本填充。

Screenshot of output output截图

EDIT:编辑:

I changed my CSS code for #title to我将 #title 的 CSS 代码更改为

#title{
    -fx-font: 20 'Bell MT';
    font: 20 'Bell MT';
    -fx-text-fill: #35568c;
}

Now my code is working properly.现在我的代码可以正常工作了。 I changed -fx-font-size to -fx-font.我将 -fx-font-size 更改为 -fx-font。 But I still don't understand why font-size was not working.但我仍然不明白为什么 font-size 不起作用。

There is no need to set non-JavaFX CSS attributes like font rather than -fx-font , those non-JavaFX CSS attributes will just be ignored (or potentially cause issues).无需设置非 JavaFX CSS 属性,如font而不是-fx-font ,那些非 JavaFX CSS 属性将被忽略(或可能导致问题)。

If you set the font in the FXML (not recommended), then I think that would override the value you set in CSS usually.如果您在 FXML 中设置字体(不推荐),那么我认为这通常会覆盖您在 CSS 中设置的值。

Generally, for styles like titles, it is probably best to use a style class rather than an id.通常,对于 styles 之类的标题,最好使用样式 class 而不是 id。

I don't think using the * universal selector is a good idea.我不认为使用*通用选择器是个好主意。 Maybe it is OK, maybe it is not.也许没关系,也许不是。 The standard JavaFX modena.css style sheet uses a syntax of .root for the defaults, perhaps that would be better for you.标准 JavaFX modena.css样式表默认使用.root的语法,也许这对您来说更好。

A description of the root style class in the Scene JavaFX CSS: Scene JavaFX CSS中root样式class的描述:

The Scene object has no settable CSS properties, nor does it have any pseudo‑classes.场景 object 没有可设置的 CSS 属性,也没有任何伪类。 However, the root node of the scene is assigned the style class "root" (in addition to style classes already assigned to the node).然而,场景的根节点被分配了样式 class “root”(除了已经分配给节点的样式类)。 This is useful because the root node of Scene is the root container for all active scene‑graph nodes.这很有用,因为场景的根节点是所有活动场景图节点的根容器。 Thus, it can serve as a container for properties that are inherited or looked up.因此,它可以作为继承或查找的属性的容器。

Also, you tagged your question javafx-8.此外,您标记了您的问题 javafx-8。 JavaFX 8 is obsolete, and a more modern version of JavaFX is recommended. JavaFX 8 已过时,建议使用更现代的版本 JavaFX。

Changing default colors更改默认值 colors

If you want to change the default colors for the application, then you can do that by overriding the looked-up colors defined in the .root section of the default modena.css stylesheet.如果您想更改应用程序的默认值 colors,那么您可以通过覆盖默认modena.css样式表的.root部分中定义的查找 colors 来实现。

The main looked-up color which you might override is -fx-base which sets the default color style for the entire application.您可能会覆盖的主要查找颜色是-fx-base ,它为整个应用程序设置默认颜色样式。

Study the JavaFX CSS reference if you would like to learn more about looked-up colors.如果您想了解有关查找 colors 的更多信息,请研究 JavaFX CSS 参考资料。

For example, the following style sheet rule sets the default font size to 20 and the default color of the text in labels to dark green.例如,以下样式表规则将默认字体大小设置为 20,并将标签中文本的默认颜色设置为深绿色。

.root {
    -fx-font-size: 20;
    -fx-text-background-color: darkgreen;
}

Example App示例应用程序

The example defines a default size for text in the app within the .root section.该示例在.root部分中定义了应用程序中文本的默认大小。 The default style size is overridden in the .title section so that titles can be larger.默认样式大小在.title部分被覆盖,因此标题可以更大。 The example application requires JavaFX 17+.示例应用程序需要 JavaFX 17+。

The example application doesn't set default colors, but, if you want to do that, you can experiment by changing the supplied CSS to match the CSS in the default colors section of the answer.示例应用程序未设置默认值 colors,但如果您想这样做,您可以通过更改提供的 CSS 以匹配答案的默认 colors 部分中的 CSS 进行试验。

地球人

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RootFontSizeApp extends Application {

    public static final String CSS = "data:text/css," + // language=CSS
            """
            .root {
                -fx-font-size: 20;
            }
            
            .title {
                -fx-font-size: 30;
            }
            """;

    @Override
    public void start(Stage stage) throws Exception {
        Label title = new Label("Greetings earthlings");
        title.getStyleClass().add("title");

        Scene scene = new Scene(
                new VBox(20,
                        title,
                        new Label("hello, world")
                )
        );
        scene.getStylesheets().setAll(CSS);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM