在java中识别双击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4051659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
identifying double click in java
提问by Suraj Air
I want to know how can we perform action when mouse is double clicked in a component.
我想知道在组件中双击鼠标时我们如何执行操作。
采纳答案by kaliatech
public void mouseClicked(MouseEvent event)
{
if (event.getClickCount() == 2) {
System.out.println("double clicked");
}
}
回答by Sean Patrick Floyd
Assuming you mean in Swing, assign a MouseListener to your Component:
假设您的意思是在 Swing 中,为您的组件分配一个 MouseListener:
addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2){
// your code here
}
}
});
Reference:
参考:
- Java Tutorial: How to write a Mouse Listener
- Java 教程:如何编写鼠标监听器
回答by davidxxx
The e.getClickCount()==2
is not enough if you want to allow your users to do multiple double clicks in a short delay.
You are limited by the desktop configuration.
You can get it by looking the result of Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
的e.getClickCount()==2
,如果你想允许用户做多双击在很短的延迟是不够的。您受到桌面配置的限制。你可以通过查看结果来获得它 Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
A good way to bypass the problem is not to use the getClickCount()
check but to use a Timer
where you can choose the interval max between your clicks and to handle by oneself the count (very simple).
绕过这个问题的一个好方法是不使用getClickCount()
检查,而是使用一个Timer
你可以选择点击之间的最大间隔并自己处理计数(非常简单)的地方。
The code associated :
相关代码:
boolean isAlreadyOneClick;
@Override
public void mouseClicked(MouseEvent mouseEvent) {
if (isAlreadyOneClick) {
System.out.println("double click");
isAlreadyOneClick = false;
} else {
isAlreadyOneClick = true;
Timer t = new Timer("doubleclickTimer", false);
t.schedule(new TimerTask() {
@Override
public void run() {
isAlreadyOneClick = false;
}
}, 500);
}
}
Tested with Win Xp OS and perfect.
用 Win Xp OS 测试过,完美。
回答by Warren
My problem is that I have to respond one way if the user single clicks, another if they click more than one time (my Swing VM seems to be able to count up to four clicks when I click multiple times). When I ran the example above, it seemed to count a triple click as a single one. So, here is my rewrite. Basically, I just have a scheduled task that waits until the dust clears and then checks the number of clicks registered. The 400 ms wait seems to work best for me.
我的问题是,如果用户单击一次,我必须以一种方式响应,如果用户单击多次,则必须以另一种方式响应(当我多次单击时,我的 Swing VM 似乎最多可以计算四次点击)。当我运行上面的示例时,它似乎将三次单击视为一次单击。所以,这是我的重写。基本上,我只是有一个计划任务,它会等待灰尘清除,然后检查注册的点击次数。400 毫秒的等待似乎最适合我。
JButton jButton = new JButton("Click Me!");
jButton.addMouseListener(new MouseAdapter() {
private int eventCnt = 0;
java.util.Timer timer = new java.util.Timer("doubleClickTimer", false);
@Override
public void mouseClicked(final MouseEvent e) {
eventCnt = e.getClickCount();
if ( e.getClickCount() == 1 ) {
timer.schedule(new TimerTask() {
@Override
public void run() {
if ( eventCnt == 1 ) {
System.err.println( "You did a single click.");
} else if ( eventCnt > 1 ) {
System.err.println("you clicked " + eventCnt + " times.");
}
eventCnt = 0;
}
}, 400);
}
}
});