gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:35 26 4
gpt4 key购买 nike

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

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

最佳答案

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

关于java - 每次按下 JButton 时如何增加 JTextField 中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com