java dialog 关闭_java – 等待jdialog关闭

同样,关键是对话模式与否?

如果它是模态的,则不需要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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值