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

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

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


你能否重新格式化你的代码? - Mine Rockers
完成,我已经附上了一张图片,您可以查看。 - Jeric Marquez
1个回答

2

您需要在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));
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接