首页上一页 1 下一页尾页 1 条记录 1/1页
请教:最后一个方法起什么作用
发表在Java图书答疑
2010-05-31
是否精华
是
否
版块置顶:
是
否
public class JComboBoxModelTest extends JFrame{
/**
* @param args
*/
JComboBox jc=new JComboBox(new MyComboBox());
JLabel jl=new JLabel("请选择证件");
public JComboBoxModelTest() {
// TODO Auto-generated constructor stub
Container c=getContentPane();
setTitle("在窗体中设置下拉列表框");
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(new Dimension(160,80));
setLayout(new FlowLayout());
c.add(jl);
c.add(jc);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JComboBoxModelTest();
}
}
class MyComboBox extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] test={"身份证","军人证","学生证","工作证"};
@Override
public Object getElementAt(int index) {
// TODO Auto-generated method stub
return test[index];
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return test.length;
}
@Override
public void setSelectedItem(Object anItem) {
// TODO Auto-generated method stub
selecteditem=(String)anItem;
}
@Override
public Object getSelectedItem() {
// TODO Auto-generated method stub
return selecteditem;
}
public int getIndex(){
for(int i=0;i<test.length; i++){
if(test[i].equals(getSelectedItem()))
return i;
break;
}
return 0;
}
}
最后一个方法:getIndex()起什么作用了,看出来,我去掉这个方法,程序照常运行。
还有:if(test[i].equals(getSelectedItem())是用来判断什么的?
请指教,谢谢。
/**
* @param args
*/
JComboBox jc=new JComboBox(new MyComboBox());
JLabel jl=new JLabel("请选择证件");
public JComboBoxModelTest() {
// TODO Auto-generated constructor stub
Container c=getContentPane();
setTitle("在窗体中设置下拉列表框");
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(new Dimension(160,80));
setLayout(new FlowLayout());
c.add(jl);
c.add(jc);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new JComboBoxModelTest();
}
}
class MyComboBox extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] test={"身份证","军人证","学生证","工作证"};
@Override
public Object getElementAt(int index) {
// TODO Auto-generated method stub
return test[index];
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return test.length;
}
@Override
public void setSelectedItem(Object anItem) {
// TODO Auto-generated method stub
selecteditem=(String)anItem;
}
@Override
public Object getSelectedItem() {
// TODO Auto-generated method stub
return selecteditem;
}
public int getIndex(){
for(int i=0;i<test.length; i++){
if(test[i].equals(getSelectedItem()))
return i;
break;
}
return 0;
}
}
最后一个方法:getIndex()起什么作用了,看出来,我去掉这个方法,程序照常运行。
还有:if(test[i].equals(getSelectedItem())是用来判断什么的?
请指教,谢谢。