java单击双击,区分Java中的单击和双击

I search the forum and see this codes:

public void mouseClicked(MouseEvent e) {

if (e.getClickCount() == 2) {

System.out.println(" and it's a double click!");

wasDoubleClick = true;

} else {

Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty(

"awt.multiClickInterval");

timer = new Timer(timerinterval.intValue(), new ActionListener() {

public void actionPerformed(ActionEvent evt) {

if (wasDoubleClick) {

wasDoubleClick = false; // reset flag

} else {

System.out.println(" and it's a simple click!");

}

}

});

timer.setRepeats(false);

timer.start();

}

}

but the code runs incorrectly(Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!"). Can anybody show me why? or can you give me some better ways to do this?

Thank you!

解决方案

Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!").

That is normal. A double click only happens if you click twice within the specified time interval. So sometimes if you don't click fast enough you will get two single clicks in a row.

Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

The above line of code determines how fast the double click must be.

For what its worth here is some code I have used to do the same thing. Don't know if its any better or worse than the code you have:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class ClickListener extends MouseAdapter implements ActionListener

{

private final static int clickInterval = (Integer)Toolkit.getDefaultToolkit().

getDesktopProperty("awt.multiClickInterval");

MouseEvent lastEvent;

Timer timer;

public ClickListener()

{

this(clickInterval);

}

public ClickListener(int delay)

{

timer = new Timer( delay, this);

}

public void mouseClicked (MouseEvent e)

{

if (e.getClickCount() > 2) return;

lastEvent = e;

if (timer.isRunning())

{

timer.stop();

doubleClick( lastEvent );

}

else

{

timer.restart();

}

}

public void actionPerformed(ActionEvent e)

{

timer.stop();

singleClick( lastEvent );

}

public void singleClick(MouseEvent e) {}

public void doubleClick(MouseEvent e) {}

public static void main(String[] args)

{

JFrame frame = new JFrame( "Double Click Test" );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.addMouseListener( new ClickListener()

{

public void singleClick(MouseEvent e)

{

System.out.println("single");

}

public void doubleClick(MouseEvent e)

{

System.out.println("double");

}

});

frame.setSize(200, 200);

frame.setVisible(true);

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt,可以通过以下两种方式来区分鼠标单击双击操作: 1. 使用QMouseEvent事件 当鼠标按下并释放时,会触发QMouseEvent事件。在处理QMouseEvent事件时,我们可以通过判断事件类型和时间间隔来区分单击双击操作。例如,我们可以使用QTime类记录上一次鼠标按下的时间,并在本次鼠标按下时计算时间间隔。如果时间间隔小于某个值(例如300ms),则认为是双击操作;否则认为是单击操作。 示例代码: ``` void Widget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { static QTime lastClickTime; int interval = lastClickTime.elapsed(); lastClickTime.start(); if (interval < QApplication::doubleClickInterval()) { qDebug() << "double click"; } else { qDebug() << "single click"; } } } ``` 2. 使用QShortcut快捷键 QShortcut是Qt提供的一种快捷键机制。我们可以通过为Widget对象添加QShortcut对象来捕获鼠标单击双击事件。在QShortcut对象的构造函数,我们可以设置快捷键的组合键和触发方式。例如,我们可以设置快捷键为鼠标左键,并设置触发方式为单击双击。在处理QShortcut对象的activated()信号时,我们就可以区分单击双击操作。 示例代码: ``` void Widget::initShortcuts() { QShortcut *singleClickShortcut = new QShortcut(QKeySequence(Qt::LeftButton), this); connect(singleClickShortcut, &QShortcut::activated, this, &Widget::handleSingleClick); QShortcut *doubleClickShortcut = new QShortcut(QKeySequence(Qt::LeftButton | Qt::DoubleClick), this); connect(doubleClickShortcut, &QShortcut::activated, this, &Widget::handleDoubleClick); } void Widget::handleSingleClick() { qDebug() << "single click"; } void Widget::handleDoubleClick() { qDebug() << "double click"; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值