创建一个 JComboBox 很容易看出它有哪些可用的方法,比如在 Eclipse 中打个点它就会提示方法列表。如:
JComboBox productTypes = new JComboBox();
productTypes.removeAllItems(); (这个时候打了点,略等半秒就会有方法列表)。
productTypes.addItem(new Option("001", "饮料"));
productTypes.addItem(new Option("002", "饼干"));
productTypes.addItem(new Option("003", "快餐"));
public class Option {
private final String label;
private final String value;
public Option(String label, String value) {
this.value = value;
this.label = label;
}
public String getLabel() {return this.label;}
public String getValue{) {return this.value;}
public String toString() {
return "[" + this.value + "]" + this.label;
}
}