【发布时间】:2013-03-05 16:51:41
【问题描述】:
我有一个带有主 JFrame 的小型应用程序,它以模态方式打开 JDialog。在这个 JDialog 中,我启动了一个 javax.swing.Timer,如果 JDialog 关闭,它应该停止。
public Tasks() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
showTasks();
t = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Tasks.this.showTasks();
}
});
t.start();
}
但它只是继续运行。
我尝试为 JDialog 实现 WindowListener,但我覆盖了 windowClosed、windowClosing 甚至 windowDeactivated ... 无济于事
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("Closing");
t.stop();
}
如何在 JDialog 关闭时停止计时器?
【问题讨论】:
-
请编辑您的问题以包含一个sscce,以显示您的方法。
-
opening a JDialog as modal。确保在使对话框可见之前将 WindowListener 添加到对话框中。
标签: java swing timer jdialog windowlistener