在你的 start 方法中创建一个Group
and 场景:
Group root = new Group();
Scene scene = new Scene(root, 551, 400, Color.BLACK);
primaryStage.setScene(scene);
创建ImageView
并设置以下属性:
ImageView imageView = new ImageView();
// set aspect ratio
imageView.setPreserveRatio(true);
// resize based on the scene
imageView.fitWidthProperty().bind(scene.widthProperty());
imageView.fitHeightProperty().bind(scene.heightProperty());
创建一个StackPane
(至少这是我使用的)并绑定您的属性:
StackPane stack = new StackPane();
stack.getChildren().add(imageView);
stack.translateXProperty()
.bind(scene.widthProperty().subtract(stack.widthProperty())
.divide(2));
stack.translateYProperty()
.bind(scene.heightProperty().subtract(stack.heightProperty())
.divide(2));
将此堆栈添加到根元素:
root.getChildren().add(stack);
primaryStage
在 start 方法中显示并执行其他代码:
primaryStage.show();