日期:2014-05-20  浏览次数:20793 次

Vector
public class test extends JFrame implements ActionListener{
Vector vc=new Vector();
vc.add("dddd"); JComboBox jcb=new JComboBox(vc);
JButton jb=new JButton();


  test(){
  jcb.setEditable(true);
  setLayout(new FlowLayout());
  add(jcb);add(jb);
  setTitle("提示框");
  setSize(300,300);
  setResizable(false);
  jb.addActionListener(this);
  setLocationRelativeTo(null);
  setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
  if(e.getSource()==jb) System.out.print(jcb.getSelectedItem());
  }
public static void main(String[] args) {
// TODO Auto-generated method stub
  test t1=new test();
  
}

}
有颜色的地方有问题

------解决方案--------------------
根据提示:Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

原因:JVM是通过main运行,不能在public class里先调用vector add
类似问题:不能在main前,调用其他类的method


解决办法:把 Vector<String> vc1=new Vector<String>();
vc1.add("aaa");
JComboBox jcb=new JComboBox(vc1);
放进构造器constructor test(){里