Closing a Stage from within its controller

后端 未结 2 798
情话喂你
情话喂你 2020-12-17 19:04

I need a way to close a Stage from within itself by clicking a Button.

I have a main class from which I create the main stage with a scene.

2条回答
  •  长情又很酷
    2020-12-17 19:37

    You can derive the stage to be closed from the event passed to the event handler.

    new EventHandler() {
      @Override public void handle(ActionEvent actionEvent) {
        // take some action
        ...
        // close the dialog.
        Node  source = (Node)  actionEvent.getSource(); 
        Stage stage  = (Stage) source.getScene().getWindow();
        stage.close();
      }
    }
    

提交回复
热议问题