Change the scene in JavaFX without resizing the window

I'm trying to change the scene on JavaFX without changing the window size But when I set stage setScene(scene2); As the window size decreases, I want to maximize both scenes I tried stage Stage after setscene (scene2) setMaximized(true); But the result is the same

How can I solve 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 fadeout / fadein occurs and (here I want to maximize):

Solution

Replacing the root cause of an existing scene may be better than creating 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 need to replace the scene, for some reason, you can set the size of the new scene to be 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();
    }
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>