同样,关键是对话模式与否?
如果它是模态的,则不需要WindowListener,因为您将知道对话框已被处理,因为代码将在对话框的setVisible(true)调用之后立即恢复.即,这应该工作:
projectDialog = new FilePathDialog();
projectDialog.setVisible(true);
doWork(); // will not be called until the dialog is no longer visible
另一方面,如果它没有模式,则WindowListener应该可以工作,并且您可能在此处未显示的代码中遇到另一个问题,并且您将要发布sscce以供我们分析,运行和修改.
编辑gpeche
请查看此SSCCE,它显示3种类型的默认关闭参数将触发窗口监听器:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogClosing {
private static void createAndShowGui() {
JFrame frame = new JFrame("DialogClosing");
JPanel mainPanel = new JPanel();
mainPanel.add(new JButton(new MyAction(frame,JDialog.DISPOSE_ON_CLOSE,"DISPOSE_ON_CLOSE")));
mainPanel.add(new JButton(new MyAction(frame,JDialog.HIDE_ON_CLOSE,"HIDE_ON_CLOSE")));
mainPanel.add(new JButton(new MyAction(frame,JDialog.DO_NOTHING_ON_CLOSE,"DO_NOTHING_ON_CLOSE")));
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MyAction extends AbstractAction {
private JDialog dialog;
private String title;
public MyAction(JFrame frame,int defaultCloSEOp,final String title) {
super(title);
dialog = new JDialog(frame,title,false);
dialog.setDefaultCloSEOperation(defaultCloSEOp);
dialog.setPreferredSize(new Dimension(300,200));
dialog.pack();
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
System.out.println(title + " window closed");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println(title + " window closing");
}
});
this.title = title;
}
@Override
public void actionPerformed(ActionEvent arg0) {
dialog.setVisible(true);
}
}