JavaFX - 在初始化时需要帮助设置Stage

时间:2021-07-08 21:35:38

I keep getting a null reference exception when I try to set my stage to a variable upon initialization.

当我尝试在初始化时将我的阶段设置为变量时,我一直得到一个空引用异常。

my code.

public class ListController implements Initializable {
        private Stage thisStage;

        @Override
        public void initialize(URL location, ResourceBundle resources) {
            lblCount.setText("Hey"); //This works

            thisStage = (Stage) lblCount.getScene().getWindow(); //this doesn't
        }

        public ListController()
        {
            thisStage = (Stage) lblCount.getScene().getWindow(); //this doesn't
        }
}

I tried to use a constructor and that didn't work neither.

我试图使用构造函数,但两者都不起作用。

3 个解决方案

#1


0  

One way to get a reference to the stage is to create a method on your controller and set it:

获取对舞台的引用的一种方法是在控制器上创建一个方法并进行设置:

        FXMLLoader loader = new FXMLLoader();
        Parent node = loader.load(getClass().getResource("view.fxml").openStream());
        Scene scene = new Scene(node);

        Stage stage = new Stage();
        Controller controller = (Controller)loader.getController();

        controller.setPrimaryStage(stage);

        stage.show();

Then in your method you have a valid reference.

然后在您的方法中,您有一个有效的参考。

#2


0  

when you call the stage ya can pass the stage reference by a method like this

当你调用舞台时,ya可以通过这样的方法传递舞台参考

  Stage primaryStage = new Stage(); 
  FXMLLoader loader = new FXMLLoader();
  loader.setLocation(YourClass.class.getResource("yourpaht.fxml"));
  //here you change AnchorPane for your root Node
  rootLayout = (AnchorPane) loader.load();
  // Show scene.
  Scene scene = new Scene(rootLayout);
  primaryStage.setScene(scene);
  // Da acceso al programa principal.
  ListController controller = loader.getController();
  //method for passing the stage to the controller 
  controller.controlerPassing(this, primaryStage);
  // Show stage,
  primaryStage.show();

now the controler class looks can look like something like this

现在控制器类看起来像这样的东西

public class ListController implements Initializable {
      private Stage stage;
      pivate YourClass classs;

      public controlerPassing(YourClass classs, Stage stage){
        this.stage = stage;
        this.classs = classs;
      }

     @Override
     public void initialize(URL location, ResourceBundle resources) {
          lblCount.setText("Hey"); //This works
          //now you can do whatever you want whit the stage
          stage. do stuff........
     }
}

#3


-1  

This null reference can be solved if we find out the data types in the Stage class and use such initialization accordingly.

如果我们在Stage类中找到数据类型并相应地使用这样的初始化,则可以解决此null引用。

For example,

Stage myStage = null ; // this will raise a null pointer reference exception the possible solution can be to say:

Stage myStage = {" ", 0, ' ', etc } ; // as long as the class Stage has data members like String, int, char, etc

#1


0  

One way to get a reference to the stage is to create a method on your controller and set it:

获取对舞台的引用的一种方法是在控制器上创建一个方法并进行设置:

        FXMLLoader loader = new FXMLLoader();
        Parent node = loader.load(getClass().getResource("view.fxml").openStream());
        Scene scene = new Scene(node);

        Stage stage = new Stage();
        Controller controller = (Controller)loader.getController();

        controller.setPrimaryStage(stage);

        stage.show();

Then in your method you have a valid reference.

然后在您的方法中,您有一个有效的参考。

#2


0  

when you call the stage ya can pass the stage reference by a method like this

当你调用舞台时,ya可以通过这样的方法传递舞台参考

  Stage primaryStage = new Stage(); 
  FXMLLoader loader = new FXMLLoader();
  loader.setLocation(YourClass.class.getResource("yourpaht.fxml"));
  //here you change AnchorPane for your root Node
  rootLayout = (AnchorPane) loader.load();
  // Show scene.
  Scene scene = new Scene(rootLayout);
  primaryStage.setScene(scene);
  // Da acceso al programa principal.
  ListController controller = loader.getController();
  //method for passing the stage to the controller 
  controller.controlerPassing(this, primaryStage);
  // Show stage,
  primaryStage.show();

now the controler class looks can look like something like this

现在控制器类看起来像这样的东西

public class ListController implements Initializable {
      private Stage stage;
      pivate YourClass classs;

      public controlerPassing(YourClass classs, Stage stage){
        this.stage = stage;
        this.classs = classs;
      }

     @Override
     public void initialize(URL location, ResourceBundle resources) {
          lblCount.setText("Hey"); //This works
          //now you can do whatever you want whit the stage
          stage. do stuff........
     }
}

#3


-1  

This null reference can be solved if we find out the data types in the Stage class and use such initialization accordingly.

如果我们在Stage类中找到数据类型并相应地使用这样的初始化,则可以解决此null引用。

For example,

Stage myStage = null ; // this will raise a null pointer reference exception the possible solution can be to say:

Stage myStage = {" ", 0, ' ', etc } ; // as long as the class Stage has data members like String, int, char, etc