如何在锚窗格中将图像视图居中?

我正在使用场景构建器来创建我的 GUI,并且当用户选择列表中的项目时,我正在显示 Image.Image 的大小不同,当 Image 小于它所在的窗格时,图像会显示在窗格的左上角.如何让 ImageView 和/或 Image 显示在 Pane 的中心?

I am using scene builder to create my GUI and I am displaying Images as users select items in a list. The Images are of different sizes and when an Image that is smaller than the pane it is in, the image is displayed in the top left of the pane. How do I go about getting the ImageView and/or Image to be displayed in the center of the Pane?

我查看了整个 javafx imageview 和 image API,但我没有发现太多关于将 ImageView 置于 Pane 的中心.我也没有在场景构建器中看到任何东西.通常在scene builder中,如果有办法使节点居中,就会有居中的选项.

I've looked throughout the javafx imageview and image APIs but I haven't found much on centering the ImageViewin a Pane. I haven't seen anything in scene builder, either. Usually in scene builder, if there is a way to center a node, there will be an option for centering.

推荐答案

现在可以在任何类型的窗格中设置特定位置的图像对齐.. 为此,您可以使用 try HBox

In any kind of pane its now possible to set image alignment in specific postion.. for this you can use try HBox

当您将 Image 插入 HBox 时,最初如下所示.

when you insert an Image into the HBox, initially its shown like below.

现在设置 HBox

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

now change the alignment to centre and see the output

希望对你有用

也可以通过获取图像在 Pane 中以编程方式完成此操作

Its also possible to do so programmatically in a Pane via get image

试试这个

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);

相关文章