拾色器标签颜色

时间:2019-04-29 15:13:58

标签: javafx-8 color-picker

如何使用CSS和setStyle(...)在Javafx 8.0中更改ColorPicker控件的标签颜色?

我尝试过cb1.setStyle(“-fx-xxx;”);我为xxx使用了许多不同的组合,但我找不到正确的组合。 请提供正确的代码帮助

public class Sample extends Application {   
@Override
public void start(Stage primaryStage) {

    TextField tf1 = new TextField();
    tf1.setText("SAMPLE");
    tf1.setAlignment(Pos.CENTER);
    final ColorPicker cp1 = new ColorPicker();
    cp1.setValue(Color.RED);
    cp1.setStyle("-fx-background-color: rgba(0,0,0,0);-fx-color-label-visible:true;");
    cp1.valueProperty().addListener((observable, oldValue, newValue) -> {
        tf1.setStyle("-fx-background-color: rgba(0,0,0,0);-fx-border-color: " + colorToRGBCSS(cp1.getValue()) + ";\n-fx-border-width: 2;\n-fx-text-fill:" + colorToRGBCSS(cp1.getValue()));
        cp1.setStyle("-fx-background-color: rgba(0,0,0,0);-fx-color-label-visible:true;\n-fx-color-label-text-fill:" + colorToRGBCSS(cp1.getValue()));

    });

    tf1.setStyle("-fx-background-color: rgba(0,0,0,0);-fx-border-color: " + colorToRGBCSS(cp1.getValue()) + ";\n-fx-border-width: 2;-fx-text-fill: red;");
    tf1.setFont(Font.font("Arial", FontWeight.BOLD, FontPosture.REGULAR, 30));
    VBox root = new VBox();
    root.setSpacing(10);
    root.getChildren().addAll(tf1,cp1);
    Scene scene = new Scene(root, 400, 300);
    primaryStage.setTitle("Sample");
    primaryStage.setScene(scene);
    primaryStage.show();
}
public String colorToRGBCSS(Color color) {
    double red = color.getRed() * 255;
    double green = color.getGreen() * 255;
    double blue = color.getBlue() * 255;
    double alpha = color.getOpacity() * 255;
    String colorString = String.format("rgba(%f,%f,%f,%f) ;", red, green, blue, alpha);
    return colorString;
}
public static void main(String[] args) {
    launch(args);
}

}

0 个答案:

没有答案