提问者:小点点

Java Fx-添加样式的标签编程不覆盖


我想根据条件向标签添加样式。当我尝试这样做时,它会覆盖以前的样式。有没有更好的方式来连接这些风格?

    Label lbl = new Label(labelDescription.getLabelText());

    if (labelDescription.getLabelStyling() == LabelStyling.BOLD) {
        lbl.setStyle("-fx-font-weight: bold;");
    }

    if (labelDescription.getLabelStyling() == LabelStyling.ITALIC) {
        lbl.setStyle("-fx-font-style: italic;");
    }

    if (labelDescription.getTextSize() != null) {
        lbl.setStyle("-fx-font-size: " + labelDescription.getTextSize() + "px;");
    }

共2个答案

匿名用户

我认为对您的案例来说,重构一点样式逻辑会更容易,如下所示:

Label lbl = new Label(labelDescription.getLabelText());

String style = "";
if (labelDescription.getLabelStyling() == LabelStyling.BOLD) {
    style += "-fx-font-weight: bold;";
}

if (labelDescription.getLabelStyling() == LabelStyling.ITALIC) {
    style += "-fx-font-style: italic;";
}

if (labelDescription.getTextSize() != null) {
    style += "-fx-font-size: " + labelDescription.getTextSize() + "px;";
}

lbl.setStyle(style);

虽然如果设置了超过这3个值,这可能会非常低效,但是您应该考虑使用StringBuilder

希望有帮助!

匿名用户

注您可以通过编程方式设置字体,而不需要使用CSS:

Label lbl = new Label(labelDescription.getLabelText());

// get defaults:
Font font = lbl.getFont();
String family = font.getFamily();
double size = font.getSize();
FontPosture posture = FontPosture.REGULAR ;
FontWeight weight = FontWeight.NORMAL ;

if (labelDescription.getLabelStyling() == LabelStyling.BOLD) {
    weight = FontWeight.BOLD ;
}

if (labelDescription.getLabelStyling() == LabelStyling.ITALIC) {
    posture = FontPosture.ITALIC ;
}

if (labelDescription.getTextSize() != null) {
    size = labelDescription.getTextSize();
}

lbl.setFont(Font.font(family, weight, posture, size));

如果您真的想使用CSS,您可以通过字符串连接来构建样式,或者使用外部样式表和自定义CSS伪类来构建字体粗细和样式。

例如:

label-style.css:

/*
Note that you have to use a font which supports
bold, italic, and bold-italic on the system.
*/

#my-label {
  -fx-font-family: "Arial" ;
}

#my-label:important {
  -fx-font-weight: bold ;
}

#my-label:emphasized {
  -fx-font-style: italic ;
}

我通常更喜欢这种方法,因为它使以逻辑方式以编程方式更改样式以响应应用程序中的状态更改变得更容易。对于颜色,您可以使用“查找颜色”。唯一的缺点是我不知道改变字体大小的方法,所以这些仍然需要通过内联样式来完成。

下面是一个使用该方法的完整示例,它根据用户输入设置伪类:

import javafx.application.Application;
import javafx.css.PseudoClass;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LabelStyleTest extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        
        VBox root = new VBox(5);
        root.setAlignment(Pos.CENTER);
        
        CheckBox important = new CheckBox("Important");
        CheckBox emphasized = new CheckBox("Emphasized");
        Spinner<Integer> size = new Spinner<>(4, 36, 12);

        HBox sizeBox = new HBox(2, new Label("Size:"), size);
        sizeBox.setAlignment(Pos.CENTER);
        
        Label label = new Label("A label");
        label.setId("my-label");
        
        PseudoClass importantPC = PseudoClass.getPseudoClass("important");
        PseudoClass emphasizedPC = PseudoClass.getPseudoClass("emphasized");
        
        important.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> 
            label.pseudoClassStateChanged(importantPC, isNowSelected));
        emphasized.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> 
            label.pseudoClassStateChanged(emphasizedPC, isNowSelected));
        
        size.valueProperty().addListener((obs, oldSize, newSize) -> 
            label.setStyle("-fx-font-size: "+newSize+" px;"));
        
        root.getChildren().addAll(important, emphasized, sizeBox, label);
        Scene scene = new Scene(root, 800, 500);
        scene.getStylesheets().add("label-style.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

在您的示例中,您将执行如下操作:

Label lbl = new Label(labelDescription.getLabelText());
lbl.setId("my-label");

PseudoClass importantPC = PseudoClass.getPseudoClass("important");
PseudoClass emphasizedPC = PseudoClass.getPseudoClass("emphasized");

lbl.pseudoClassStateChanged(importantPC, 
    labelDescription.getLabelStyling() == LabelStyling.BOLD));

lbl.pseudoClassStateChanged(emphasizedPC, 
    labelDescription.getLabelStyling() == LabelStyling.ITALIC);

if (labelDescription.getTextSize() != null) {
    lbl.setStyle("-fx-font-size: " + labelDescription.getTextSize() + "px;");
}