Set Height and Width of Stage and Scene in javafx

后端 未结 3 1980
鱼传尺愫
鱼传尺愫 2021-02-07 08:37
  • I develop one javafx application.
  • In my application there are two scenes and one stage.
  • In application the height and width for both scenes are same or c
3条回答
  •  不知归路
    2021-02-07 09:13

    That is because the Stage adapts its size to the scene, unless explicitly instructed diferently...

    To here is one solution:

    stage.setScene(scene2);
    stage.setHeight(1000);
    stage.setWidth(1000);
    

    And a sample application:

    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.AnchorPane;
    import javafx.stage.Stage;
    
    public class Test extends Application {
    
        @Override
        public void start(final Stage stage) throws Exception {
    
            AnchorPane anchor1 = new AnchorPane();
            final Scene scene1 = new Scene(anchor1, 250, 250);
            Button boton1 = new Button();
            anchor1.getChildren().add(boton1);
    
            AnchorPane anchor2 = new AnchorPane();
            final Scene scene2 = new Scene(anchor2, 500, 500);
            Button boton2 = new Button();
            anchor2.getChildren().add(boton2);
    
            boton2.setOnAction(new EventHandler() {
    
                @Override
                public void handle(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    stage.setScene(scene1);
                    stage.setHeight(1000);
                    stage.setWidth(1000);
                }
            });
    
            boton1.setOnAction(new EventHandler() {
    
                @Override
                public void handle(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    stage.setScene(scene2);
                    stage.setHeight(1000);
                    stage.setWidth(1000);
                }
            });
            stage.setScene(scene1);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

提交回复
热议问题