我有这个可运行窗口:
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
op = new JOptionPane("Breaktime",JOptionPane.WARNING_MESSAGE);
dialog = op.createDialog("Break");
dialog.setAlwaysOnTop(true);
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
});
我是否可以在此处设置一个计时器,在 1 或 2 分钟内关闭它,而不是单击“确定”按钮?
是的,诀窍是得到Timer
在你打电话之前开始setVisible
...
public class AutoClose02 {
public static void main(String[] args) {
new AutoClose02();
}
private Timer timer;
private JLabel label;
private JFrame frame;
public AutoClose02() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JOptionPane op = new JOptionPane("Breaktime", JOptionPane.WARNING_MESSAGE);
final JDialog dialog = op.createDialog("Break");
dialog.setAlwaysOnTop(true);
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// Wait for 1 minute...
timer = new Timer(60 * 1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
timer.setRepeats(false);
// You could use a WindowListener to start this
timer.start();
dialog.setVisible(true);
}
}
);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)