按代码关闭fxml窗口,javafx [英] close fxml window by code, javafx

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

问题描述

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

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


我知道stage.close()或stage.hide( )在fx中执行此操作

I know stage.close() or stage.hide() do this in fx

如何在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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