简体   繁体   中英

Stage: Not resizing to Scene

I have a scene on a stage. The width of the scene is 337.0 pixels. However, when I add it to the stage, the size of the stage is 337.6 pixels which leaves a white gap at the right edge of the screen due to the 0.6 pixel difference.

I tried using stage.sizeToScene() , but that's not working. I also tried setting the width of the stage manually by trying stage.setWidth(337) . that did not affect the width of the stage, it still remained 337.6.

The I tried the following: stage.setWidth(337.0) . Then when I print the stage width on the console, by using stage.getWidth() , the value printed is 337.0, however in actual, the 0.6 pixel white line still stays.

I tried doing the following alos:

stage.hide();
stage.show();

This worked, it removed the white line, but it shows the window switching which looks very bad. Is there a way to do the same without switching windows like above.

Any help will be appreciated.

I had a similar problem with setting the correct size for my stage.

What helped in my case was using:

stage.setMinWidth(newActiveScene.getRoot().minWidth(-1));
stage.setMinHeight(newActiveScene.getRoot().minHeight(-1));

where stage is the stage you are using and newActiveScene is your new scene set on the stage.

Actually I am using a method to set new Scenes on a Stage that looks like that:

/**
 * Sets a new active {@link Scene} on the stage.
 *
 * @param newActiveScene the new scene to show
 */
public static void setActiveScene(Scene newActiveScene) {
    stage.setScene(newActiveScene);
    stage.setMinWidth(newActiveScene.getRoot().minWidth(-1));
    stage.setMinHeight(newActiveScene.getRoot().minHeight(-1));
}

where stage is the used Stage. This assumes that stage.show() was already called once (for example when initializing the stage).

As suggested by Simon Wall, I would like to make some edits to his code. This is what worked well for me:

    public void setActiveScene(Scene newActiveScene) {
        stage.setScene(newActiveScene);
        stage.setWidth(newActiveScene.getRoot().minWidth(-1));
        stage.setHeight(newActiveScene.getRoot().minHeight(-1));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM