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

时间:2016-10-16 08:44:53

标签: java swing jbutton jtextfield

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

how do I increase the number in the jtextfield every time I press the jbutton

1 个答案:

答案 0 :(得分:2)

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