全屏中的JavaFX切换场景

时间:2016-03-16 04:24:39

标签: java javafx fullscreen fxml

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

一些相关的片段:

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:

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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:-3)

首先调整场景构建器视图(响应式响应屏幕大小)这是您的场景构建器视图问题再次调整视图(布局或Fxid)。