【问题标题】:How can I change the size of 1 specific label's font if I have a CSS file for labels?如果我有标签的 CSS 文件,如何更改 1 个特定标签字体的大小?
【发布时间】:2019-03-30 22:41:51
【问题描述】:

我有一个 CSS 文件:

label {
-fx-font-size: 10px;
-fx-font-weight: bold;
-fx-text-fill: #333333;
-fx-effect: dropshadow( gaussian , rgba(255,255,255,0.5) , 0,0,0,1 );
}

如果我想让一个标签有另一种样式,我该怎么做?我正在用下面的代码尝试它,但它没有改变。

l_mov.setFont(Font.font("calibri", FontWeight.BOLD, FontPosture.REGULAR, 
25));

【问题讨论】:

  • 是否有任何理由不在要更改的元素上使用 ID 并将自定义样式应用于该元素?

标签: css user-interface javafx label


【解决方案1】:

如果您有两个样式不同的标签,也许您可​​以使用两个不同的 'id' 来识别它们。

【讨论】:

  • 我有 200 个标签。问题是我希望 199 具有相同的样式(这就是我使用 CSS 的原因)而另一个具有不同的样式
  • @Baspro 这就是为什么你应该使用 id 作为你的特定标签。
  • 没错,为特殊的使用特定的id
  • 谢谢,我去看看。
【解决方案2】:

即使Chao-Wen chen solution 似乎最适合您的情况:(使用id)。您应该记住,您也可以使用自己的 css 样式类。 这是一个更详尽的示例:

public class CssApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        VBox vb = new VBox(10.0);

        // Title
        Label title = createTitle("Title");

        // SubTitle 1
        Label subtitle1 = createSubTitle("SubTitle1");
        //Standard text
        Label lorem = new Label("Lorem ipsum dolor sit amet, mea eu nibh sonet accusam, mea dicunt oblique et. Mei purto efficiantur ne, quas audiam consulatu at eum. Ea sit dicta zril, adipisci praesent pertinacia ei his.");

        // SubTitle 2
        Label subtitle2 = createSubTitle("SubTitle2");
        //Standard text
        Label ipsum = new Label("Amet dictas consequat ut vix, maluisset hendrerit vim ex, ne pro tale aliquid accusata. Mea porro aperiri voluptua te, case lorem per eu.");
        Label specificText = new Label("I am a specific text with a particular styling.");
        specificText.setId("specific");

        vb.getChildren().addAll(title, subtitle1, lorem, subtitle2, ipsum, specificText);
        Scene scene = new Scene(vb, 500.0, 400.0);
        scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }


    private static Label createTitle(String pText) {
        Label newLabel = new Label(pText);
        newLabel.getStyleClass().add("label-title");
        return newLabel;
    }

    private static Label createSubTitle(String pText) {
        Label newLabel = new Label(pText);
        newLabel.getStyleClass().add("label-subtitle");
        return newLabel;
    }
}

这里是 css 样式表。请注意,样式类的顺序很重要。

.label {
    -fx-font-family: serif;
    -fx-font-size: 12.0;
    -fx-font-weight: normal;
    -fx-text-fill: black;
}

.label-title {
    -fx-font-family: sans-serif;
    -fx-font-size: 50.0;
    -fx-font-weight: bold;
    -fx-text-fill: red;
}

.label-subtitle {
    -fx-font-family: monospace;
    -fx-font-size: 19.0;
    -fx-font-style: oblique;
    -fx-font-weight: normal;
    -fx-text-fill: blue;
}

#specific {
    -fx-text-fill: green;
}

处理这种例子(这是一个简单的例子,仅用于演示):

对于这个例子,我只使用一个样式表,但是可以为特定面板应用不同的样式表(如果提供了相同的样式类,这个样式表将覆盖所有祖先的样式表)。

【讨论】:

  • 我建议使用.label.label-title 和.label.label-subtitle 作为选择器,而不仅仅是.label-title 和.label-subtitle。这样,规则的顺序并不重要,因为选择器比基于单个类的选择器更具体。在您当前的样式表中,将 .label 规则移动到样式表的底部会使用 .label 样式设置除 #specific 之外的所有标签。
  • 你说得对,这就是为什么我说样式类的顺序很重要。但是您的解决方案可以摆脱订单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多