[英]javafx-Replacing background image of a BorderPane
I have a BorderPane
on a Scene
.& I have a background image in that pane. 我在Scene
上有BorderPane
。在该窗格中有背景图片。 My code for that image: 我的图像代码:
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);
Later I have added some VBox
as Left , Right portion in that pane.I want to change the background image of that BorderPane
(Obviously without causing any change in the VBox's position,element) with particular Button
click.How can I do it? 后来我在该窗格中的左,右部分添加了一些VBox
我想通过单击特定的Button
来更改BorderPane
的背景图像(显然不会导致VBox的位置,元素发生任何变化)。我该怎么做?
Just really use a background image instead of adding a ImageView
as child: 只是真正使用背景图像,而不是将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.