如何将表单字段放入列表中以便能够遍历它们?
How do I put the form fields into a list to be able to iterate through them?
我想使用 ArrayList
遍历此表单的字段,但我不知道该怎么做。
表格图片如下:
这是代码:
private void btnTotalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
double index=Double.parseDouble(txtIndexNou.getText())- Double.parseDouble(txtIndexVechi.getText());
txtIndexTotal.setText(Double.toString(index));
txtCam1.setText(Double.toString(index));
txtTotal1.setText(Double.toString((index *a)));
double index1=Double.parseDouble(txtIndexNou2.getText())-Double.parseDouble(txtIndexVechi2.getText());
txtIndexTotal.setText(Double.toString(index1));
txtCam1.setText(Double.toString(index1));
txtTotal1.setText(Double.toString((index1 *a)));
double index2=Double.parseDouble(txtIndexNou3.getText())-Double.parseDouble(txtIndexVechi3.getText());
txtIndexTotal.setText(Double.toString(index2));
txtCam1.setText(Double.toString(index2));
txtTotal1.setText(Double.toString((index2 *a)));
double index3=Double.parseDouble(txtIndexNou4.getText())-Double.parseDouble(txtIndexVechi4.getText());
txtIndexTotal.setText(Double.toString(index3));
txtCam1.setText(Double.toString(index3));
txtTotal1.setText(Double.toString((index3 *a)));
double index4=Double.parseDouble(txtIndexNou5.getText())-Double.parseDouble(txtIndexVechi5.getText());
txtIndexTotal.setText(Double.toString(index4));
txtCam1.setText(Double.toString(index4));
txtTotal1.setText(Double.toString((index4 *a)));
}
txtIndexNou 和 txtIndexVechi 我认为是某种文本输入必须用循环创建并且引用存储在 ArrayList 中, 然后当你想读回它们的值时,只需遍历列表即可。
编辑 1
要优化代码重复,您可以将您的逻辑封装到一个方法中。
不需要列表。我相信您想要做的是减少代码重复。您需要做的就是创建一个单独的方法来执行您正在复制的所有员工:
public void doStuff(String input1, String input2){
double index=Double.parseDouble(input1)-Double.parseDouble(input2);
txtIndexTotal.setText(Double.toString(index));
txtCam1.setText(Double.toString(index));
txtTotal1.setText(Double.toString((index*a)));
}
然后调用它:
doStuff(txtIndexNou.getText(), txtIndexVechi.getText());
doStuff(txtIndexNou2.getText(), txtIndexVechi2.getText());
doStuff(txtIndexNou3.getText(), txtIndexVechi3.getText());
doStuff(txtIndexNou4.getText(), txtIndexVechi4.getText());
doStuff(txtIndexNou5.getText(), txtIndexVechi5.getText());
我想使用 ArrayList
遍历此表单的字段,但我不知道该怎么做。
表格图片如下:
这是代码:
private void btnTotalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
double index=Double.parseDouble(txtIndexNou.getText())- Double.parseDouble(txtIndexVechi.getText());
txtIndexTotal.setText(Double.toString(index));
txtCam1.setText(Double.toString(index));
txtTotal1.setText(Double.toString((index *a)));
double index1=Double.parseDouble(txtIndexNou2.getText())-Double.parseDouble(txtIndexVechi2.getText());
txtIndexTotal.setText(Double.toString(index1));
txtCam1.setText(Double.toString(index1));
txtTotal1.setText(Double.toString((index1 *a)));
double index2=Double.parseDouble(txtIndexNou3.getText())-Double.parseDouble(txtIndexVechi3.getText());
txtIndexTotal.setText(Double.toString(index2));
txtCam1.setText(Double.toString(index2));
txtTotal1.setText(Double.toString((index2 *a)));
double index3=Double.parseDouble(txtIndexNou4.getText())-Double.parseDouble(txtIndexVechi4.getText());
txtIndexTotal.setText(Double.toString(index3));
txtCam1.setText(Double.toString(index3));
txtTotal1.setText(Double.toString((index3 *a)));
double index4=Double.parseDouble(txtIndexNou5.getText())-Double.parseDouble(txtIndexVechi5.getText());
txtIndexTotal.setText(Double.toString(index4));
txtCam1.setText(Double.toString(index4));
txtTotal1.setText(Double.toString((index4 *a)));
}
txtIndexNou 和 txtIndexVechi 我认为是某种文本输入必须用循环创建并且引用存储在 ArrayList 中, 然后当你想读回它们的值时,只需遍历列表即可。
编辑 1
要优化代码重复,您可以将您的逻辑封装到一个方法中。
不需要列表。我相信您想要做的是减少代码重复。您需要做的就是创建一个单独的方法来执行您正在复制的所有员工:
public void doStuff(String input1, String input2){
double index=Double.parseDouble(input1)-Double.parseDouble(input2);
txtIndexTotal.setText(Double.toString(index));
txtCam1.setText(Double.toString(index));
txtTotal1.setText(Double.toString((index*a)));
}
然后调用它:
doStuff(txtIndexNou.getText(), txtIndexVechi.getText());
doStuff(txtIndexNou2.getText(), txtIndexVechi2.getText());
doStuff(txtIndexNou3.getText(), txtIndexVechi3.getText());
doStuff(txtIndexNou4.getText(), txtIndexVechi4.getText());
doStuff(txtIndexNou5.getText(), txtIndexVechi5.getText());