每次按下 JButton 时如何增加 JTextField 中的数字?

How do I increase the number in the JTextField every time I press the JButton?

    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));

    }


}                                     

如何在每次按 JButton 时增加 JTextField 中的数字?

您需要在 vote1ActionPerformed 的方法之外存储 int vote1vote2,这样您就不会每次都将投票计数重置为 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));
        }
    }