如何从javafx中的事件处理程序返回结果? [英] How to return result from event handler in javafx?

查看:386
本文介绍了如何从javafx中的事件处理程序返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从javafx中的事件处理程序返回结果?我有下面的代码,以及如何将数据从事件返回到函数showPrompt?是否有可能恢复事件功能的数据?

How to return result from event handler in javafx? I have bellow code, and how to return data from event to function showPrompt? Is it possible to recover the data for the function of the event?

public static String showPrompt(String title, String defValue){
          final Stage dlgStage = new Stage();
          TextField txtPromptValue = new TextField(defValue);

          Button btnOk = new Button("Ok");
          Button btnCancel = new Button("Cancel");
          btnOk.setOnAction(new EventHandler<ActionEvent>(){
              @Override
              public void handle(ActionEvent arg0) {
                  //How to return data from event to function?
                  dlgStage.close();
              }
          });
          btnCancel.setOnAction(new EventHandler<ActionEvent>(){
              @Override
              public void handle(ActionEvent arg0) { 
                  //How to return data from event to function?
                  dlgStage.close();
              }
          });
          //
          Label lblTitle = new Label(title);
          lblTitle.setFont(Font.font("Amble CN", FontWeight.NORMAL, 14));
          //
          VBox vbox = new VBox(lblTitle,txtPromptValue,btnOk,btnCancel);
          vbox.setAlignment(Pos.CENTER);
          vbox.setMinSize(300, 200);
          //
          Scene dlgScene = new Scene(vbox);
          //
          dlgStage.setScene(dlgScene);
          dlgStage.initStyle(StageStyle.UNDECORATED);
          dlgStage.initModality(Modality.APPLICATION_MODAL);
          dlgStage.setMinWidth(300);
          dlgStage.setMinHeight(200);
          dlgStage.show();       

}

推荐答案

简短的回答是你无法返回一个值。

The short answer is you can't return a value.

此代码名称为回调

new EventHandler<ActionEvent>(){
    @Override
    public void handle(ActionEvent arg0) {
        dlgStage.close();
    }
}

回调没有返回类型,如您所见上面的示例,它是 void

Callbacks have no return type, as you can see in the example above, it is void.

回调是您传递的方法另一个方法的参数。另一种方法会在需要时调用回调方法。这意味着回调是异步的。
在您的示例中,当您按下按钮时,它会调用回调

Callbacks are methods that you pass as an argument to another method. The other method will call you callback method when it wants. This means that callbacks are asynchronous.
In your example, it calls the callback when you press the button.

总之,你不能使用 return 从它返回。

In conclusion, you can't return from it using return.

您可以通过回调调用方法,并将返回值发送给它作为参数

You can call a method from your callback and sent your return value to it as an argument.

示例:

btnCancel.setOnAction(
   new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent arg0) {
            YourClass.setReturnValue("This is button Cancel");
            dlgStage.close();
        }
    }
});

其中 setReturnValue 是属于<的方法code> YourClass 或它的一个实例,以便它将零售您的退货价值。

Where setReturnValue is a method belonging to YourClass or an instance of it so it will retail your returned value.

另一种方式更好方法是创建一个扩展 Stage 的类。同样在 showPrompt 方法中,您必须使用 showAndWait()或类似方法来阻止执行。

Another way better approach would be to create a class that extends Stage maybe. Also in your showPrompt method you will have to block execution using showAndWait() or similar.

总之,您无法从一种方法创建整个提示

In conclusion, you can't create your entire Prompt from just one method.

这篇关于如何从javafx中的事件处理程序返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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