Java get JPanel Components(Java 获取 JPanel 组件)
问题描述
我有一个 JPanel,里面装满了 JTextFields...
I have a JPanel full of JTextFields...
for (int i=0; i<maxPoints; i++) {
JTextField textField = new JTextField();
points.add(textField);
}
我以后如何在该 JPanel 中获取 JTextField?就像我想要他们的价值观一样
How do I later get the JTextFields in that JPanel? Like if I want their values with
TextField.getText();
谢谢
推荐答案
请记住,他们自己并没有到达那里(我想阅读一些关于在运行时动态创建这些面板的问题)
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:
List<JTextField> list = new ArrayLists<JTextField>();
// your code...
for (int i=0; i<maxPoints; i++) {
JTextField textField = new JTextField();
points.add(textField);
list.add( textField ); // keep a reference to those fields.
}
//稍后
for( JTextField f : list ) {
System.out.println( f.getText() ) ;
}
不是那么容易吗?
请记住将这些类型的工件 ( list ) 尽可能保密.它们仅供您控制,我认为它们不属于界面.
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
public List<JTextField> getFields();
你应该考虑:
public List<String> getTexts(); // get them from the textfields ...
这篇关于Java 获取 JPanel 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 获取 JPanel 组件
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01