通过代码,javafx关闭fxml窗口

我需要通过控制器中的代码关闭当前的fxml窗口

我知道stage.close()或stage.hide()在fx中做到这一点

如何在fxml中实现呢?我试过了

private void on_btnClose_clicked(ActionEvent actionEvent) {

Parent root = FXMLLoader.load(getClass().getResource("currentWindow.fxml"));

Scene scene = new Scene(root);

Stage stage = new Stage();

stage.setScene(scene);

stage.show();

}

但这不起作用!

所有帮助将不胜感激。谢谢!

回答:

  1. 如果您还没有关闭按钮,请给它一个fx:id: <Button fx:id="closeButton" onAction="#closeButtonAction">
  2. 在您的控制器类中:

        @FXML private javafx.scene.control.Button closeButton;

@FXML

private void closeButtonAction(){

// get a handle to the stage

Stage stage = (Stage) closeButton.getScene().getWindow();

// do what you have to do

stage.close();

}

以上是 通过代码,javafx关闭fxml窗口 的全部内容, 来源链接: utcz.com/qa/428732.html

回到顶部