简体   繁体   English

在JavaFX中更改场景而不调整窗口大小

[英]Change Scene without resize window in JavaFX

I'm trying to change Scenes on JavaFX, but without change the window size. 我正在尝试更改JavaFX上的场景,但不改变窗口大小。 However, when I set stage.setScene(scene2); 但是,当我设置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); 我试着stage.setMaximized(true)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): 然后fadeOut / fadeIn发生并且(在这里我想保持最大化): 在此输入图像描述

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. 在过去的4-5个小时里,我一直在处理类似于你的问题,并给出@ James_D的答案,我看到了简单修复场景变化的调整大小问题的方法。 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. 这个答案仍然直接取决于@James_D,但我觉得没有明确解释发生了什么,所以我发布的内容比帮助任何遇到这个线程的人更了解为什么第二个解决方案@James_D提供(即更改场景,而不是场景的根)工作。

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. 基本上,要解决问题,您需要做的就是在创建场景时设置width和height属性,如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM