问题描述
我在StackPane中调整ImageView的大小时遇到了一个大问题:如果StackPane是根容器,并且我将图像的高度和宽度绑定到StackPane,则一切都很好.
I have a ploblem with resizing of ImageView in StackPane:If StackPane is root container and I bind image's height and width to StackPane, everything is fine.
<StackPane id="StackPane" fx:id="stack" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: lightgreen;" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication8.FXMLController">
<children>
<ImageView fx:id="image" fitHeight="100.0" fitWidth="300.0" pickOnBounds="true" preserveRatio="true" />
</children>
<stylesheets>
<URL value="@fxml.css" />
</stylesheets>
</StackPane>
但是,如果我将此类堆栈窗格放置在例如网格中,则图像无法正确调整大小.如我所读,ImageView无法调整大小的问题.有什么方法可以调整它的大小?或者您可以给一些建议如何调整ImageView的大小?
But if I place such stack pane in, for example, grid, than image doesnt resize properly.As I read, the problem that ImageView is non-Resizable. Is there any ways to make it resizable? Or could you give some advise how to resize ImageView?
推荐答案
听起来您正在尝试为StackPane创建背景图像.在这种情况下,通过CSS应用图像可能比ImageView更好.
It sounds like you are trying to create a background image for the StackPane. In which case applying the image via CSS may be a better route to use than an ImageView.
.stack-pane {
-fx-background-image: url("flower.png");
-fx-background-size: cover;
}
请参见背景图片部分.
See the background images section of the Region specification in the JavaFX CSS reference guide.
使可调整大小的ImageView本身是一个尚未解决的主要问题.该问题链接到简单解决方法的代码-可调整大小的ImagePane,用于包装图像填充可调整大小的窗格.
Making ImageView itself resizable is an unresolved major issue. The issue links to code for a simple workaround - a resizable ImagePane wrapping an Image to fill the resizable pane.
这篇关于在StackPane中调整JavaFX ImageView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!