Java获取JPanel组件 [英] Java get JPanel Components
问题描述
我有一个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组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!