首页上一页 1 下一页尾页 1 条记录 1/1页
JComboBoxModel
发表在Java图书答疑
2015-04-13
是否精华
是
否
版块置顶:
是
否
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
selecteditem cannot be resolved to a variable
at Thirteen.MyComboBox.getSelectedItem(JComboBoxModelTest.java:44)
at javax.swing.JComboBox.setModel(JComboBox.java:295)
at javax.swing.JComboBox.<init>(JComboBox.java:161)
at Thirteen.JComboBoxModelTest1.<init>(JComboBoxModelTest1.java:8)
at Thirteen.JComboBoxModelTest1.main(JComboBoxModelTest1.java:23) 这个怎么解决
JAVA从入门到精通 第三版 第十三章13.7节 13.13
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.*;
public class JComboBoxModelTest extends JFrame {
JComboBox jc=new JComboBox(new MyComboBox());
JLabel jl=new JLabel("请选择证件");
public JComboBoxModelTest(){
setSize(new Dimension(160,180));
setVisible(true);
setTitle("在窗口中设置下拉列表框");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jc);
cp.add(jl);
}
public static void main(String[] args){
new JComboBoxModelTest();
}
}
class MyComboBox extends AbstractListModel implements ComboBoxModel{
String selscteditem=null;
String[] test={
"身份证","军人证","学生证","工作证"
};
public Object getElementAt(int index){
return test[index];
}
public int getSize(){
return test.length;
}
public void setSelectedItem(Object item){
selecteditem = (String) item;
}
public Object getSelectedItem(){
return selecteditem;
}
public int getIndex(){
for(int i = 0; i <test.length; i++){
if(test[i].equals(getSelectedItem()))
return i;
}
return 0;
}
}
selecteditem cannot be resolved to a variable
at Thirteen.MyComboBox.getSelectedItem(JComboBoxModelTest.java:44)
at javax.swing.JComboBox.setModel(JComboBox.java:295)
at javax.swing.JComboBox.<init>(JComboBox.java:161)
at Thirteen.JComboBoxModelTest1.<init>(JComboBoxModelTest1.java:8)
at Thirteen.JComboBoxModelTest1.main(JComboBoxModelTest1.java:23) 这个怎么解决
JAVA从入门到精通 第三版 第十三章13.7节 13.13
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.*;
public class JComboBoxModelTest extends JFrame {
JComboBox jc=new JComboBox(new MyComboBox());
JLabel jl=new JLabel("请选择证件");
public JComboBoxModelTest(){
setSize(new Dimension(160,180));
setVisible(true);
setTitle("在窗口中设置下拉列表框");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jc);
cp.add(jl);
}
public static void main(String[] args){
new JComboBoxModelTest();
}
}
class MyComboBox extends AbstractListModel implements ComboBoxModel{
String selscteditem=null;
String[] test={
"身份证","军人证","学生证","工作证"
};
public Object getElementAt(int index){
return test[index];
}
public int getSize(){
return test.length;
}
public void setSelectedItem(Object item){
selecteditem = (String) item;
}
public Object getSelectedItem(){
return selecteditem;
}
public int getIndex(){
for(int i = 0; i <test.length; i++){
if(test[i].equals(getSelectedItem()))
return i;
}
return 0;
}
}