Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

java - 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: enter image description here

Then the fadeOut/fadeIn occurs and (It is here that I want to keep maximized): enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...