【问题标题】:JavaFx: How to make the background image size change with the window when increasing/decreasing the window size?JavaFx:增加/减少窗口大小时如何使背景图像大小随窗口变化?
【发布时间】:2018-09-17 17:20:34
【问题描述】:

如何在增加或减少窗口框架时将背景图像设置为窗口大小?现在图像的设置大小为 300,300,因此当增加窗口时,大小保持不变。 这是我的代码的 sn-p:

    grid.getChildren().addAll(nameLabel, nameInput, passLabel, passInput, 
    loginButton);

    grid.setMinSize(300,300);
    grid.setMaxSize(300,300);
    StackPane root = new StackPane(grid);
    NumberBinding maxScale = Bindings.min(root.widthProperty().divide(300), 
    root.heightProperty().divide(300));
    grid.scaleXProperty().bind(maxScale);
    grid.scaleYProperty().bind(maxScale);

    BackgroundImage myBI= new BackgroundImage(new Image("image.jpg", 300, 300, 
    false, true),
    BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, 
    BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
    //then you set to your node
    grid.setBackground(new Background(myBI));

    Scene scene = new Scene(root, 300,300);
    window.setScene(scene);
    window.show();

【问题讨论】:

标签: java image user-interface javafx background-image


【解决方案1】:

你可以尝试根据documentation实例化一个BackgroundSize对象,并将cover值设置为true。然后不要使用 BackGroundSize.DEFAULT,而是使用您创建的 BackGroundSize 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多