java中的distinguish_Java TrayIcon: addMouseListener -> distinguish between single click and double cli...

This is an imperfect solution to an imperfect problem. By it's very nature, a double click will generate two mouse events, but a double click is any two clicks which occur within a short period of time between each other.

So, you could insert a small delay on the first click, which if triggered, will "assume" that the event is only a single click.

This example uses a Swing Timer set to 300 milliseconds (you could try 250-275, but I found 300 to be just right). When it detects the first click, it starts the timer, if it detects a second click, it stops the timer, otherwise the timer is allowed to execute after the 300 millisecond delay, which assumes a double click...

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.Timer;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class JavaApplication314 {

public static void main(String[] args) {

new JavaApplication314();

}

public JavaApplication314() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

}

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

private JLabel label;

public TestPane() {

addMouseListener(new MouseHandler());

label = new JLabel("...");

setLayout(new GridBagLayout());

add(label);

}

@Override

public Dimension getPreferredSize() {

return new Dimension(200, 200);

}

protected void doOneClick() {

label.setText("One Click");

}

protected void doTwoClicks() {

label.setText("Two Clicks");

}

public class MouseHandler extends MouseAdapter {

private Timer oneClickTimer;

public MouseHandler() {

oneClickTimer = new Timer(300, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

doOneClick();

}

});

oneClickTimer.setRepeats(false);

}

@Override

public void mouseClicked(MouseEvent e) {

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

oneClickTimer.stop();

doTwoClicks();

} else if (e.getClickCount() == 1) {

oneClickTimer.restart();

}

}

}

}

}

Another option might be would be to use a meta-key (like Alt) to change the state of the menu on a single click

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值