在JavaFX 8中更改Imageview的背景颜色

时间:2014-06-11 16:00:17

标签: javafx-8

我试图使用CSS更改ImageView背景颜色bt无法改变它...

如何在Javafx8中设置我的ImageView背景颜色?

任何人都可以帮助我吗?

以下是我的屏幕截图:screenshot

我想用黑色替换图像的灰色背景。

1 个答案:

答案 0 :(得分:3)

好吧,您似乎需要为Screen而不是ImageView

添加背景颜色

这是一个工作代码和输出

public class ImageViewBackgroundColor extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        try {
            stage.setWidth(Screen.getPrimary().getBounds().getWidth());
            stage.setHeight(Screen.getPrimary().getBounds().getHeight());
            BorderPane borderPane = new BorderPane();
            ImageView imageView = new ImageView();
            Image image = new Image(getClass().getResource("huskar.jpg")
                    .toString());
            imageView.setImage(image);
            imageView.setStyle("-fx-background-color: BLACK");
            imageView.setFitHeight(stage.getHeight());
            imageView.setPreserveRatio(true);
            imageView.setSmooth(true);
            imageView.setCache(true);
            borderPane.setCenter(imageView);
            Scene scene = new Scene(borderPane, Color.BLACK);
            stage.setScene(scene);
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

<强>输出

enter image description here

相关问题