使用Java Swing的JComboBox实现Html中Select的key-value功能

1、自定义列表项类Item
public class Item {
	private int key;
	
	private String value;
	
	public Item(int key, String value){
		this.key = key;
		this.value = value;
	}


	public void setKey(int key) {
		this.key = key;
	}


	public int getKey() {
		return key;
	}


	public void setValue(String value) {
		this.value = value;
	}


	public String getValue() {
		return value;
	}
	
	public String toString(){
		return value;
	}
}

2、向JComboBox中插入数据
Item item = new Item(key, value);
cb.addItem(item);

3、获取当前选择项
Item item = (Item)cb.getSelectedItem();
调用item.getKey()和item.getValue()获取key和value值

你可能感兴趣的:(java,swing,select)