JavaFX在全屏模式下更改场景 [英] JavaFX changing Scenes on fullscreen mode

查看:112
本文介绍了JavaFX在全屏模式下更改场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

----主类----

I have problem with JavaFX. I created two scenes and switch button. When I click that button I'm changing scene. But earlier i set fullscreen on true and after I pressed the button, windows taskbar shows for a moment. Is there any way to change scenes without having this taskbar visible? There is the code:
----Main class----

import java.io.IOException;

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

---- DesktopApplication类----

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

---- MyLayout class ----

----MyLayout 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建议:不要整个场景,但只有根元素:

I had a similar issue and solved it like @James_D suggested: Do not replace the scene as a whole, but only the root element:

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

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

This requires changing the rest of the initialisation code a bit:

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在全屏模式下更改场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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