求助:关于图形用户界面设计
我声明了一个JComboBox box = new JComboBox()和String str = {"黄","红","绿","紫","黑"},在声明的时候没有把str加入box里,而想在后面的程序中把str加入到box里,该怎么处理?
addItem
public void addItem(Object anObject)
- Adds an item to the item list. This method works only if the
JComboBox
uses a mutable data model.Warning: Focus and keyboard navigation problems may arise if you add duplicate String objects. A workaround is to add new objects instead of String objects and make sure that the toString() method is defined. For example:
comboBox.addItem(makeObj("Item 1")); comboBox.addItem(makeObj("Item 1")); ... private Object makeObj(final String item) { return new Object() { public String toString() { return item; } }; }
- Parameters:
anObject
- the Object to add to the list