2013-01-25 27 views
0

我想知道inputdialog如何返回值,特別是當還有ok和cancel按鈕時。 有人可以解釋它如何設法返回值?JOptionPane的showInputDialog如何工作?

更新:

讓我這樣說吧。 我想用6個按鈕創建一個對話框,每個按鈕返回不同的值。 而我希望它得到這樣的值: String value = MyDialog.getValue(); // like showInputDialog

問題是我如何返回按鈕按下按鈕?

+5

*「我在想,怎麼......」「你希望人們通過你的嘟wa聲來趟過嗎?適當時請使用大寫字母。它使文本更易於閱讀。 –

+0

只需下載[JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html),當您安裝時,會有一個'src.zip(在Windows下)',在'JAVA_HOME'目錄中,解壓縮並自己查看代碼,究竟以什麼方式工作:-) –

+0

您的目標是創建一個不使用JOptionPane的提示對話框? – VGR

回答

2

現在我對自己的目標有了更清晰的理解,我認爲不是試圖效仿JOptionPane,而是給每個按鈕一個不同的actionCommand會更容易:

private JDialog dialog; 

private String inputValue; 

String showPromptDialog(Frame parent) { 
    dialog = new JDialog(parent, true); 
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 

    // [add components to dialog here] 

    firstButton.setAction(new ButtonAction("Button 1", "first")); 
    secondButton.setAction(new ButtonAction("Button 2", "second")); 
    thirdButton.setAction(new ButtonAction("Button 3", "third")); 
    fourthButton.setAction(new ButtonAction("Button 4", "fourth")); 
    fifthButton.setAction(new ButtonAction("Button 5", "fifth")); 
    sixthButton.setAction(new ButtonAction("Button 6", "sixth")); 

    dialog.pack(); 
    dialog.setLocationRelativeTo(parent); 

    inputValue = null; 
    dialog.setVisible(true); 

    return inputValue; 
} 

private class ButtonAction 
extends AbstractAction { 
    private static final long serialVersionUID = 1; 

    ButtonAction(String text, 
       String actionCommand) { 
     super(text); 
     putValue(ACTION_COMMAND_KEY, actionCommand); 
    } 

    public void actionPerformed(ActionEvent event) { 
     inputValue = event.getActionCommand(); 
     dialog.dispose(); 
    } 
} 
+0

是的,就是這樣但你能解釋一下爲什麼它不會將海峽退回零而是等待?因爲在我看來它應該在setVisible(true)之後返回null; – KoSMoS

+2

Dialog.setVisible(true)會阻止調用線程,直到該對話框不再可見(如果該對話框爲模態)。這是在[setVisible](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.html#setVisible%28boolean%29)方法的javadoc中指定的,雖然它有點兒難以遵循;不贊成show()方法的javadoc實際上更清楚了。它使用SecondaryLoop實現。在Java 1.7之前,SecondaryLoop不是公開的類,但在模態對話框中調用setVisible(true)時,Dialog始終使用某個輔助循環的一些私有版本。 – VGR

2

Java tutorials

Object[] possibilities = {"ham", "spam", "yam"}; 
String s = (String)JOptionPane.showInputDialog(
    frame, 
    "Complete the sentence:\n" 
    + "\"Green eggs and...\"", 
    "Customized Dialog", 
    JOptionPane.PLAIN_MESSAGE, 
    icon, 
    possibilities, 
    "ham"); 

//If a string was returned, say so. 
if ((s != null) && (s.length() > 0)) { 
    setLabel("Green eggs and... " + s + "!"); 
    return; 
} 

你可能會喜歡的部分,以熟悉的「獲取用戶的輸入,從一個對話框」

你也應該與Java Docs熟悉就同一主題

+0

這不是我想要的)我想知道如何String s =(String)JOptionPane.showInputDialog(null,「test」);在引擎蓋下完成 – KoSMoS

+2

你想要什麼?如果點擊'Cancel','JOptionPane#showInputDialog'將返回'String',如果您點擊'Cancel',則返回'null'? – MadProgrammer

+0

@KoSMoS在這裏你可以閱讀源代碼:http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/JOptionPane.java#JOptionPane。 showInputDialog%28java.awt.Component%2Cjava.lang.Object%2Cjava.lang.String%2Cint%2Cjavax.swing.Icon%2Cjava.lang.Object%5B%5D%2Cjava.lang.Object%29 –