Java 获取 JPanel 组件
我有一个充满 JTextFields 的 JPanel...
for (int i=0; i<maxPoints; i++) {
JTextField textField = new JTextField();
points.add(textField);
}
以后如何获取该 JPanel 中的 JTextFields? 就像我想要他们的价值观一样
TextField.getText();
谢谢
I have a JPanel full of JTextFields...
for (int i=0; i<maxPoints; i++) {
JTextField textField = new JTextField();
points.add(textField);
}
How do I later get the JTextFields in that JPanel? Like if I want their values with
TextField.getText();
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Java 中的每个 JPanel 也是一个 AWT 容器。 因此,您应该能够使用 getComponents 获取面板中包含的组件的数组,迭代它们,检查它们的类型(以确保您没有获取其他控件),并对它们执行您需要的任何操作。
然而,这通常是糟糕的设计。 如果您知道需要访问特定组件,则最好维护这些文本字段的数组并将其传递,即使它们在视觉上包含在容器内。
如果这是一个经常性任务或者可以从多个点完成,那么甚至可以使用一个特殊的类来表示带有文本字段的面板,然后通过其界面提供访问这些字段的方法。
Every JPanel in Java is also an AWT container. Thus, you should be able to use getComponents to get the array of contained components in the panel, iterate over them, check their types (To make sure you didn't get other controls), and do whatever you need with them.
However, this is generally poor design. If you know that you will need to access specific components, it is better to maintain an array of those text fields and pass it around, even though they are visually contained within the container.
If this is a recurrent task or could be done from multiple points, it may even make sense to have a special class representing a panel with text fields, that will then provide through its interface means of accessing these fields.
请记住,他们自己并没有到达那里(我认为阅读一些有关在运行时动态创建这些面板的问题)
在那里发布的答案中,有人说您应该在数组中保留对这些文本字段的引用。 这正是您所需要的:
// 后来
不是那么容易吗?
请记住将这些类型的工件(列表)尽可能保密。 它们仅供您控制,我认为它们不属于界面。
假设您想要获取文本数组,而不是
您应该考虑:
Well bear in mind they didn't get there by them selves ( I think a read some questions about dynamically creating these panels at runtime )
In the answers posted there, someone said you should kept reference to those textfields in an array. That's exactly what you need here:
// Later
Wasn't that easy?
Just remember to keep these kinds of artifacts ( list ) as private as possible. They are for your control only, I don't think they belong to the interface.
Let's say you want to get the array of texts, instead of
You should consider:
您应该调用 getComponents 方法,该方法返回 JPanel 上所有元素的数组。 之后,您可以迭代数组并检查它是否与所寻找的组件相等。
You should call the getComponents method this returns with an array of all elements on your JPanel. After you can iterate on the array and check if its equals with the sought after component.
这就是我递归地遍历容器并获取 JPanel 上的文本字段所做的事情。
然后使用它,你可以这样称呼它
This is what I did to recursively go through the container and get the textfields that are on the JPanels.
And then to use it, you call it this way
在创建过程中动态为其命名,然后执行此操作。
Dynamicly give it a name during creation and then do this.
您的问题是编写乏味的代码文本。 为什么不直接生成它并粘贴到程序中!!...
将上述代码的输出粘贴到您的程序中,您就完成了。
现在,要访问文本字段的内容,您可以以类似的方式生成繁琐的代码文本......
Your problem is writing the tedious code text. Why not just generate it and paste in the program!!...
Paste the output of the above code in your program and you are done.
Now, to access the content of text fields you can generate the tedious code text in a similar way....
如果您有父母,那么您可以随时随地获取您的文本字段。
例如,我已在名为 searchBar 的 JPanel 上添加了文本字段。
首先我给textField起了一个名字(“txt_search_box”),然后我能够
从任何地方获取此文本字段,因为我通过以下方式引用了 JPanel(searchBar)
使用这个-
if you have parent then you can get your textField anytime, anywhere.
for example, i have added my textField on a JPanel named searchBar.
first i gave a name ("txt_search_box") to textField and then i was able to
get this textField from anywhere because i had reference to JPanel(searchBar) by
using this-