关闭可运行的JOptionPane [英] Closing a runnable JOptionPane

查看:117
本文介绍了关闭可运行的JOptionPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个可运行的窗口:

I have this Runnable window:

 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分钟内将其关闭,而不是单击确定"按钮?

Is it possible that I can have a timer here to close this within 1 or 2 minutes instead of clicking the OK button?

推荐答案

是的,技巧是在调用setVisible ...

Yes, the trick would be to get the Timer started before you call 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);
            }
        }
        );
    }

}

这篇关于关闭可运行的JOptionPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆