简体   繁体   English

每次按下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));

    }


}                                     

How do I increase the number in the JTextField every time I press the JButton ? 每次按下JButton时,如何增加JTextField的数字?

每次按下j按钮时,如何增加jtextfield中的数字

You need to store int vote1 and vote2 outside of your method for vote1ActionPerformed , so that you dont reset the vote count to 0 every time. 您需要在您的方法之外存储int vote1vote2以进行vote1ActionPerformed ,这样您vote1ActionPerformed投票计数重置为0。

That way its really easy to update it to a bigger number each time. 这样每次都可以很容易地将它更新为更大的数字。 For example this would work: 例如,这将工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM