2013-05-10 43 views
0

我的Java JComboBox actionPerformed是在由陣列循環從一個數組

假設組合框將列出:

"Argentina Vs USA" // array[0] 
"Brazil Vs Canada" // array[1] 
"Canada Vs Netherland" // array[2] 
"Holland Vs Netherland" // array[3] 

然後,一旦一個選項被選擇在..

actionPerformed(ActionEvent e){ 
JComboBox cb = (JComboBox)e.getSource(); 
String countryVScountry = (String)cb.getSelectedItem(); 
//I need help here, I don't want to get the String, but the 
//array number instead. 
//e.g. I choose "Brazil Vs Canada" 
//What method should I use to make it return "1"? 
//then if I choose "Holland Vs Netherland", it will return 3 
} 

我不想獲取字符串,而是數組數字。 例如我選擇「巴西VS加拿大」

  • 我應該用什麼方法使它返回「1」?
  • 然後如果我選擇 「荷蘭Vs的荷蘭」,它將返回3
+3

荷蘭VS荷蘭? – wchargin 2013-05-10 21:40:37

+0

gratz指出這一點!哈哈 – 2013-05-10 21:42:14

回答

3

使用getSelectedIndex()而非getSelectedItem()

+2

大聲笑,非常感謝你,併爲我犯的愚蠢問題感到抱歉。更應該先學習。再一次感謝你 – 2013-05-10 21:40:29