2014-04-30 17 views
1

我需要創建一個JDialog或JFrame類(它並不特別重要,只要它是圖形界面) - 可用於靜態命令從actionPerformed(ActionEvent)處理程序中調用,並會阻止該進程,直到用戶從用戶界面提供它們的選擇。(Java Swing)Stalling actionPerformed命令直到對話框返回

顯然這是可能的,因爲這正是JOptionPane創建的對話框所做的。但是,當我嘗試停止線程直到用戶完成選擇過程時,界面無法正確顯示。

這裏是一個我遇到的麻煩我的代碼部分:

public static Results queryForResults(String message, int propertyOne, 
     int propertyTwo){ 

    MyCustomDialog mcd = new MyCustomDialog(message, propertyOne,propertyTwo); 
    mcd.show(); 
    // complete() is a boolean property of my MyCustomDialog class that turns 
    // true when the interface has been completed. 
    while(!mcd.complete()){ 
     synchronized(mcd){ 
      try{ 
       mcd.wait(10000L); 
      } catch(InterruptedException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
    } 
    mcd.dispose(); 
    // My MyCustomDialog class has a getResults() property which returns 
    // a Results object (i.e. another custom class I've made to contain 
    // the selections made by the user.) 
    return mcd.getResults(); 
} 

出現對話框外框,但它的內部沒有。它們看起來像是對話框下面屏幕上的任何內容的屏幕截圖。我覺得這是行不通的,因爲我不應該用wait命令拖延Swing Event線程。你是怎麼做到的?

回答

2

不需要一個while循環。一個modal JDialog將導致執行停止在ActionLIstener中,直到關閉對話框。

另外,不要使用show()方法來顯示對話框。您應該使用setVisible(true)方法。

相關問題