按下按钮一段时间后如何递增整数变量? [英] How to increment an integer variable when a button is pressed for sometime?

查看:71
本文介绍了按下按钮一段时间后如何递增整数变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想编写一个程序来检测何时按下特定键(比如空格键)并增加一个整数变量并将输出显示为递增从0开始的毫秒,当我释放密钥时,它可以以相同的速率减少到0.



即使是关于如何尝试这个东西的建议也会有所帮助。我会尝试自己,但目前我无法理解这件事。



提前致谢!!



我尝试过:



我目前正在研究核心java库并尝试创建简单的程序。我无法使用类Scanner执行此操作。我认为使用Scanner类不可能这样做。

解决方案

不能用Scanner类做到这一点。您需要一种在按下某个键时以及释放该键时激活的方法。请参阅 https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html [ ^ ]

Hi

I want to write a program which detects when a specific key(say Spacebar) is pressed and it increments an integer variable and displays the output as incrementing milliseconds starting from 0 and when i release the key it can decrement in the same rate to 0.

Even a suggestion on how to try this thing will be a help. I'll try on my own but currently i am unable to figure out this thing.

Thanks in advance !!

What I have tried:

I am currently working on core java libraries and trying to create simple programs. I am unable to do this using the class Scanner. I think it is not possible to do this using the Scanner class.

解决方案

No it is not able to do this with the Scanner class. You would need a method that gets activated when a key is pressed, and also when it is released. See https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html[^].


这篇关于按下按钮一段时间后如何递增整数变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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