我需要使用forloop和text_fields实例变量来实例化每个 文本字段,使其成为侦听器,并将其添加到applet。 text_fields变量是一个最大数组为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.
}
答案 0 :(得分:1)
这可能会有所帮助。
Container c = getContentPane();
c.setLayout(new FlowLayout());
JTextField[] txt = new JTextField[FIELDS]; // FIELDS is an int, representing the max number of JTextFields
int i = 0;
for (i = 0; i < FIELDS; i++)
{
txt[i] = new JTextField();
// add any listener you want to txt[i]
c.add(txt[i]);
}
答案 1 :(得分:0)
目前还不清楚FIELDS
是你的JTextField
aray还是常数。如果它是组件数组本身,请在迭代时考虑使用.length
数组字段。这减少了代码维护:
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命名约定下的常量。