我是靠谱客的博主 无辜机器猫,最近开发中收集的这篇文章主要介绍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

最后

以上就是无辜机器猫为你收集整理的java中的distinguish_Java TrayIcon: addMouseListener -> distinguish between single click and double cli...的全部内容,希望文章能够帮你解决java中的distinguish_Java TrayIcon: addMouseListener -> distinguish between single click and double cli...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部