Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有3个组合框和一个向量变量的Swing JFrame帮助_Java_Swing_Combobox_Jframe - Fatal编程技术网

Java 带有3个组合框和一个向量变量的Swing JFrame帮助

Java 带有3个组合框和一个向量变量的Swing JFrame帮助,java,swing,combobox,jframe,Java,Swing,Combobox,Jframe,我创建了一个SwingJFrame,其中包含3个组合框和一个向量变量来填充它们,但在执行代码时,所有组合框都是空的。谁能告诉我怎么了 public class Notes extends JFrame { JFrame jf; JPanel jp = new JPanel(); Vector<Integer> v = new Vector<Integer>(); int i; Integer x; Dimension d

我创建了一个Swing
JFrame
,其中包含3个组合框和一个向量变量来填充它们,但在执行代码时,所有组合框都是空的。谁能告诉我怎么了

public class Notes extends JFrame {

    JFrame jf;
    JPanel jp = new JPanel();
    Vector<Integer> v = new Vector<Integer>();
    int i;
    Integer x;
    Dimension d = new Dimension(40, 12);

    Notes() {

        jf = new JFrame("ComboBox Demo");

        for (i = 1; i <= 31; i++) {
            x = new Integer(i);
            v.add(x);
        }

        JComboBox date = new JComboBox(v);
        v.removeAllElements();



        for (i = 1; i <= 12; i++) {
            x = new Integer(i);
            v.add(x);
        }

        JComboBox month = new JComboBox(v);
        v.removeAllElements();


        for (i = 2011; i <= 2020; i++) {
            x = new Integer(i);
            v.add(x);
        }

        JComboBox year = new JComboBox(v);
        v.removeAllElements();


        date.setSize(d);
        month.setSize(d);
        year.setSize(d);

        jp.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
        jp.add(date);
        jp.add(month);
        jp.add(year);

        jf.add(jp, BorderLayout.PAGE_START);

        jf.setSize(300, 300);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String arg[]) {
        new Notes();
    }
}
public类注释扩展JFrame{
jf框架;
JPanel jp=新的JPanel();
向量v=新向量();
int i;
整数x;
尺寸d=新尺寸(40,12);
附注({
jf=新JFrame(“组合框演示”);

对于(i=1;i这很明显,因为您正在删除向量的所有元素:

v.removeAllElements(); //that's wrong
当您将v传递给JComboBox构造函数时,组合框不会复制每个值,而是引用您提供给它的向量。因此,如果您从该向量中删除元素,您将看到一个空的组合框

如果您这样做是为了在每次循环后重用v变量,请将上面的错误行替换为:

v = new Vector<Integer>();
v=新向量();

很明显。很明显,这段代码有很多错误。如果你花时间解释为什么“那是错误的”,也许你会花更多的时间,以及他应该做什么。@little bunny foo foo:至少给我时间完成我的回答。顺便说一句,我的代码段有什么问题吗?@Overbose,我指的是OP的代码段,不是你的。正确的建议如何从“Vector”中删除所有元素,这是恐龙时代之前的API,还有另一个选项不存在+1+1@mKorble关于
java.util.Vector
的正确答案。否则,这是一个很好的答案,所以我试图挽救这个问题。我认为没有必要在问题主题中使用“.pls help”。