Closing a runnable JOptionPane

前端 未结 1 393
既然无缘
既然无缘 2020-11-28 16:01

I have this Runnable window:

 EventQueue.invokeLater(new Runnable(){
    @Override
    public void run() {
        op = new JOptionPane(\"Breaktime\",JOption         


        
相关标签:
1条回答
  • 2020-11-28 16:51

    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);
                }
            }
            );
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题