Java - 在运行时使用引用名称添加 Swing 组件?
我目前正在开发一个在运行时将 JTextFields 添加到面板的应用程序,使用:
int rowCounter = 1;
pnlRows.add(new JLabel("Row " + rowCounter));
pnlRows.add(new JTextField(20));
pnlRows.revalidate();
validate();
我遇到的问题是我想在方法中使用它们的值,但无法引用它们。
有没有办法按照以下方式做一些事情:
int i = 1;
JTextField row + i = new JTextField();
i++;
这样每次我运行时,所做的 TextField 将分别称为 row1、row2、row3。
谢谢。
I am currently working on an app that adds JTextFields to a panel at runtime using:
int rowCounter = 1;
pnlRows.add(new JLabel("Row " + rowCounter));
pnlRows.add(new JTextField(20));
pnlRows.revalidate();
validate();
The problem i have is that i want to use their values in a method but have no way of referencing them.
Is there a way to do something along the lines of:
int i = 1;
JTextField row + i = new JTextField();
i++;
So that every time i run the TextField made will be called row1, row2, row3 respectively.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,没有,您需要做的是将它们添加到列表中,以便稍后您可以获取它们。
在班级的某个地方,您需要这样:
然后,当他们添加它们时,您可以执行以下操作:
最后,如果您想要获取所有内容,您可以迭代列表并一一获取所有文本字段。
No there isn't, what you need to do is add them to a list so that later you can get them all.
Somewhere in your class you need this:
Then when they add them you can do this:
Then finally wne you want to get them all you can iterate the list and get all the text fields out one by one.
据我所知这是不可能的。另一种(也许更简洁)的方法是在创建标签时存储标签,例如
您可以使用 fieldList 访问所需的文本字段:
编辑:fieldList 是
当然的。
As far as I know this is not possible. Another (and perhaps neater) way to do this is to store the Labels when you create them something like
You can then use the fieldList to access the text fields you want:
Edit: fieldList is a
of course.