这就是我创建 JComboBox 的方式 -
String[] options = {"First", "Second" , "Third"};
JComboBox optionsCombo = new JComboBox(options);
When one of these items is selected, how do i get the index of the item which was selected ? 我不想要被选中的项目。
int index = optionsCombo.getSelectedIndex()
将给出选定的索引。在组合框动作监听器中使用它
索引从 0,1,2,.. 开始
optionsCombo.getSelectedIndex()
采用 :optionsCombo.getSelectedIndex();
在actionListener里面像这样:
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Selected: " + optionsCombo.getSelectedItem());
System.out.println(", Position: " + optionsCombo.getSelectedIndex());
}
};
optionsCombo.addActionListener(actionListener);