【发布时间】:2015-04-21 11:43:44
【问题描述】:
我正在尝试一次在JFrame 中获取多个文本字段的值,有什么办法可以解决这个问题吗?目前我正在使用这段代码来生成我想要的效果,但是必须有更简单的方法,比如循环遍历它?
这是我正在使用的代码;它用于获取所有文本字段以获得总权重分数。
weight = new int[15];
weight[0] = Integer.parseInt(tf_weight1.getText());
weight[1] = Integer.parseInt(tf_weight2.getText());
weight[2] = Integer.parseInt(tf_weight3.getText());
weight[3] = Integer.parseInt(tf_weight4.getText());
weight[4] = Integer.parseInt(tf_weight5.getText());
weight[5] = Integer.parseInt(tf_weight6.getText());
weight[6] = Integer.parseInt(tf_weight7.getText());
weight[7] = Integer.parseInt(tf_weight8.getText());
weight[8] = Integer.parseInt(tf_weight9.getText());
weight[9] = Integer.parseInt(tf_weight10.getText());
weight[10] = Integer.parseInt(tf_weight11.getText());
weight[11] = Integer.parseInt(tf_weight12.getText());
weight[12] = Integer.parseInt(tf_weight13.getText());
weight[13] = Integer.parseInt(tf_weight14.getText());
weight[14] = Integer.parseInt(tf_weight15.getText());
我正在考虑做一些类似的事情;
String s = "tf_weight";
int inte = 1;
for(int i = 0; i<14; i++)
{
s = s + inte + ".getText()";
for(int j = 0; j<1; j++)
{
inte++;
criteria[i] = s.getText().replaceAll(" ", "~");
}
}
任何帮助将不胜感激。
【问题讨论】:
-
获取所有这些文本字段所在的框架,然后以与显示相同的方式遍历所有组件
-
Java 变量不能这样工作。为什么不将 JTextFields 放在
ArrayList< JTextField>之类的集合中,或者使用 JTable 更好?
标签: java loops jframe textfield