我想找到jtextfields的总和(sht1到sht13)并将结果放在llog jtextfield中,但如果我不在每个jtextfield(sht1到sht13)中放置值,它就不会工作 我如何修复我的代码,以便它只采用我选择将值放在
上的jtextfields的值public class Wrapper
{
public _CommentAdmindto Result { get; set; }
}
public class _CommentAdmindto
{
public long dateTime { get; set; }
public IEnumerable<CommentDtoAdmin> list { get; set; }
}
答案 0 :(得分:1)
建议:
ArrayList<JTextField>
如,
// note that likely you should be using an ActionListener not a MouseListener
private void myButtonActionPerformed(java.awt.event.ActionEvent e) {
int sum = 0;
// assuming a JTextField ArrayList<JTextField> called vFieldList
for (JTextField vField : vFieldList) {
String text = vField.getText().trim(); // get text and trim out all outside whitespace
try {
int value = Integer.parseInt(text);
// if exception thrown, this line won't be reached
sum += value; // add value to sum
} catch (NumberFormatException nfe) {
// ignore exception
}
}
llog.setText("" + sum);
}
您是否通过使用列表和for循环看到了这种情况如何简化代码,使调试或增强更容易?
更好的是: