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中的数字?

最佳答案

您需要将int vote1vote2存储在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));
        }
    }

09-25 21:44