2

我正在尝试删除 JComboBox 中的选定项目(我已添加设计时间)但未执行删除功能。我究竟做错了什么

// jComboBox1.removeAllItems(); working
// jComboBox1.removeItem(jComboBox1.getSelectedItem()); working
jComboBox1.remove(jComboBox1.getSelectedIndex()); not working
4

3 回答 3

5

中没有这种方法removeClass JComboBox

您想使用public void removeItemAt(int anIndex)

删除 anIndex 处的项目 此方法仅在 JComboBox 使用可变数据模型时有效。

jComboBox1.removeItemAt(jComboBox1.getSelectedIndex());
于 2013-04-09T13:25:00.420 回答
1

来自 JavaDoc

public void remove(int index)

    Removes the component, specified by index, from this container. This method also notifies the layout manager to remove the component from this container's layout via the removeLayoutComponent method.

    Parameters:
        index - the index of the component to be removed

但正如上面的答案中提到的,你需要做

jComboBox1.removeItemAt(jComboBox1.getSelectedIndex());

希望能帮助到你

于 2013-04-09T13:28:30.957 回答
0

看起来你想要jComboBox1.removeItemAt(...)

请参阅http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#removeItemAt(int)

于 2013-04-09T13:24:23.887 回答