我试图创建一个循环来在页面上多次创建这个 JComboBox。知道为什么这不起作用吗?
String[] numbers = {"1","2","3","4","5","6","7","8","9","10"};
JComboBox numberbox = new JComboBox(numbers);
for(int j = 1; j <= 12; j++){
numberbox[j].setLocation(103,j*30);
numberbox[j].setSize(47,26);
numberbox[j].setSelectedIndex(1);
numberbox[j].setBackground(Color.WHITE);
panelForTextFields.add(numberbox[j]);
numberbox[j].addActionListener(this);
}
我相信它是循环部分以外的工作代码。当我用这段代码替换它时,它会打印一个可以按我想要的方式工作的 JComboBox。
String[] numbers = {"1","2","3","4","5","6","7","8","9","10"};
JComboBox numberbox = new JComboBox(numbers);
numberbox.setLocation(103,30);
numberbox.setSize(47,26);
numberbox.setSelectedIndex(1);
numberbox.setBackground(Color.WHITE);
panelForTextFields.add(numberbox);
numberbox.addActionListener(this);
我在 Eclipse 中收到的错误是“表达式的类型必须是数组类型,但它解析为 JComboBox”