我想使用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)));
}
答案 0 :(得分:0)
txtIndexNou 和 txtIndexVechi 我假设是某种文本输入必须用循环创建并且引用存储在ArrayList中,然后当你想要回读他们的价值观,只是遍历清单。
为了优化代码重复,您可以将逻辑封装到方法中。
答案 1 :(得分:0)
无需列表。我相信你想要做的是减少代码重复。您需要做的就是创建一个单独的方法来完成您复制的所有工作人员:
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());