2012-08-29 86 views
0

後我想只經過一定量的時間顯示此JOptionPane的按鈕「OK」(比方說例如5秒)。 (我的目的其實是爲了讓做完這個其他線程背後的一些工作線程)的JOptionPane:顯示「OK」按鈕一定量的時間

JOptionPane jop2 = new JOptionPane(); 
jop2.showMessageDialog(null, "Please wait 5s", "WAIT", JOptionPane.INFORMATION_MESSAGE); 

我不知道一切是如何做到這一點,你能提供給我一些代碼工作將回答這個問題嗎? 非常感謝您提前!

+0

用戶仍然可以在5秒內關閉「JOptionPane」(甚至在出現按鈕之前)。看起來'5秒'也是非常隨意的。關閉選項應該由線程完成觸發。 –

+1

_提供給我一些code_這不是這個網站的工作方式:它的想法是幫助_you_自己提供代碼:-) – kleopatra

回答

4

沒有具體的方法使用JOptionPane來做到這一點。您必須創建自定義對話框並在固定時間後顯示確定按鈕。您可以使用Swing timer的一次傳球。

ActionListener taskPerformer = new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { 
     button.setVisible(true); 
    } 
}; 

Timer timer = new Timer(0, taskPerformer); 
timer.setInitialDelay(5000); 
timer.setRepeats(false); 
timer.start(); 
+0

你能告訴我一小段代碼嗎?非常感謝您提前 – user1633807

-1

,你可以使用類似這樣停止碼5秒

 try { 
      Thread.sleep(5000); // do nothing for 5000 miliseconds (5 seconds) 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
+0

我只想在5秒後顯示此JOption窗格的按鈕,而不是停止整個代碼。 – user1633807

+2

阻擋EDT 5秒將確保用戶無法處理窗口。它可能也會確保你不能讀取對話框的內容(或其他討厭的副作用)。人們應該**從不**阻止美國東部時間 – Robin

1

這聽起來像你要找的是什麼SwingWorkerProgressMonitor的組合。 SwingWorker將執行長時間運行的任務(第5秒),並通知用戶使用ProgressMonitor的進度如何。下面是一個示例,展示如何使這兩個工作在一起: getting the cancel event of Java ProgressMonitor

當然,如果您確信要在工作完成後採用顯示繼續按鈕的方法,則以下示例這應該讓你開始正確的方向。您將使用SwingWorker來提醒您的Dialog長時間運行的後臺任務已完成。

import java.awt.*; 
import java.awt.Dialog.ModalityType; 
import java.awt.event.*; 
import javax.swing.*; 

public class TempProject extends Box{ 

    public TempProject(){ 
     super(BoxLayout.Y_AXIS); 

     //Contains the content of the Alert Dialog 
     Box info = Box.createVerticalBox(); 
     info.add(new Label("Please wait 5 seconds")); 
     final JButton continueButton = new JButton("Continue"); 
     info.add(continueButton); 

     //The alert to wait 5 seconds 
     final JDialog d = new JDialog(); 
     d.setTitle("WAIT"); 
     d.setModalityType(ModalityType.APPLICATION_MODAL); 
     d.setContentPane(info); 
     d.pack(); 

     //The action of the "Continue Button" 
     continueButton.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       d.dispose(); 
      } 
     }); 
     continueButton.setVisible(false); 

     //Thread That Does Work 
     final SwingWorker sw = new SwingWorker<Integer, Integer>() 
     { 
      protected Integer doInBackground() throws Exception { 
       //Do long running thread work here 
       int i = 0; 
       while (i++ < 100) { 
        System.out.println(i); 
        Thread.sleep(100); 
       } 
       return null; 
      } 

      @Override 
      protected void done(){ 
       // What to do when the long runnng thread is done 
       continueButton.setVisible(true); 
      } 


     }; 


     //Button to start the long running task 
     JButton button = new JButton("Click Me"); 
     button.addActionListener(new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       sw.execute(); 
       d.setVisible(true); 
      }}); 
     add(button); 
    } 


    public static void main(String args[]) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setContentPane(new TempProject()); 
       frame.setPreferredSize(new Dimension(500, 400)); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 


} 
相關問題