简体   繁体   中英

Change Scene without resize window in JavaFX

I'm trying to change Scenes on JavaFX, but without change the window size. However, when I set stage.setScene(scene2); the window size decreases, and I want to keep the both Scenes maximized. I've tried to stage.setMaximized(true) after the stage.setScene(scene2); but the result is the same.

How can I fix it?

My code:

package controller;

import java.io.IOException;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.util.Duration;

public class App extends Application {  
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("../view/fxml/Loading.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle("Project");
        stage.setMaximized(true);
        stage.show();

        FadeTransition fadeIn = new FadeTransition(Duration.seconds(1), root);
        fadeIn.setFromValue(0);
        fadeIn.setToValue(1);

        FadeTransition fadeOut = new FadeTransition(Duration.seconds(1), root);
        fadeOut.setFromValue(1);
        fadeOut.setToValue(0);

        fadeIn.play();

        fadeIn.setOnFinished((e) -> {
            fadeOut.play();
        });

        fadeOut.setOnFinished((e) -> {
            try {               
                Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml"));

                Scene scene2 = new Scene(root2);

                stage.setScene(scene2);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}

When I compile: 在此输入图像描述

Then the fadeOut/fadeIn occurs and (It is here that I want to keep maximized): 在此输入图像描述

It's probably better here just to replace the root of the existing scene, than to create a new scene:

fadeOut.setOnFinished((e) -> {
    try {               
        Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml"));

        scene.setRoot(root2);

    } catch (IOException e1) {
        e1.printStackTrace();
    }
});

If you really do need to replace the scene, for some reason, you can set the new scene's size to the same as the existing scene:

fadeOut.setOnFinished((e) -> {
    try {               
        Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml"));

        Scene scene2 = new Scene(root2, scene.getWidth(), scene.getHeight());

        stage.setScene(scene2);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
});

I have been dealing with an issue similar to yours for the last 4-5 hours and given @James_D's answer I saw the way to simply fix the resizing issue with scene changes. This answer still directly goes off of @James_D, but I did not feel there was a clear explanation as to what was going on, so more than anything I am posting this to help anybody who runs across this thread understand why the second solution @James_D provided (ie Changing scenes, not the root of the scene) works.

Note: I did run across a few answers stating that setting a new root for the scene may be a better option than changing the whole scene, but that did not work in my case, so changing scenes was the best option for me.

Anyways, I am pretty sure the reason the scene changes sizes when swapped is because you do not set the scene width and height attributes on the scene object explicitly. Not specifically setting the size appears to make the scene adjust to the bounds of the objects it contains automatically when the new scene is loaded to the stage.

Basically, to fix the problem all you need to do is set the width and height attributes when you create your scene, like below.

Before

Scene scene2 = new Scene(root2);

After

Scene scene2 = new Scene(root2, 900, 600);//or whatever size you want
stage.setScene(scene2);//now we are set if initial scene is 900w X 600h, scene size will stay the same

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