2011-06-30 74 views

回答

8

你可以手動創建的JOptionPane,不帶靜電的方法:

JOptionPane pane = new JOptionPane("Your message", JOptionPane.INFORMATION_MESSAGE); 
JDialog dialog = pane.createDialog(parent, "Title"); 

則可以顯示對話框,並啓動一個定時器在十秒鐘後,以隱藏它。

0

使小框狀的JOptionPane,顯示它在線程和處理10秒後

2

我的Java是有些生疏,但你應該能夠只使用標準Timer類:

import java.util.Timer; 

int timeout_ms = 10000;//10 * 1000 
Timer timer = new Timer(); 
timer.schedule(new CloseDialogTask(), timeout_ms); 

//implement your CloseDialogTask: 

class CloseDialogTask extends TimerTask { 
    public void run() { 
    //close dialog code goes here 
    } 
} 
+5

擺動,優選使用javax.swing.Timer中代替這一個。 –

6

我試過這些答案,遇到了顯示對話框是阻塞呼叫的問題,所以計時器無法工作。下面解決這個問題。

 JOptionPane opt = new JOptionPane("Application already running", JOptionPane.WARNING_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}); // no buttons 
    final JDialog dlg = opt.createDialog("Error"); 
    new Thread(new Runnable() 
     { 
      public void run() 
      { 
      try 
      { 
       Thread.sleep(10000); 
       dlg.dispose(); 

      } 
      catch (Throwable th) 
      { 
       tracea("setValidComboIndex(): error :\n" 
        + cThrowable.getStackTrace(th)); 
      } 
      } 
     }).start(); 
    dlg.setVisible(true); 
1
// ===================== 
// yes = 0, no = 1, cancel = 2 
// timer = uninitializedValue, [x] = null 

public static void DialogBox() { 

    JOptionPane MsgBox = new JOptionPane("Continue?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION); 
    JDialog dlg = MsgBox.createDialog("Select Yes or No"); 
    dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
    dlg.addComponentListener(new ComponentAdapter() { 
     @Override 
     // ===================== 
     public void componentShown(ComponentEvent e) { 
     // ===================== 
     super.componentShown(e); 
     Timer t; t = new Timer(7000, new ActionListener() { 
      @Override 
      // ===================== 
      public void actionPerformed(ActionEvent e) { 
      // ===================== 
      dlg.setVisible(false); 
      } 
     }); 
     t.setRepeats(false); 
     t.start(); 
     } 
    }); 
    dlg.setVisible(true); 
    Object n = MsgBox.getValue(); 
    System.out.println(n); 
    System.out.println("Finished"); 
    dlg.dispose(); 
    } 
} 
+1

這可能會解決問題,但您應該解釋如何以及爲什麼解決這個問題,而不僅僅是代碼。 – Adam