java中其他类中的方法没用被调用但是却被执行了???高手解释一下,代码如下:
importjava.awt.*;importjavax.swing.*;publicclassComboboxextendsJFrame{JComboBoxjc=new...
import java.awt.*;
import javax.swing.*;
public class Combobox extends JFrame{
JComboBox jc=new JComboBox(new Cbox());
JLabel jl=new JLabel("下拉选择");
public Combobox(){
Container c=getContentPane();
setSize(200,200);
setTitle("下拉窗口");
setVisible(true);
c.add(jc);
c.add(jl);
setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Combobox();
}
}
class Cbox extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] str={null,"学生证","军人证","身份证","工作证"};
public int getSize(){
return str.length;
}
public Object getElementAt(int index){
return str[index];
}
public void setSelectedItem(Object item){
selecteditem=(String)item;
}
public Object getSelectedItem(){
return selecteditem;
}
public int getIndex() {
for (int i = 0; i < str.length; i++) {
if (str[i].equals(getSelectedItem()))
return i;
}
return 0;
}
}
注意:
public int getSize()
public Object getElementAt(int index)
public void setSelectedItem(Object item)
public Object getSelectedItem()
public int getIndex()
这些Cbox中的方法貌似都没被调用,为什么被执行了?是哪条语句调用的?请说具体点,感谢! 展开
import javax.swing.*;
public class Combobox extends JFrame{
JComboBox jc=new JComboBox(new Cbox());
JLabel jl=new JLabel("下拉选择");
public Combobox(){
Container c=getContentPane();
setSize(200,200);
setTitle("下拉窗口");
setVisible(true);
c.add(jc);
c.add(jl);
setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Combobox();
}
}
class Cbox extends AbstractListModel implements ComboBoxModel{
String selecteditem=null;
String[] str={null,"学生证","军人证","身份证","工作证"};
public int getSize(){
return str.length;
}
public Object getElementAt(int index){
return str[index];
}
public void setSelectedItem(Object item){
selecteditem=(String)item;
}
public Object getSelectedItem(){
return selecteditem;
}
public int getIndex() {
for (int i = 0; i < str.length; i++) {
if (str[i].equals(getSelectedItem()))
return i;
}
return 0;
}
}
注意:
public int getSize()
public Object getElementAt(int index)
public void setSelectedItem(Object item)
public Object getSelectedItem()
public int getIndex()
这些Cbox中的方法貌似都没被调用,为什么被执行了?是哪条语句调用的?请说具体点,感谢! 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询