2013-08-28 229 views
1

我有一個關於如何自動關閉對話框的問題,我可以用它在事件中提醒用戶並在延遲後自動關閉而不必關閉對話框?自動關閉對話框

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(); 
     } 
    } 
} 

回答

0

可能您需要的是使用JOptionPane.setVisible(false)。我還沒有嘗試過,但http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html底部的例子可以給你一個提示。

+0

OP無法找到對JOptionPane的引用,也沒有JOptionPane.setVisible(false),[在大多數情況下是必需的,必須先嚐試](http://stackoverflow.com/a/18107432/714968),通過@ kleopatra – mKorbel

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; 

...

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 

請注意,此代碼所做的消息將四捨五入的時間值,但我認爲這是對你有用的起點。

+0

非常棒!如何在禁用中設置JDialog x按鈕或如何刪除x按鈕?我試過了代碼,但是當我點擊x按鈕時,它停止運行。請幫忙。非常感謝。 –

+0

在我的測試中,按下窗口關閉符號的方式就像按下OK鍵一樣。然而調用'd.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);'應該完全禁止✖。如果沒有提供的默認操作選項適合,您可以添加一個WindowListener來決定如何對windowClosing執行操作。 – Holger

+0

可替代地,你可以通過執行'd.dispose();刪除所有的窗口控件。 d.setUndecorated(真); d.getRootPane()。setBorder(BorderFactory.createEtchedBorder());'創建完對話框後。 – Holger