1

我有一个关于JOptionPane如何自动关闭对话框的问题,我可以使用该对话框在事件中提醒用户并在延迟后自动关闭而无需关闭对话框?

public class ShutdownComputer
{  
    public static void main(String[]args)  
    {  
        int wholeTimeBeforeShutdown=0;  

        int hour=0;  
        int minute=0;  
        int second=0;  

        try  
        {  
            String a=JOptionPane.showInputDialog(null,"HOUR","HOUR BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  
            String b=JOptionPane.showInputDialog(null,"MINUTE","MINUTE BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  
            String c=JOptionPane.showInputDialog(null,"SECOND","SECOND BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  

            if((a==null)||(a.equals("")))  
            {  
                a="0";  
            }  

            if((b==null)||(b.equals("")))  
            {  
                b="0";  
            }  

            if((c==null)||(c.equals("")))  
            {  
                c="0";  
            }  

            int e=Integer.parseInt(a);  
            int f=Integer.parseInt(b);  
            int g=Integer.parseInt(c);  

            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+((e*60)*60);  
            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(f*60);  
            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(g);  

            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown*1000;  


            Thread.sleep(wholeTimeBeforeShutdown);  

            JOptionPane.showMessageDialog(null, "You only have less than 2 minutes left"); 

            Runtime.getRuntime().exec("shutdown -r -f -t 120");  
        }  
        catch(Exception exception)  
        {  
            exception.printStackTrace();  
        }  
    }  
}
4

2 回答 2

1
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;

…</p>

    final String msg="Shutdown\nWill happen automatically in "; 
    final JOptionPane op=new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);
    final JDialog d=op.createDialog("Shutdown");
    d.setModal(true);
    // deadline in three hours:
    final long deadline=System.nanoTime()+TimeUnit.HOURS.toNanos(3);
    ActionListener updater=new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        long left=deadline-System.nanoTime();
        if(left<=0) {
          d.dispose();
          return;
        }
        String time;
        long t=TimeUnit.NANOSECONDS.toHours(left);
        if(t>0) time=t>1? t+" hours": "one hour";
        else {
          t=TimeUnit.NANOSECONDS.toMinutes(left);
          time=t>1? t+" minutes": "one minute";
        }
        op.setMessage(msg+time);
      }
    };
    updater.actionPerformed(null);//update initial message
    d.pack(); // resize dialog for the updated message
    final Timer t=new Timer(1000, updater);
    t.setInitialDelay(0);
    t.setRepeats(true);
    t.start();
    d.setVisible(true);
    t.stop();
    // do your action

请注意,此代码发出的消息将舍入时间值,但我认为这对您来说是一个可用的起点。

于 2013-08-28T17:14:44.873 回答
0

Probably what you need is to use JOptionPane.setVisible(false). I haven't tried that but the example at the bottom of http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html can give you a hint.

于 2013-08-28T16:59:12.633 回答