count button clicks

后端 未结 8 1925
天命终不由人
天命终不由人 2020-12-16 08:58

I want to count the number of times the button is clicked using GUI.

I did this code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent         


        
8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 09:18

    You have declared count variable inside the ActionListener. Declare it outside the block.

    int clicked = 0;  //make it as your class member.
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {                                         
    
      clicked++;
      System.out.println(clicked);
    }    
    

提交回复
热议问题