2016-10-16 6 views
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)); 

    } 


}          

JButtonを押すたびにJTextFieldの番号を増やすにはどうすればよいですか?JButtonを押すたびにJTextFieldの数を増やすにはどうすればよいですか?

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

+0

コードを再フォーマットしてください。 –

+0

が完了しました。確認できる画像が添付されています –

答えて

2

あなたが0に投票数を毎回リセットしていけないようにするには、vote1ActionPerformedのためにあなたの方法のint vote1vote2外側を格納する必要があります。

そのように、毎回大きな番号にそれを更新するのは本当に簡単です。たとえば、これは動作します:

//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)); 
     } 
    } 
関連する問題