JavaFX 在全屏模式下改变场景

JavaFX changing Scenes on fullscreen mode

我对 JavaFX 有疑问。我创建了两个场景和切换按钮。 当我点击那个按钮时,我正在改变场景。但早些时候我将全屏设置为 true,按下按钮后,windows 任务栏会显示片刻。有没有办法在不显示此任务栏的情况下更改场景? 有代码:
----主要class----

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        DesktopApplication.launch(DesktopApplication.class);
    }
}

----桌面应用程序class----

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class DesktopApplication extends Application implements Runnable {

Scene firstScene;
Scene secondScene;
Scene scene;
public static Stage primaryStagePublic;

public DesktopApplication() {
}

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Title");
    primaryStage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.initStyle(StageStyle.UNDECORATED);

    int width = (int) Screen.getPrimary().getBounds().getWidth();
    int height = (int) Screen.getPrimary().getBounds().getHeight();

    HBox mainLayout = new HBox();
    mainLayout.getChildren().add(new Text("hello!"));
    MyLayout myLayout = new MyLayout(this);

    firstScene = new Scene(myLayout,width,height);
    secondScene = new Scene(mainLayout, width, height);

    scene = firstScene;

    primaryStage.setScene(scene);
    primaryStage.setFullScreen(true);
    primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    primaryStage.show();

    primaryStagePublic = primaryStage;

}

@Override
public void run() {
    Thread thread = new Thread() {
        public void run() {
            launch(DesktopApplication.class);
        }
    };

    thread.start();

    while (true) {

    }
}

public void swapScenes(Stage primaryStage){
    primaryStage.setScene(secondScene);
    primaryStage.setFullScreen(true);
    primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
}
}

----我的布局class----

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
public class MyLayout extends HBox{

private DesktopApplication desktopApplication;

public MyLayout(DesktopApplication desktopApplication) {
    this.desktopApplication = desktopApplication;
    init();
}

private void init(){

    this.setStyle("-fx-background-color: #f8ff7d;");
    Label text = new Label("testing");
    Button button = new Button("Button");
    button.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            desktopApplication.swapScenes(DesktopApplication.primaryStagePublic);
        }
    });
    this.getChildren().addAll(text, button);
}
}

我遇到了类似的问题,按照@James_D的建议解决了:不要替换整个场景,只替换根元素:

public void swapScenes(Parent newContent){
    stage.getScene().setRoot(newContent);
}

这需要稍微更改其余的初始化代码:

public class DesktopApplication extends Application implements Runnable {

    Parent myLayout;
    Parent mainLayout;
    Scene scene;
    public static Stage stage; // if possible make this private and non static

    public DesktopApplication() {
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Title");
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.initStyle(StageStyle.UNDECORATED);

        int width = (int) Screen.getPrimary().getBounds().getWidth();
        int height = (int) Screen.getPrimary().getBounds().getHeight();

        mainLayout = new HBox();
        mainLayout.getChildren().add(new Text("hello!"));
        myLayout = new MyLayout(this);

        scene = new Scene(myLayout,width,height);

        primaryStage.setScene(scene);
        primaryStage.setFullScreen(true);
        primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        primaryStage.show();

        primaryStagePublic = primaryStage;

    }
    ...

我个人解决了这个问题(经过几个月的观察),而不是做 primaryStage.setFullScreen(true),哪个小故障什么的,做 primaryStage.setMaximized(true) 以及 primaryStage.setWidth(var1)primaryStage.setHeight(var2).我关于为什么 setFullScreen 不起作用的假设是全屏独占模式中的错误。或者,只是没有足够的权限或其他原因,它会出错。