private void vote1ActionPerformed(java.awt.event.ActionEvent evt) {
int vote1 = 0;
int vote2 = 0;
if (koonchk.isSelected()){
vote1++;
koontf.setText(Integer.toString(vote1));
}
else if (baamchk.isSelected()){
vote2++;
baamtf.setText(Integer.toString(vote2));
}
}
每次按JTextField
时如何增加JButton
中的数字?
答案 0 :(得分:2)
您需要在int vote1
的方法之外存储vote2
和vote1ActionPerformed
,这样您每次都不会将投票次数重置为0。
这样每次都可以很容易地将它更新为更大的数字。例如,这将起作用:
//Moved vote1/2 here outside of the method
static int vote1 = 0;
static int vote2 = 0;
private void vote1ActionPerformed(java.awt.event.ActionEvent evt){
//We removed vote1 and vote2 from here and put them above
if (koonchk.isSelected()){
vote1++;
koontf.setText(Integer.toString(vote1));
}
else if (baamchk.isSelected()){
vote2++;
baamtf.setText(Integer.toString(vote2));
}
}