Java 如何在每次按下JButton时增加jtext字段中的数字?
如何在每次按下Java 如何在每次按下JButton时增加jtext字段中的数字?,java,swing,jbutton,jtextfield,Java,Swing,Jbutton,Jtextfield,如何在每次按下JButton时增加JTextField中的数字 您需要将int vote1和vote2存储在执行的vote1操作的方法之外,这样您就不会每次都将投票计数重置为0 这样一来,每次都很容易将其更新为更大的数字。例如,这将起作用: private void vote1ActionPerformed(java.awt.event.ActionEvent evt) { int vote1 = 0;
JButton
时增加JTextField
中的数字
您需要将
int vote1
和vote2
存储在执行的vote1操作的方法之外,这样您就不会每次都将投票计数重置为0
这样一来,每次都很容易将其更新为更大的数字。例如,这将起作用:
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));
}
}
你能重新格式化你的代码吗?完成了,我有一张图片附在上面,你可以检查一下
//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));
}
}