在 for 循环中实例化数组以创建 JTextFields [英] Instantiating array in a for loop to create JTextFields
本文介绍了在 for 循环中实例化数组以创建 JTextFields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 forloop 和 text_fields 实例变量来实例化每个文本字段,使其成为侦听器,并将其添加到小程序.text_fields 变量是一个数组,数组的最大数量为 2.
I need to use a forloop and the text_fields instance variable to instantiate each text field, make it a listener, and add it to the applet. The text_fields variable is an array which has a max number of arrays of 2.
Container c = getContentPane();
c.setLayout(new FlowLayout());
int i = 0;
for (i = 0; i < FIELDS; i++)
{
THIS IS WHERE I DON'T KNOW WHAT TO WRITE.
i need to instantiate the arrays, make them listeners and
add them to the applet.
}
推荐答案
不清楚 FIELDS
是你的 JTextField
数组还是常量.如果是组件数组本身,在迭代时考虑使用.length
数组字段.这减少了代码维护:
It's unclear is FIELDS
is your JTextField
aray or a constant. If it is the component array itself, consider using the .length
array field when iterating. This reduces code maintenance:
JTextField[] fields = new JTextField[SIZE];
for (int i = 0; i < fields.length; i++) {
fields[i] = new JTextField("Field " + i);
fields[i].addActionListener(myActionListener);
c.add(fields[i]);
}
注意大写变量用于 Java 命名约定下的常量.
Note uppercase variables are used for constants under Java naming conventions.
这篇关于在 for 循环中实例化数组以创建 JTextFields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文