JavaFX - 根据加载的图像大小调整窗口大小

[英]JavaFX - Resize Window based on loaded image size


My main Windows appearance is defined by an FXML file with a BorderPane structure. The center tag is defined as follows:

我的主要Windows外观由具有BorderPane结构的FXML文件定义。中心标签定义如下:

<center>
    <ScrollPane focusTraversable="false">
        <content>
            <Pane fx:id="mapArea">
                <ImageView fx:id="mapImageView" />
            </Pane>
        </content>
    </ScrollPane>
</center>

In the program, the user can open an image file representing a map, add some points of interests, and save it all for later. The image can be of any size. What I want is for the main window to resize so that when an image is loaded, it fits perfectly in the ImageView, although users are allowed to resize manually, for example if the image is too large for their monitor.

在程序中,用户可以打开表示地图的图像文件,添加一些兴趣点,并将其全部保存以供日后使用。图像可以是任何大小。我想要的是主窗口调整大小,以便在加载图像时,它完全适合ImageView,但允许用户手动调整大小,例如,如果图像对于他们的显示器来说太大。

Right now in my controller, I execute the following lines when an image is loaded:

现在在我的控制器中,我在加载图像时执行以下行:

double width = getMap().getImage().getWidth();
double height = getMap().getImage().getHeight();
mapImageView.prefWidth(width);
mapImageView.prefHeight(height);

This has some strange (to me) effects. The height of the window is set so that the image COULD be fully shown if the ImageView was the only Node in the Scene, but obviously it is not. The width of the windows doesn't seem to be affected at all.

这有一些奇怪的(对我而言)效果。设置窗口的高度,以便如果ImageView是场景中唯一的节点,则可以完全显示图像,但显然不是。窗户的宽度似乎根本没有受到影响。

I have tried fiddling around with different properties on different objects, minWidth, maxWidth, prefWidth, setResizeable and so on, but I feel like I am stumbling in the dark. I don't really know how it all comes together, and this SOUNDS like something that should be trivial.

我试着在不同的对象上摆弄不同的属性,minWidth,maxWidth,prefWidth,setResizeable等,但我觉得我在黑暗中磕磕绊绊。我真的不知道这一切是如何结合在一起的,这个声音就像是应该是微不足道的。

Please let me know if you require more code.

如果您需要更多代码,请告诉我。

1 个解决方案

#1


In JavaFX when you want to set a value to some field X it will typically have a corresponding setX() method.

在JavaFX中,当您想要将值设置为某个字段X时,它通常会具有相应的setX()方法。

prefWidth(double) and prefHeight(double) return node's preferred values for width and height. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#prefWidth-double-

prefWidth(double)和prefHeight(double)返回节点的宽度和高度的首选值。 https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#prefWidth-double-

If you want to set the window(JavaFX Stage) size then in your start(Stage) method, obtain a reference to the primaryStage. Once you've loaded your image, you can do stage.setWidth(width) and stage.setHeight(height) Example:

如果要在start(Stage)方法中设置窗口(JavaFX Stage)大小,请获取对primaryStage的引用。加载图像后,可以执行stage.setWidth(width)和stage.setHeight(height)示例:

public class YourJavaFXApp extends Application {

    private Stage stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        // some code here
    }
}

If you want to resize the loaded image to fit the current window size instead then you can use mapImageView.setFitWidth(width) and mapImageView.setFitHeight(height)

如果要调整已加载图像的大小以适合当前窗口大小,则可以使用mapImageView.setFitWidth(width)和mapImageView.setFitHeight(height)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/03/591bf342c03738ffd64524e8b9fd0609.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com