public class JComboBox extends JComponent
implements ItemSelectable, ListDataListener, ActionListener, Accessible
构造方法
JComboBox()
JComboBox(ComboBoxModel<E> aModel)
JComboBox(E[] items)
JComboBox(Vector<E> items)
常用方法
// 设置选中的索引
void setSelectedIndex(int index)
// 获取当前选中的索引
int getSelectedIndex()
// 获取当前选中的数据项
Object getSelectedItem()
// 设置选项数据模型
void setModel(ComboBoxModel<?> model)
// 添加选项选中状态改变的监听器
void addItemListener(ItemListener listener)
// 设置下拉列表框是否可编辑,默认不可编辑。
void setEditable(boolean flag)
//将一个条目添加到项目列表中
void addItem(E item)
//获取选中项的索引
int getSelectedIndex();
//设置选中项
void setSelectedIndex();
//按索引删除
void remove(index);
代码示例
//次类作为一个带有main函数的测试类,将对面板的内容和修改都放置到另外一个类中
public class Swing1 {
private static void createGUI() {
//JFrame 指一个窗口,构造方法的参数为窗口标题
MyFrame frame = new MyFrame("Swing Demo");
frame.