当前位置: 首页 > 知识库问答 >
问题:

JavaFX在全屏模式下更改场景

蓬化
2023-03-14

我对JavaFX有问题。我创建了两个场景和开关按钮。当我点击那个按钮时,我正在改变场景。但之前我将fullscreen设置为true,按下按钮后,windows任务栏会显示片刻。有没有什么方法可以改变场景而不让这个任务栏可见呢?下面是代码:
-----main class----

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

共有1个答案

厉熠彤
2023-03-14

我也有过类似的问题,并像@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的新手。我有我的主要场景和次要场景;当我从第一个场景切换到第二个场景时,窗口的条形图变得可见。我该怎么解决呢?

  • }`我正在计划在同一舞台上使不同的部分成为自己的场景。如果有任何帮助,我将不胜感激。我正在使用NetBeans8.2。

  • 如何更改全屏窗口的场景并避免显示“按ESC退出全屏”消息? 我正在构建全屏桌面应用程序(触摸屏亭),所以我可以在开始时显示此消息,但现在总是当用户改变场景。 有两个问题: > 在全屏模式下更改场景时,窗口大小将减小。解决方案是切换全屏,但会显示该消息。(在全屏JavaFX中更改场景) “按ESC…”由于安全原因,无法禁用消息(https://forums.oracle.com/forums/thr

  • 我目前正在开发一个志愿者登录应用程序,需要防止任何试图篡改计算机的行为。首先,我很容易地将应用程序设置为全屏。然后我尝试将窗口的exit键组合设置为null,但在这种情况下JavaFX自动默认为escape键。我将有一个管理部分,该程序可以退出使用密码。是否有任何方法可以有效地拦截退出JavaFX应用程序全屏状态的任何可能方法,或者更好的方法是暂时挂起/锁定其他操作系统功能? 编辑——使用组合键。

  • 问题内容: 我有一个以全屏模式运行的应用程序,并且运行良好。现在,我需要添加一个简单的,未修饰的对话框,我遇到了麻烦。如果我 最大化 而不是全屏运行该应用程序,则对话框将按预期方式显示和运行。当我切换回全屏模式时,该对话框将不会显示。 该对话框扩展了JDialog,仅包含一个JSlider和几个按钮。它是未经装饰的, 不是模态的 。(我出于测试目的禁用了模式- 每次对话框阻止输入时强制退出应用程序

  • 编辑模型时使用整个屏幕。正常显示在 Navicat Data Modeler 应用程序的菜单和标题栏在这个模式时将会隐藏。移动你的滑鼠光标到屏幕的顶部,菜单和标题栏将会自动显示。 从菜单栏选择“查看”->“进入全屏幕”或简单地按 CTRL-COMMAND-F 来开始全屏模式。 当取消了全屏模式,Navicat Data Modeler 窗口将回复至之前的狀态。