2012-04-24 48 views
2

我學會了使用NotifyDescriptor創建彈出對話框。我設計了一個帶有兩個大按鈕的JPanel,它們的讀數爲PURCHASECASHOUT,我使用的代碼顯示底部的另外兩個按鈕,它們的讀數爲YesNo。我不想讓NotifyDescriptor將自己的按鈕放在屏幕上。我只是想讓我的按鈕在那裏,當我點擊一個自定義按鈕時,彈出窗口會關閉並存儲這個值(就像當點擊時點擊了yesno時它是如何關閉窗口一樣)。我使用的代碼如下在Netbeans平臺中使用自定義NotifyDescriptor

 
     // Create instance of your panel, extends JPanel... 
     ChooseTransactionType popupSelector = new ChooseTransactionType(); 

     // Create a custom NotifyDescriptor, specify the panel instance as a parameter + other params 
     NotifyDescriptor nd = new NotifyDescriptor(
       popupSelector, // instance of your panel 
       "Title", // title of the dialog 
       NotifyDescriptor.YES_NO_OPTION, // it is Yes/No dialog ... 
       NotifyDescriptor.QUESTION_MESSAGE, // ... of a question type => a question mark icon 
       null, // we have specified YES_NO_OPTION => can be null, options specified by L&F, 
         // otherwise specify options as: 
         //  new Object[] { NotifyDescriptor.YES_OPTION, ... etc. }, 
       NotifyDescriptor.YES_OPTION // default option is "Yes" 
     ); 

     // let's display the dialog now... 
     if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) { 
      // user clicked yes, do something here, for example: 
       System.out.println(popupSelector.getTRANSACTION_TYPE()); 
     } 

回答

4

爲了替換在options按鈕上的文本,你可以在一個String[]通爲options參數,或者,如果你需要多一點的控制,你可以通過在一個JButton[]。所以,在你的情況下,你需要從你的message面板上刪除按鈕,並通過String[]傳遞options參數。

對於initialValue(最後一個參數),而不是使用NotifyDescriptor.YES_OPTION,您可以使用String[]值(Purchase或Cashout)中的任何一個。 DialogDisplayer.notify()方法將返回選擇的值。因此,在這種情況下,它將返回String,但如果您傳入JButton[],則返回的值將是JButton

String initialValue = "Purchase"; 
String cashOut = "Cashout"; 
String[] options = new String[]{initialValue, cashOut}; 

NotifyDescriptor nd = new NotifyDescriptor(
      popupSelector, 
      "Title", 
      NotifyDescriptor.YES_NO_OPTION, 
      NotifyDescriptor.QUESTION_MESSAGE, 
      options, 
      initialValue 
    ); 

String selectedValue = (String) DialogDisplayer.getDefault().notify(nd); 
if (selectedValue.equals(initialValue)) { 
    // handle Purchase 
} else if (selectedValue.equals(cashOut)) { 
    // handle Cashout 
} else { 
    // dialog closed with top close button 
} 
+0

優秀!!這是我正在尋找..謝謝你! – Deepak 2012-04-25 02:00:31

+0

歡迎您,祝您好運 – 2012-04-25 03:09:44