首页 文章

在JavaFX8中更改图像视图的背景颜色

提问于
浏览
3

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

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

谁能帮我吗?

这是我的截图:
screenshot

我想用 Black 替换图像的灰色背景 .

1 回答

  • 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);
        }
    }
    

    Output

    enter image description here

相关问题