简体   繁体   English

在循环中为每个字段创建具有唯一名称的textField

[英]Creating textField with unique name for each in a loop

im beginner level programmer on Java. 我是Java的初学者级程序员。 I would like to create textField in a for loop but i also want it to have a unique name (for calling after loop). 我想在for循环中创建textField,但我也希望它具有唯一的名称(用于在after循环中调用)。 For ex. 对于前。 I have this loop; 我有这个循环;

for (int i =0;i<x;i++)

        {


            Composite composite_10 = new Composite(composite_20, SWT.BORDER);
            composite_10.setBackground(SWTResourceManager.getColor(230, 230, 250));
            composite_10.setBounds(x1, y1, 542, 76);

            txtBaseSeri = new Text(composite_10, SWT.BORDER);
            txtBaseSeri.setMessage("Base Seri");
            txtBaseSeri.setToolTipText("");
            txtBaseSeri.setBounds(x2, y2, 95, 21);
            txtBaseSeri.setText("41");
            txtBaseSeri.setTextLimit(7);
}

Here, I want the second Textfield have a name like txtBaseSeri1, then txtBaseSeri2... so that i can call them after exiting the loop. 在这里,我希望第二个Textfield的名称像txtBaseSeri1,然后是txtBaseSeri2 ...,以便退出循环后可以调用它们。 Is there any way to do that ? 有什么办法吗? Thanks in advance.. 提前致谢..

I want the second Textfield have a name like txtBaseSeri1, then txtBaseSeri2 - Is there any way to do that? 我希望第二个Textfield具有一个类似txtBaseSeri1的名称,然后是txtBaseSeri2-有没有办法做到这一点?

No. Use an ArrayList instead and add your text fields to that list, for example like this: 否。请改用ArrayList并将文本字段添加到该列表中,例如:

List<Text> txtBaseSeri = new ArrayList<>();
for (int i = 0; i < x; i++) {
    // ...

    Text txt = new Text(composite_10, SWT.BORDER);
    // ...

    txtBaseSeri.add(txt);
}

Afterwards, you can access each Text object through its index, like 之后,您可以通过其索引访问每个Text对象,例如

Text first = txtBaseSeri.get(0);
Text second = txtBaseSeri.get(1);
// ...

or you can loop through them, like 或者您可以遍历它们,例如

for (Text txt : txtBaseSeri) {
    // in each loop iteration, txt will be set to the next element
    // ...
}

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

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