Javafx: Change scene in setOnAction

后端 未结 2 2101
忘了有多久
忘了有多久 2021-01-21 18:11

I\'m building a JavaFX application with multiple Scenes. I have a problem with scope of variable when changing scenes within setOnAction event. This is my code:

         


        
2条回答
  •  遇见更好的自我
    2021-01-21 18:42

    This question has already been solved, but I think it's worth clarifying that your line fails because the this keyword refers to the anonymous EventHandler you are implementing. In Java, you reference the outer class instance with OuterClass.this. So OuterClass.this.getStage().allScene(createAccountPane1); will work.

    If you are looking for a prettier solution, some coders like to define a local variable that points to the outer class instance:

    final OuterClass self = this;
    createAccountButton.setOnAction(new EventHandler(){
            public void handle(ActionEvent t){
                  self.getStage().allScene(createAccountPane1);
            }
    }
    

提交回复
热议问题