Keep stage maximised after scene change

血红的双手。 提交于 2021-02-19 03:42:31

问题


When I change scenes on my stage with the following code, my stage changes size. However the button in the top right to maximize/minimize the windows says that the stage is still maximized even though it is clearly not.

How am I able to keep the stage maximized when a scene change happens?

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Program2 extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            StackPane p = new StackPane();
            primaryStage.setTitle("Chart Application");
            Label loader = new Label("Loading...");
            loader.setGraphic(new ImageView(new Image("https://media.giphy.com/media/FmcNeI0PnsAKs/giphy.gif")));
            loader.setFont(new Font(35));
            p.setStyle("-fx-background: #FFFFFF;");
            p.getChildren().add(loader);
            StackPane.setAlignment(loader, Pos.CENTER);

            primaryStage.setScene(new Scene(p));
            primaryStage.setMaximized(true);

            Task<VBox> task = new Task<VBox>() {
                @Override
                public VBox call() {
                    VBox result = new VBox();
                    for(int i = 0; i < 50000; i++) { //Here simply for small delay
                        result.getChildren().add(new Label(Integer.toString(i)));
                    }
                    return result ;
                }
            };

            task.setOnSucceeded(e -> {
                VBox result = task.getValue();

                ScrollPane scrollPane = new ScrollPane();
                scrollPane.setContent(result);
                scrollPane.setStyle("-fx-background: #FFFFFF;");

                primaryStage.setScene(new Scene(scrollPane));
            });

            new Thread(task).start();

            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

As this is operating system specific I image I am using Windows 10 with JDK 8 u112 and JavaFX 8 with the e(fx)clipse plugin for eclipse


回答1:


Instead of replacing the scene, use the same scene and replace its root:

primaryStage.getScene().setRoot(scrollPane);


来源:https://stackoverflow.com/questions/41199343/keep-stage-maximised-after-scene-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!