2013-01-18 77 views

回答

7

如果你想用JOptionPane.showInputDialog自定義按鈕文本,可以擴展的JOptionPane的解決方案:

public class JEnhancedOptionPane extends JOptionPane { 
    public static String showInputDialog(final Object message, final Object[] options) 
      throws HeadlessException { 
     final JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE, 
               OK_CANCEL_OPTION, null, 
               options, null); 
     pane.setWantsInput(true); 
     pane.setComponentOrientation((getRootFrame()).getComponentOrientation()); 
     pane.setMessageType(QUESTION_MESSAGE); 
     pane.selectInitialValue(); 
     final String title = UIManager.getString("OptionPane.inputDialogTitle", null); 
     final JDialog dialog = pane.createDialog(null, title); 
     dialog.setVisible(true); 
     dialog.dispose(); 
     final Object value = pane.getInputValue(); 
     return (value == UNINITIALIZED_VALUE) ? null : (String) value; 
    } 
} 

你可以這樣調用它:

JEnhancedOptionPane.showInputDialog("Number:", new Object[]{"Yes", "No"}); 
+0

非常感謝!正是我正在尋找...! –

8

下面的代碼應該會出現一個對話框,您可以指定Object[]中的按鈕文本。

Object[] choices = {"One", "Two"}; 
Object defaultChoice = choices[0]; 
JOptionPane.showOptionDialog(this, 
      "Select one of the values", 
      "Title message", 
      JOptionPane.YES_NO_OPTION, 
      JOptionPane.QUESTION_MESSAGE, 
      null, 
      choices, 
      defaultChoice); 

此外,請務必查看Oracle網站上的Java教程。我發現這個鏈接的教程http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#create

15

,如果你不希望它只是一個單一的使用inputdialog,這些行創建對話框

UIManager.put("OptionPane.cancelButtonText", "nope"); 
UIManager.put("OptionPane.okButtonText", "yup"); 
之前添加210

其中'yup'和'nope'是你想要顯示的文字

+0

這也非常有幫助!謝謝! –

相關問題