Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

user interface - How can I keep my window size while switching scenes in Java FX?

I've simply been trying to make sense of why the window size (which I set to full screen) is changing when I change scenes, is there a way (i.e. a command) which can be issued once to let Java FX know not to change the size?

import javafx.application.Application;
import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

public class Main extends Application {

    Scene scene,scene_2;

    public static void main(String[] args) {

        launch(args);

    }

    @Override

    public void start(Stage Win_primary) throws Exception {

        Win_primary.setTitle("AN-0");



        Button start,exit_pro,back_to_start;

        start= new Button();

        start.setText("Start");

        Image exit_door = new Image("file:C:\Users\MyPc\Pictures\FX\vector-icons_05-128 (2).png");



        exit_pro= new Button ();
        exit_pro.setGraphic(new ImageView(exit_door));





          start.setStyle(

                "-fx-background-radius: 100em; " +

                "-fx-min-width: 200px; " +

                "-fx-min-height: 200px; " +

                "-fx-max-width: 200px; " +

                "-fx-max-height: 170px;" +

                "-fx-font-size: 20px"

        );


           exit_pro.setMaxSize(50, 50);
           exit_pro.setOnAction(e -> Win_primary.close());

        StackPane layout = new StackPane();
        layout.setStyle("-fx-background-color: #FFFFFF ;");
        layout.getChildren().add(start);
        layout.setAlignment(exit_pro, Pos.BOTTOM_CENTER);
         layout.getChildren().add(exit_pro);


        start.setOnAction(e -> {
             Win_primary.setScene(scene_2);

            });
        scene = new Scene(layout, 300, 250);


        //scene 2

        StackPane layout2 = new StackPane();
        back_to_start = new Button("Cancel");
        layout2.setStyle("-fx-background-color: #FFFFFF ;");
        layout2.getChildren().add(back_to_start);
        layout.setAlignment(back_to_start, Pos.BOTTOM_CENTER);
        scene_2 = new Scene (layout2,300,250);   
        back_to_start.setOnAction(e -> Win_primary.setScene(scene));


        Win_primary.setScene(scene);
        Win_primary.setMinHeight(700);
        Win_primary.setMinWidth(700);
        Win_primary.setMaximized(true);

        Win_primary.setFullScreen(true);

        Win_primary.show();

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same issue and ended up not changing the scene, but only the root of the scene. So instead of:

start.setOnAction(e -> {
 Win_primary.setScene(scene_2);
});

you would then write:

start.setOnAction(e -> {Win_primary.getScene().setRoot(layout2)});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...