action从数组中执行

时间:2013-05-10 21:37:05

标签: java swing awt actionlistener jcombobox

我的Java JComboBox由循环数组构成

假设组合框将列出:

"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
}

我不想获取String,而是获取数组编号。 例如我选择“巴西对加拿大”

  • 我应该使用什么方法让它返回“1”?
  • 然后,如果我选择“Holland Vs Netherland”,它将返回3

1 个答案:

答案 0 :(得分:3)

使用getSelectedIndex()而不是getSelectedItem()