簡體   English   中英

javafx-替換BorderPane的背景圖像

[英]javafx-Replacing background image of a BorderPane

我在Scene上有BorderPane 。在該窗格中有背景圖片。 我的圖像代碼:

BorderPane B_pane = new BorderPane();
Image image = new Image("/Client/social3.png",width,height,false,true,true);
ImageView imageView = new ImageView(image);
B_pane.getChildren().add(imageView);

后來我在該窗格中的左,右部分添加了一些VBox我想通過單擊特定的Button來更改BorderPane的背景圖像(顯然不會導致VBox的位置,元素發生任何變化)。我該怎么做?

只是真正使用背景圖像,而不是將ImageView添加為子ImageView

@Override
public void start(Stage primaryStage) {
    BorderPane borderPane = new BorderPane();
    Image image1 = new Image("https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/se/se-logo.png?v=dd7153fcc7fa");
    Image image2 = new Image("https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a");

    BackgroundSize bSize = new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false);

    Background background2 = new Background(new BackgroundImage(image2,
            BackgroundRepeat.NO_REPEAT,
            BackgroundRepeat.NO_REPEAT,
            BackgroundPosition.CENTER,
            bSize));

    borderPane.setBackground(new Background(new BackgroundImage(image1,
            BackgroundRepeat.NO_REPEAT,
            BackgroundRepeat.NO_REPEAT,
            BackgroundPosition.CENTER,
            bSize)));

    Button btn = new Button("Change Background");
    btn.setOnAction((ActionEvent event) -> {
        borderPane.setBackground(background2);
    });

    borderPane.setCenter(btn);

    Scene scene = new Scene(borderPane, 600, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM