尝试使用ScheduledExecutorService.就像是:
JDialog dialog = pane.createDialog("Error");
dialog.addWindowListener(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
ScheduledExecutorService sch = Executors.newSingleThreadScheduledExecutor();
sch.schedule(new Runnable() {
public void run() {
dialog.setVisible(false);
dialog.dispose();
}
}, 10, TimeUnit.SECONDS);
dialog.setVisible(true);
[编辑]
关于camickr注释,文档中没有提到在事件调度线程上执行ScheduledExedcutorService.因此最好使用swing.Timer
JDialog dialog = pane.createDialog("Error");
dialog.addWindowListener(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
Timer timer = new Timer(10000, new ActionListener() { // 10 sec
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.start();
dialog.setVisible(true);