JavaFX 在全屏模式下改变场景

标签 java windows javafx fullscreen taskbar

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

import java.io.IOException;

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

----DesktopApplication类----

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

----MyLayout类----

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;

    }
    ...

关于JavaFX 在全屏模式下改变场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35673607/

相关文章:

java - 使用 Jsoup 解析 iframe 中的 YouTube 缩略图

java - PlayFramework - 如何上传文件/图像

windows - 稍后在脚本中使用 BAT 输入

java - 如何使用 java Streams API 转换 for 循环迭代 n 次以实现具有多个动态变化的计数的列表

java - 在confluence或jira中获取附件下载统计

windows - 为什么我无法为入站 HTTP 和 HTTP 添加防火墙豁免?

c# - 你能阻止 Windows TileWindows 函数将所有窗口恢复到它们以前的大小吗?

javafx - FXML 文档命名约定

maven - 如何将 Spring Boot 构建插件与 JavaFX 应用程序一起使用

java - 如何使用 JavaFX 中的边框更改形状属性?