Change scene in full screen JavaFX

2019-07-19 17:22发布

问题:

I'm new to JavaFX. I have my main and secondary scenes; when I change from the first scene to the second one, the window's bar becomes visible. How can I fix that?

Here is my code

public class ProyectoTeoriaBD1 extends Application {

Stage primaryStage;

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

@Override
public void start(final Stage primaryStage) {

    this.primaryStage = primaryStage;
    GridPane gp = new GridPane();
    gp.setHgap(10);
    gp.setVgap(10);
    gp.setPadding(new Insets(25,25,25,25));

    Scene firstScene = new Scene(gp);
    Button b = new Button("Change Scene");        
    gp.add(b,1,1);
    primaryStage.setScene(firstScene);
     primaryStage.setFullScreen(true);
    primaryStage.show();



   b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
           GridPane gp = new GridPane();
           Scene secondScene = new Scene(gp);
           Text txtSecond = new Text("Second Scene");
           gp.add(txtSecond, 1, 1);
           primaryStage.setScene(secondScene);
           primaryStage.setFullScreen(false);
           primaryStage.setFullScreen(true);

        }
    });


}

}

回答1:

A full runnable testable code could be helpful. Also provide your system environment details. I have tested your code below (try yourself) which works on my windows 7 64 bit with JavaFX version 2.2.0.
(I will update my answer as you provide more details and lastly welcome to stackoverflow!)

Update: Ok I guess your primary stage was in full screen mode initially. In that case you need to toggle full screen mode. See below.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Test extends Application {
    private Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setFullScreen(true);
        Button btn = new Button("Login");
        btn.setOnAction(loginClienteHandler());

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("JavaFX version: " + com.sun.javafx.runtime.VersionInfo.getRuntimeVersion());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public EventHandler loginClienteHandler() {
        EventHandler evh = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                primaryStage.setScene(new Scene(VBoxBuilder.create().children(new Text("text")).build()));
                primaryStage.sizeToScene();
                primaryStage.setFullScreen(false);
                primaryStage.setFullScreen(true);
            }
        };
        return evh;
    }

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


回答2:

I am making a game as well, and I needed fullscreen functionality. The way I solved it was by:

1: using primaryStage.setFullScreen(true); to enable fullscreen of the main scene.

2: when changing to another scene, apply primaryStage.setFullScreen(true); again directly after setting the new scene.

My example:

    public void start(Stage primaryStage) {

            // Start of program
            setUpGame();

            // MENU
            // Setting up menu stage
            primaryStage.setTitle("My cool game");
            primaryStage.setResizable(false);
            primaryStage.setFullScreen(true);
            primaryStage.setFullScreenExitHint("");

            // Setting up grid pane
            mod.getMenuGrid().setAlignment(Pos.CENTER);
            mod.getMenuGrid().setHgap(10);
            mod.getMenuGrid().setVgap(10);
            mod.getMenuGrid().setPadding(new Insets(25, 25, 25, 25));

            ... More code until I'm changing scene ... 
    }

    private void startGame(Stage primaryStage) {
        setNames();
        createPlayers();
        setUpGameScene();
        primaryStage.setScene(mod.getGameScene());
        primaryStage.setFullScreen(true);
    }

I don't know if this is the best way around it, but at least it's simple and it works.You just have to apply the line of code consistently when changing scenes.