j w*_*ill 4 java user-interface javafx imageview scenebuilder
我正在使用场景构建器来创建我的GUI,并且Image当用户选择列表中的项目时我显示s.的Images为不同的尺寸,并且当一个Image比它在窗格越小,图像被显示在窗格的左上方.如何获取ImageView和/或Image显示在Pane?的中心?
我看了整个JavaFX的imageview的并且图像API,但我还没有发现太多为中心的ImageView一个Pane.我也没有在场景构建器中看到任何东西.通常在场景构建器中,如果有一种方法可以使节点居中,则会有一个居中选项.
Ans*_*har 11
在任何类型的窗格中,它现在可以在特定位置设置图像对齐..为此您可以使用try HBox
当您插入Image到HBox,初步显示出像下面的.
现在设置的对齐属性 HBox

现在将对齐更改为居中并查看输出

我希望它为你工作
它也可以在Panevia get图像中以编程方式执行
试试这个
AnchorPane anchorPane = new AnchorPane();
Image img = new Image(getClass().getResourceAsStream("Edit-Male-User-icon.png"));
ImageView imageView = new ImageView(img);
anch.getChildren().add(imageView );
ImageView app = (ImageView) (Node) anchorPane.getChildren().get(0);
app.setX(100);
app.setY(100);
Run Code Online (Sandbox Code Playgroud)
Group在start方法中创建一个和场景:
Group root = new Group();
Scene scene = new Scene(root, 551, 400, Color.BLACK);
primaryStage.setScene(scene);
Run Code Online (Sandbox Code Playgroud)
创建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());
Run Code Online (Sandbox Code Playgroud)
创建一个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));
Run Code Online (Sandbox Code Playgroud)
将此堆栈添加到根元素:
root.getChildren().add(stack);
Run Code Online (Sandbox Code Playgroud)
primaryStage在start方法中显示并执行其他代码:
primaryStage.show();
Run Code Online (Sandbox Code Playgroud)