简体   繁体   English

Netbeans:Jframe中带有子索引的JCombobox

[英]Netbeans: JCombobox in Jframe with subindex

My English is not good, but I try. 我的英语不好,但是我尝试了。

I created 4 JComboBox in the NetBeans JForm 我在NetBeans JForm创建了4个JComboBox

Combo1
Combo2
Combo3
Combo4

How do I call them by the number? 我如何按号码称呼他们? For example: 例如:

i = 2;

String item = (String) combo(i).getSelectedItem();

This obviously does not work, I know. 我知道这显然行不通。

And I can not create another array called combo[] because NetBeans considers it to be another JComboBox . 而且我无法创建另一个名为combo[]数组,因为NetBeans认为它是另一个JComboBox

Is there any way to do it? 有什么办法吗?

Or this can not be done in NetBeans? 还是在NetBeans中无法做到这一点?

And I can not create another array called combo[] 而且我无法创建另一个名为combo []的数组

Sure you can. 你当然可以。 The array variable name would be "combo" and the individual combo boxes are "combo1", "combo2" etc. 数组变量名称为“ combo”,单个组合框为“ combo1”,“ combo2”等。

The basic code is: 基本代码是:

JCombobox[] comboBoxes = new JComboBox[4];
JComboBox combo1 = new JComboBox(...);
comboBoxes[0] = combo1;
JComboBox combo2 = new JComboBox(...);
comboBoxes[1] = combo2;

Then when you want to access the combo box you use: 然后,当您要访问组合框时,请使用:

String item = comboBoxes[i].getSelectedItem().toString();

How you actually create the combo boxes and add the to the frame is up to you, but then is no reason you can't add the combo box to an array. 实际创建组合框并将其添加到框架的方式由您自己决定,但是没有理由不能将组合框添加到数组。

because NetBeans 因为NetBeans

Don't use NetBeans to create the GUI. 不要使用NetBeans创建GUI。 If you do you are spending time learning the IDE and the code won't be portable if you ever move to another IDE. 如果这样做的话,您会花时间学习IDE,并且如果您移至另一个IDE,则代码将无法移植。

Instead, create the GUI manually and just use the IDE to compile and debug your code. 而是手动创建GUI,然后仅使用IDE编译和调试代码。 This way you spend time learning Java, not the IDE. 这样,您就可以花时间学习Java,而不是IDE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM