按代码关闭fxml窗口

按代码关闭fxml窗口

本文介绍了按代码关闭fxml窗口,javafx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to close the current fxml window by code in the controller

如何在fxml中实现此功能?我试过

how to implement this in fxml? I tried

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();
}

但它不起作用!

非常感谢所有帮助。谢谢!

All help will be greatly appreciated. Thanks!

推荐答案


  1. 给你的关闭按钮一个fx:id,如果你还没有: < Button fx:id =closeButtononAction =#closeButtonAction>

  2. 在您的控制器类中:

  1. give your close button an fx:id, if you haven't yet: <Button fx:id="closeButton" onAction="#closeButtonAction">
  2. In your controller class:

@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();
}


这篇关于按代码关闭fxml窗口,javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:43