在javafx中设置舞台和场景的高度和宽度 [英] Set Height and Width of Stage and Scene in javafx

查看:207
本文介绍了在javafx中设置舞台和场景的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我开发了一个javafx应用程序.
  • 在我的应用程序中,有两个场景和一个阶段.
  • 在应用程序中,两个场景的高度和宽度相同或恒定.
  • 因此,根据我的研究,场景的高度和宽度保持不变,这在构造函数中已提及,但场景会根据舞台的高度和宽度进行自我调整.
  • 当我的午餐应用程序的舞台高度和宽度与场景的恒定高度和宽度不同时,场景将根据舞台进行调整.
  • 但是在运行时当我应用第二个场景时,场景无法根据舞台的高度和宽度进行调整.场景的高度和宽度保持不变.

  • 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 constant.
  • so as per my research the height and width for scene remain constant which mention in the constructor but the scene adjust itself with height and width of stage.
  • when i lunch application with the height and width of stage which is different than the constant height and width of scene then scene adjust with stage.
  • but when at the run time when i apply the 2nd scene then scene is not adjust with height and width of stage.the height and width of scene remain constant.

那么有什么解决办法吗?

so any solution?

推荐答案

据我了解,上面发布的问题.我认为舞台足够好来设置首选的高度和宽度,因为侦听器会获得更新的请求以应用于窗口大小.但是它有一些局限性,如果您最大化或最小化javaFX屏幕,并尝试导航到其他屏幕,则其他屏幕将具有相同的窗口大小,但是场景内容将失真为它的默认高度和宽度,例如,登录和Javafx中的家庭场景(所有场景都是使用fxml创建的). Login.fxml由其控制器初始化.正如您提到的那样,场景是在构造函数中初始化的,因此它必须是相关fxml的控制器(到目前为止,FXML已与控制器紧密耦合).您将在构造函数本身中设置场景大小(高度和宽度).

As I understand the problem above posted. I think the stage is good enough to set the preferred height and width as per the listener get the newer request to apply on the windows size. But it has some limitations, if you maximize or minimize the javaFX screen and will try to navigate to other screen then other screen will be having same window size but the scene content will distorted into the default height and width of it, e.g take a login and home scene in javafx (all scene is screated with fxml). Login.fxml is initialized by its controller. As you have mentioned that scene is initialized in constructor, so it must be the controller of the related fxml(as of now FXML is tight coupled with controller). You are going to set the scene size(height & width) in constructor itself.

1.)LoginController for login.fxml

1.) LoginController for login.fxml

 import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;

    import java.io.IOException;

    class LoginController  {

        private Stage stage;
        private Scene scene;
        private Parent parent;
        @FXML  
        private Button gotoHomeButton;        

        public LoginController()  throws Exception {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/login.fxml"));
            fxmlLoader.setController(this);
            try {
                parent = (Parent) fxmlLoader.load();
                // set height and width here for this login scene
                scene = new Scene(parent, 1000, 800);
            } catch (IOException ex) {
                System.out.println("Error displaying login window");
                throw new RuntimeException(ex);
            }
        }

        // create a launcher method for this. Here I am going to take like below--
        public void launchLoginScene(Stage stage) {
           this.stage = stage;
            stage.setScene(scene);
            stage.setResizable(true);

            stage.widthProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
                    setCurrentWidthToStage(number2); 
                }
            });

            stage.heightProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
                    setCurrentHeightToStage(number2);
                }
            });

            //Don't forget to add below code in every controller
            stage.hide();
            stage.show();

        }

         @FXML
        public void authenticateUser(ActionEvent actionEvent) { 

        // write your logic to authenticate user


         // 
         new HomeController().displayHomeScreen(stage);

        } 

        private void setCurrentWidthToStage(Number number2) {
            stage.setWidth((double) number2);
        }

        private void setCurrentHeightToStage(Number number2) {
            stage.setHeight((double) number2);
        }
    }

2.)与HomeController相同-

2.) Same for HomeController --

public class HomeController {

    private Parent parent;
    private Stage stage;
    private Scene scene;


    public HomeController (){
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
        fxmlLoader.setController(this);
        try {
             parent = (Parent) fxmlLoader.load();
                // set height and width here for this home scene
                scene = new Scene(parent, 1000, 800);
        } catch (IOException e) {
         // manage the exception
        }
    }

    public void displayHomeScreen(Stage stage){
        this.stage = stage;
        stage.setScene(scene); 

        // Must write
        stage.hide()
        stage.show();
    }
}

3.)主班

import javafx.application.Application;

import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        new LoginController().launchLoginScene(primaryStage);
    }


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

只需尝试在每个控制器中将Stage.hide()放在Stage.show()之前.希望对您有所帮助.

Just try to put Stage.hide() before Stage.show() in every controller. I hope this will help you out.

这篇关于在javafx中设置舞台和场景的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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