【问题标题】:JavaFx how to use java generated RGB color in CSSJavaFx如何在CSS中使用java生成的RGB颜色
【发布时间】:2017-06-25 23:21:50
【问题描述】:

我正在从事一个项目,我试图从图片中找到最常见的颜色。我找到这个的代码有效,但我想将场景的背景颜色设置为我找到的 rgb 颜色。

我知道如何使用 css 设置场景的背景颜色,但我不知道如何在其中使用我的方法。如果不可能,有没有其他方法可以设置背景颜色?

现在的css代码:

.root{
-fx-background-color: rgb(50,50,50);
-fx-font-size: 11pt; 
}

现在的 JavaFx 代码:

Stage window;

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

@Override
public void start(Stage primaryStage) throws Exception {
    ColorFinder finder= new CollorFinder("/imgs/picture.jpg");
    int r = finder.rood();
    int g = finder.groen();      //calling my method and setting r g & b
    int b = finder.blauw();

    window = primaryStage;
    window.setTitle("Color");

    Label text = new Label("Most popular color:");
    Label rgb = new Label("rgb("+r+","+g+","+b + ")");



    VBox layout = new VBox(20);
    layout.getChildren().addAll(text,rgb);
    layout.setAlignment(Pos.CENTER);

    Scene scene = new Scene(layout, 300,200);
    String css = gui.class.getResource("styles.css").toExternalForm();
    scene.getStylesheets().add(css);
    window.setScene(scene);
    window.show();
}
}

我想在 css 中做但不可能的事情:

ColorFinder finder= new CollorFinder("/imgs/picture.jpg");
    int r = finder.rood();
    int g = finder.groen();
    int b = finder
.root{
    -fx-background-color: rgb(r,g,b);
    -fx-font-size: 11pt;
}

【问题讨论】:

    标签: css javafx colors rgb background-color


    【解决方案1】:

    有两种方法:

    1. 内联样式方法setStyle(String style)

      layout.setStyle("-fx-background-color: rgb(" + r + "," + g + ", " + b + ");");
      

      r, g, b 取值范围 -> (0 - 255)

    2. 方法setBackground(Background value)

      layout.setBackground(new Background(new BackgroundFill(Color.rgb(r, g, b), CornerRadii.EMPTY, Insets.EMPTY)));
      

      r, g, b 取值范围 -> (0 - 255)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多