JButton长按事件 [英] JButton long press event

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

问题描述

我正在NetBeans平台上开发Java桌面应用程序.我有几个JFrame,在这些帧内,我有几个JButton.

I am developing Java desktop application in NetBeans platform. I have several JFrames and within these frames I have several JButtons.

我的应用程序将在工业PC,基于Linux的Panel PC等触摸面板上运行.因此,我需要使用按钮的长按事件.

My application will be run on touch panels like industrial PCs, Linux based panel PCs etc. So I will need to use long press event of a button.

如何处理JButton长按事件?点击事件还可以,但是我找不到任何有关长按/长按的参考或示例.

How can I handle long press event of JButton? Click event is OK but I could not find any references or samples about long press/long click.

推荐答案

如果您决定让实现使用JButton,则应注意通常不使用单击事件"(尽管从理论上讲,您是) ,可以使用某种MouseListener组合来实现此目的)-所有AbstractButton子类都有一个ActionListener队列,用于处理激活按钮的默认平台事件.因此,您应该专注于操作"而不是点击"

If you decided for your implementation to use JButton, you should be aware that usually you don't use "click events" with them (although you, in theory, can use some sort of MouseListener combo to achieve this) - all AbstractButton subclasses have an ActionListener queue handling the default platform event of activating the button. You should thus focus on Actions instead of 'clicks'

http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#abstractbutton

http://docs.oracle. com/javase/7/docs/api/javax/swing/Action.html#buttonActions

如果您确定要始终监视JButton对象上的长按事件,请向ActionListener添加计时器,例如通过System.currentTimeMillis()来检查动作之间的时间差和/或使用带有MousePressed/mouseReleased事件时间度量的MouseListener(所有java.awt.Component子类都定义了addMouseListener())来获取时间增量,以便您可以检测按"的长度.

If you're sure you want to monitor for long press events on JButton objects anyway, add a timer to the ActionListener, e.g. by means of System.currentTimeMillis(), to check the time difference between actions and/or use MouseListener (all java.awt.Component subclasses has addMouseListener() defined) with mousePressed/mouseReleased event time measurement to get the time delta so that you can detect the length of the 'press'.

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

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