按下的按钮的计数 [英] The count number of pressed button

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

问题描述

我需要,当我按下按钮显示按钮按下了多少次。我使用这种方法,但在控制台上仍显示数字1.

I need that when I press the button to show me how many times was the button pressed. I use this method, but on console still show me the number 1.

这里是代码:

 button_help.setOnMousePressed(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {

    int count = 0;
    count ++;

        System.out.println(count);
    }


 });


推荐答案

您的解决方案不会工作,每次单击按钮时变量的值。

Your solution doesn´t work as you are reseting the value of variable every time you click button. You have to define it once and than just increase the valu of it.

解决方案:

int count = 0;

button_help.setOnMousePressed(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {
        count++;
        System.out.println(count);
    }

});

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

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