[英]Java/Netbeans — Reference a jTextfield dynamically?
Not sure if this is even remotely possible, but... 不知道这是否遥不可及,但是...
I have a jFrame with 10 different Textfields. 我有一个带有10个不同Textfield的jFrame。 Elequently named tf1, tf2, tf3... 后来被命名为tf1,tf2,tf3 ...
What I'd LOVE to do is be able to reference them dynamically. 我希望做的是能够动态引用它们。 Something like: 就像是:
int i = 1;
while (i<11) {
tf[i].settext("blah - " + i);
}
Any ideas? 有任何想法吗? If anybody knows of a working example it'd be great. 如果有人知道一个可行的例子,那就太好了。
If you want to set the text of all the textfields in a JFrame: 如果要在JFrame中设置所有文本字段的文本,请执行以下操作:
Component ca[] = getContentPane().getComponents();
System.out.println("ca = " + Arrays.toString(ca));
int i = 0;
for(Component c: ca) {
if(JTextField.class.isAssignableFrom(c.getClass())) {
JTextField tf = (JTextField) c;
tf.setText("blah -"+(++i));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.