How do I center an Image view in an anchor pane?(如何在锚窗格中将图像视图居中?)
问题描述
我正在使用场景构建器来创建我的 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);
这篇关于如何在锚窗格中将图像视图居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在锚窗格中将图像视图居中?
基础教程推荐
- 将 double 转换为 Int,向下舍入 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
