全屏中的JavaFX切换场景 [英] JavaFX switch scene in Fullscreen

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

问题描述

我想使用Next-Button在Fullscreen中切换我的JavaFX应用程序的场景。但是,如果我点击该按钮,它会在一秒钟内从全屏切换到窗口并返回全屏。如何避免这种情况并保持全屏模式?

I want to switch the scenes of my JavaFX application in Fullscreen with a "Next"-Button. But if I click on that Button it switches from fullscreen to windowed and back to fullscreen within a second. How can I achieve to avoid that and stay in fullscreen mode?

一些相关的片段:

Application.java :

Application.java:

public class Application extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLMain.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
        stage.setFullScreen(true);
        stage.setTitle("AppName");

    }

    public static void main(String[] args) {
        launch(args);
    }

}

FXMLMainController.java:

FXMLMainController.java:

@FXML
private void handleBtnNext(ActionEvent event) throws Exception{
    Stage stage; 
    Parent root;
    if(event.getSource()==btnNext){
        //get reference to the button's stage         
        stage=(Stage) btnNext.getScene().getWindow();
        //load up OTHER FXML document
        root = FXMLLoader.load(getClass().getResource("FXMLOptions.fxml"));
    }
    else{
        stage=(Stage) btnNext.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("FXMLMain.fxml"));

    }
    //create a new scene with root and set the stage
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    stage.setFullScreen(true);
}


推荐答案

该行为,应用程序的位置切换场景时弹出全屏模式,很奇怪(在Java 8u60,OS X 10.11.3上也适用于我)。它可能是错误

That behavior, where the app pops out of full screen mode when you switch scenes, is weird (it happens for me too on Java 8u60, OS X 10.11.3). It may be a bug.

要解决它,您可以重复使用相同的舞台和场景并调整场景的根,而不是更改场景本身。

To work-around it, you can just reuse the same stage and scene and adjust the root of your scene, rather than changing the scene itself.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class FullScreenScenes extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Button next1 = new Button("Show Scene 2");
        StackPane layout1 = new StackPane(next1);
        layout1.setStyle("-fx-background-color: palegreen;");

        Button next2 = new Button("Show Scene 1");
        StackPane layout2 = new StackPane(next2);
        layout2.setStyle("-fx-background-color: paleturquoise;");

        Scene scene = new Scene(layout1);

        next1.setOnAction(event -> scene.setRoot(layout2));
        next2.setOnAction(event -> scene.setRoot(layout1));

        stage.setScene(scene);
        stage.setFullScreen(true);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于全屏中的JavaFX切换场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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