我在 Scene
上有一个 BorderPane
。& 我在该 Pane 中有一个背景图像。
我对该图像的代码:
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);
后来我在该 Pane 中添加了一些 VBox
作为 Left 和 Right 部分。我想更改那个 BorderPane
的背景图像(显然不会引起任何变化VBox 的位置、元素)与特定的 Button
单击。我该怎么做?
最佳答案
只是真正使用背景图像而不是添加 ImageView
作为 child :
@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();
}
关于javafx 替换 BorderPane 的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41188217/