首页 文章

Javafx:在setOnAction中更改场景

提问于
浏览
2

我正在构建一个包含多个场景的JavaFX应用程序 . 在setOnAction事件中更改场景时,我的变量范围有问题 . 这是我的代码:

Stage myStage;

public Scene logInScene(){
   ... all the buttons / textFields

   createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
        public void handle(ActionEvent t){
              **this.getStage().allScene(createAccountPane1);**
        }
   }
}

public Stage getStage(){
      return this.myStage;
}

public void allScene(Pane p){
      this.myStage.setScene(p);
}

我在setOnAction函数中遇到错误 . “找不到符号”getStage() . 我知道这必须是一个范围问题,它不能识别该范围之外的任何变量/函数 . 我如何做到这一点,以便我可以改变?我试过通过变量,但这只会使我的代码混乱,我希望有一个更简单的方法 . 多谢你们!

2 回答

  • 2

    只要您保持一致性,您的代码就会起作用:

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Test extends Application{
        private Stage stage;
        @Override
        public void start(Stage primaryStage) throws Exception {
            stage = primaryStage;
            Scene scene = logInScene();
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public Scene logInScene(){
            Pane root = new Pane();
            Button createAccountButton = new Button("create account");
            createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
                public void handle(ActionEvent t){
                      stage.setScene(CreateAccountScene());
                }
           });
            root.getChildren().add(createAccountButton);
            return new Scene(root);
        }
        protected Scene CreateAccountScene() {
            VBox root = new VBox();
            Label userLabel = new Label("Insert the username:");
            final TextField userField = new TextField();
            Button createAccountButton = new Button("create account");
            createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
                public void handle(ActionEvent t){
                      System.out.println("Account for user " + userField.getText() + " was created succesfully");
                }
           });
            root.getChildren().addAll(userLabel,userField,createAccountButton);
            return new Scene(root);
        }
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    
  • 3

    这个问题已经解决了,但我认为值得澄清的是你的行失败了,因为 this 关键字引用了你正在实现的匿名EventHandler . 在Java中,使用 OuterClass.this 引用外部类实例 . 所以 OuterClass.this.getStage().allScene(createAccountPane1); 会奏效 .

    如果您正在寻找更漂亮的解决方案,一些编码器喜欢定义一个指向外部类实例的局部变量:

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

相关问题