通过代码关闭fxml窗口,javafx

alfredo138923:

我需要通过控制器中的代码关闭当前的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();
}

但这不起作用!

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章