简体   繁体   English

如何在JFrame中捕获JDialogs窗口关闭事件

[英]How to capture a JDialogs window closing event in a JFrame

I have a major JFrame running my application. 我有一个运行我的应用程序的主要JFrame。 It calls a JDialog class to perform a utility function, after performing its function, the user closes the dialog. 它调用JDialog类以执行实用程序功能,在执行其功能之后,用户关闭对话框。 I want the JFrame to detect that the JDialog has been closed. 我希望JFrame检测到JDialog已关闭。 In other words how do I fire an event from the dialog when it's closed, and then catch it on the JFrame. 换句话说,当对话框关闭时,如何从对话框中触发事件,然后在JFrame上捕获该事件。 Thank you. 谢谢。

You can implement a WindowListener and add it to the JDialog. 您可以实现WindowListener并将其添加到JDialog。 The logic you seek after the JDialog is closed can be placed in the windowClosed or windowClosing method of the WindowListener. 关闭JDialog之后寻求的逻辑可以放在WindowListener的windowClosed或windowClosing方法中。 See the Window Listener Tutorial at Oracle for more information on how to implement this listener. 有关如何实现此侦听的更多信息,请参见Oracle的窗口侦听器教程 Below is an example using the WindowAdapter class (empty implementation of a WindowListener) with the windowClosed method overridden: 下面是一个使用WindowAdapter类(WindowListener的空实现)并覆盖windowClosed方法的示例:

myJDialog.addWindowListener(new WindowAdapter(){
    @Override
    public void windowClosed(WindowEvent e){
        myJframe.doSomething();
    }
});

you should add a Window Listener to your JDialog 您应该将窗口侦听器添加到您的JDialog

jdialog.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
    System.out.println("jdialog window closed event received");
}

public void windowClosing(WindowEvent e) {
    System.out.println("jdialog window closing event received");
}
});

If you want the event be handled directly by a specific object, this object should implement the WindowAdapter. 如果希望事件直接由特定对象处理,则此对象应实现WindowAdapter。 for example your jFrame. 例如您的jFrame。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM