场景改变后保持舞台最大化 [英] Keep stage maximised after scene change

查看:120
本文介绍了场景改变后保持舞台最大化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下代码更改舞台上的场景时,舞台会更改大小.但是,右上角用于最大化/最小化窗口的按钮表示该阶段仍处于最大化状态,即使显然不是.

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

因为这是特定于操作系统的映像,所以我正在将Windows 10与JDK 8 u112和JavaFX 8配合使用,并将e(fx)clipse插件用于eclipse

推荐答案

而不是替换场景,而是使用相同的场景并替换其根:

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

primaryStage.getScene().setRoot(scrollPane);

这篇关于场景改变后保持舞台最大化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆