博为峰小博老师:
下面利用ComboBoxModel来创建一个下拉列表框实例,其代码如下所示:
publicclassBWF {
String[]s={"桃花","梅花","玫瑰","月季","茉莉","菊花"};
publicBWF(){
JFramejf=newJFrame("博为峰教育");
ContainercontentPane=jf.getContentPane();
ComboBoxModelmodel=newUserDefineComboBoxModel();
JComboBoxcombo=newJComboBox(model);
combo.setBorder(BorderFactory.createTitledBorder("你最喜欢的花是那个?"));
contentPane.add(combo);
jf.pack();
jf.setVisible(true);
jf.addWindowListener(newWindowAdapter() {
publicvoidwindowClosing(WindowEvente) {
System.exit(0);
}
});
}
publicstaticvoidmain(String[]args) {
newBWF();
}
classUserDefineComboBoxModelextendsAbstractListModelimplementsComboBoxModel{
Stringitem=null;
publicintgetSize() {
//TODOAuto-generated method stub
returns.length;
}
publicString getElementAt(intindex) {
//TODOAuto-generated method stub
returns[index++];
}
publicvoidsetSelectedItem(ObjectanItem) {
item=(String)anItem;
}
publicObject getSelectedItem() {
returnitem;
}
}
}