我正在做一个元素,试图从图片中找到最常见的颜色。我找到这个的代码有效,但我想将场景的背景颜色设置为我找到的 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;
}
最佳答案
有两种方法:
内联样式方法
setStyle(String style)
:layout.setStyle("-fx-background-color: rgb(" + r + "," + g + ", " + b + ");");
r, g, b
取值范围 -> (0 - 255)方法
setBackground(背景值)
:layout.setBackground(new Background(new BackgroundFill(Color.rgb(r, g, b), CornerRadii.EMPTY, Insets.EMPTY)));
r, g, b
取值范围 -> (0 - 255)
关于css - JavaFx 如何在 CSS 中使用 java 生成的 RGB 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42111471/