编程方式
为了您StackPane你可以使用BackgroundImage类:
BackgroundImage backgroundImage= new BackgroundImage(new Image(getClass().getResource("thinking-man.jpg").toExternalForm(),32,32,false,true),
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
stackPane.setBackground(new Background(backgroundImage));
对于按钮:按钮有一个graphic属性:
button.setGraphic(new ImageView(new Image(getClass().getResource("thinking-man.jpg").toExternalForm())));
使用CSS
如果您更喜欢使用样式表来设置背景图片,你可以使用:
-fx背景图像,-fx背景重复,-fx背景位置和-fx-background-size。
使用相同的例子与StackPane:
stackPane.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stackPane.getStyleClass().add("stackpane-with-background");
在application.css:
.stackpane-with-background{
-fx-background-image: url("thinking-man.jpg");
-fx-background-repeat: stretch;
-fx-background-size: 900 506;
-fx-background-position: center center;
-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
}
希望这有助于。