如何在JavaFX中使用Callback进行任何事件?

时间:2014-05-02 14:45:22

标签: java javafx

我有类Dialog,它包含方法showPrompt(),这个方法有2个按钮“Ok”和“Cancel”。我想处理这些按钮上的事件。怎么样?以下是我的课程方法:

    public class Dialog {

  public static void 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 handle this event from extern code?
                      dlgStage.close();
                  }
              });
              btnCancel.setOnAction(new EventHandler<ActionEvent>(){
                  @Override
                  public void handle(ActionEvent arg0) {
                      //------How to handle this event from extern code?
                      dlgStage.close();
                  }
              });
              //
              Label lblTitle = new Label(title);
              //
              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();       
  }

}

0 个答案:

没有答案