如果我有标签的CSS文件,如何更改1个特定标签的字体的大小?

时间:2018-10-26 08:04:00

标签: css user-interface javafx label

我有一个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));

2 个答案:

答案 0 :(得分:3)

即使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;
}

涉及这种示例(这只是一个简单的示例):

enter image description here

在此示例中,我仅使用一个样式表,但是可以将不同的样式表应用于特定的面板(如果提供了相同的样式类,则该样式表将覆盖所有祖先样式)。

答案 1 :(得分:2)

如果您有两个具有不同样式的标签,则也许可以使用两个不同的“ id”来标识它们。