实例化阵列中的for循环创建JTextField的 [英] Instantiating array in a for loop to create JTextFields

查看:144
本文介绍了实例化阵列中的for循环创建JTextField的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个forloop和text_fields实例变量每个实例
文本字段,使它成为一个监听器,并且将其添加到该小程序。
该text_fields变量是其具有2阵列的最大数量的阵列

 容器C =的getContentPane();c.setLayout(新的FlowLayout());
 INT I = 0;
 对于(i = 0; I<领域;我++)
 {
   这是我不知道写什么。
       我需要实例化阵列,使他们的听众和
       它们添加到小程序。
 }


解决方案

目前还不清楚是字段的JTextField 阿雷或恒定。如果是分量数组本身,可以考虑进行遍历时,使用。长度阵列领域。这降低了code保养:

  JTextField的[]栏=新的JTextField [SIZE]
的for(int i = 0; I< fields.length;我++){
   域[I] =新的JTextField(田+ I);
   域[I] .addActionListener(myActionListener);
   c.add(场[I]);
}

请注意大写变量在Java命名约定用于常量。

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.


 }

解决方案

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]);
}

Note uppercase variables are used for constants under Java naming conventions.

这篇关于实例化阵列中的for循环创建JTextField的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆