计数按钮点击 [英] count button clicks

查看:104
本文介绍了计数按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算使用GUI点击按钮的次数。

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

我做了这段代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
 {                                         
  int clicked = 0;
  clicked++;
  System.out.println(clicked);
 }    

但是每次单击按钮时它都会显示输出1。

But it showing the output "1", each time I click the button.

我希望每次点击按钮向我显示计数。

I want every time I click the button to show me the count.

ex:如果我点击按钮两次,它应该给我输出2。

ex: If I click the button two times it should give me output of "2".

推荐答案

每次点击都会重置计数器,因为你已经在action方法中定义了变量。尽量不这样做。

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);
}

这篇关于计数按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆