2012-05-20 62 views
1

我想訪問user.Eg選擇的選項的索引,如下圖所示,如果我選擇microsoft選項,那麼它應該給我索引1.這可能嗎?從Joptionpane獲取索引

enter image description here

+3

類似的問題似乎在這裏已經回答: HTTP://計算器的.com /問題/ 3074478 /如何對選擇 - 一個指數值從 - 一個串陣-IN-A-的JOptionPane – bschandramohan

回答

2

那麼你得到"Microsoft"(井Object顯示微軟至少)從節目調用的返回值,是足夠好了嗎?

如果您需要索引,只需在您提供給對話框的輸入數組中找到該返回值的索引即可。

請參閱Java教程的輸入部分: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input

假設你正在使用showInputDialog(..):

Object[] possibilities = {"Broadcom...", "Microsoft"}; 
Object result = JOptionPane.showInputDialog(frame, "Capture Interfaces", "Input", JOptionPane.PLAIN_MESSAGE, icon, possibilities, possibilities[0]); 

if (result != null) { 
    //result is the choosen object, if you need the index even so: 
    int index = 0; 
    for (Object o : possibilities) { 
     if (result == o) 
      break; 
     index++ 
    } 
    //index is now the index... 
}