好的,问题来了:每次我单击“确定”时,它都应该返回选定的索引号,对吗?无论我选择什么,下面的代码都只返回第一个索引。我设置了“cMenu.selected(1)”,它再次返回索引 1,无论我选择什么。
使用 JPanel、JButton、Choice
String[] menu = {"item 1" , "item 2", "item3"};
cMenu = new Choice();
cMenu.setBounds(0, 0, 75, 25);
for (int i = 0; i < menu.length; i++)
cMenu.add(menu[i]);
}
panel.add(cMenu);
final int menuSelection = cMenu.getSelectedIndex();
//Below is, of course, debugging
//Before asking, the button works it does say 0 or Hello World or whatever I want
//when clicked
OK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(menuSelection);
}
});