count button clicks

后端 未结 8 1918
天命终不由人
天命终不由人 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:15

    You are resetting the counter every time you click, because you have defined the variable inside the action method. Try not doing that.

    int clicked = 0; // move this outside
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {                                         
        // int clicked = 0; -- this resets it to 0 each time
        clicked++;
        System.out.println(clicked);
    }
    

提交回复
热议问题