4

这就是我创建 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 ? 我不想要被选中的项目。

4

3 回答 3

9
int index = optionsCombo.getSelectedIndex() 

将给出选定的索引。在组合框动作监听器中使用它

于 2013-04-02T09:31:51.343 回答
7

索引从 0,1,2,.. 开始

optionsCombo.getSelectedIndex()
于 2013-04-02T11:46:22.437 回答
5

采用 :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);
于 2013-04-02T09:31:34.107 回答