我有一個關於如何自動關閉對話框的問題,我可以用它在事件中提醒用戶並在延遲後自動關閉而不必關閉對話框?自動關閉對話框
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();
}
}
}
OP無法找到對JOptionPane的引用,也沒有JOptionPane.setVisible(false),[在大多數情況下是必需的,必須先嚐試](http://stackoverflow.com/a/18107432/714968),通過@ kleopatra – mKorbel