[英]java - catch event of double click on icon in tray
I want to make my form visible when I double click the tray icon?当我双击托盘图标时,我想让我的表单可见?
How do I catch the double click on the icon?如何捕捉双击图标?
Thanks.谢谢。
Try to use MouseListener with尝试使用 MouseListener
public void mousePressed( MouseEvent e ) {
if(e.getClickCount() >= 2){
//do something
}
}
hmmm, basically all of posts are correct..., but for corect output to the DoubleMouseClick must be wrapped to the javax.swing.Timer嗯,基本上所有帖子都是正确的......但是对于正确的 output 到 DoubleMouseClick 必须包装到 javax.swing.Timer
for example例如
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClickListener extends MouseAdapter implements ActionListener {
private final static int clickInterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
private MouseEvent lastEvent;
private Timer timer;
public ClickListener() {
this(clickInterval);
}
public ClickListener(int delay) {
timer = new Timer(delay, this);
}
@Override
public void mouseClicked(MouseEvent e) {
/*if (e.getClickCount() > 2) {
return;
}
lastEvent = e;
if (timer.isRunning()) {
timer.stop();
doubleClick(lastEvent);
} else {
timer.restart();
}*/
if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) {
System.out.println("double");
timer.stop();
} else {
timer.restart();
}
}
@Override
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() {
@Override
public void singleClick(MouseEvent e) {
System.out.println("single");
}
@Override
public void doubleClick(MouseEvent e) {
System.out.println("double");
}
});
frame.setPreferredSize(new Dimension(200, 200));
frame.pack();
frame.setVisible(true);
}
}
but corerrect for SystemTray with TrayIcon would be add ActionListener但是使用 TrayIcon 的 SystemTray 的正确方法是添加 ActionListener
for example例如
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
public class TrayIconDemo {
public static void main(String[] args) {
try {// Use an appropriate Look and Feel
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);// Turn off metal's use of bold fonts
SwingUtilities.invokeLater(new Runnable() {//Schedule a job for the event-dispatching thread: adding TrayIcon.
@Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
if (!SystemTray.isSupported()) {//Check the SystemTray support
System.out.println("SystemTray is not supported");
return;
}
final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(createImage("Icon/failed.png", "tray icon"));
final SystemTray tray = SystemTray.getSystemTray();
MenuItem aboutItem = new MenuItem("About"); // Create a popup menu components
CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
Menu displayMenu = new Menu("Display");
MenuItem errorItem = new MenuItem("Error");
MenuItem warningItem = new MenuItem("Warning");
MenuItem infoItem = new MenuItem("Info");
MenuItem noneItem = new MenuItem("None");
MenuItem exitItem = new MenuItem("Exit");
popup.add(aboutItem); //Add components to popup menu
popup.addSeparator();
popup.add(cb1);
popup.add(cb2);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(errorItem);
displayMenu.add(warningItem);
displayMenu.add(infoItem);
displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
return;
}
trayIcon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "This dialog box is run from System Tray");
}
});
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "This dialog box is run from the About menu item");
}
});
cb1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
int cb1Id = e.getStateChange();
if (cb1Id == ItemEvent.SELECTED) {
trayIcon.setImageAutoSize(true);
} else {
trayIcon.setImageAutoSize(false);
}
}
});
cb2.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
int cb2Id = e.getStateChange();
if (cb2Id == ItemEvent.SELECTED) {
trayIcon.setToolTip("Sun TrayIcon");
} else {
trayIcon.setToolTip(null);
}
}
});
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MenuItem item = (MenuItem) e.getSource();
System.out.println(item.getLabel()); //TrayIcon.MessageType type = null;
if ("Error".equals(item.getLabel())) {//type = TrayIcon.MessageType.ERROR ;
trayIcon.displayMessage("Sun TrayIcon Demo", "This is an error message", TrayIcon.MessageType.ERROR);
} else if ("Warning".equals(item.getLabel())) {//type = TrayIcon.MessageType.WARNING;
trayIcon.displayMessage("Sun TrayIcon Demo", "This is a warning message", TrayIcon.MessageType.WARNING);
} else if ("Info".equals(item.getLabel())) { //type = TrayIcon.MessageType.INFO;
trayIcon.displayMessage("Sun TrayIcon Demo", "This is an info message", TrayIcon.MessageType.INFO);
} else if ("None".equals(item.getLabel())) {//type = TrayIcon.MessageType.NONE;
trayIcon.displayMessage("Sun TrayIcon Demo", "This is an ordinary message", TrayIcon.MessageType.NONE);
}
}
};
errorItem.addActionListener(listener);
warningItem.addActionListener(listener);
infoItem.addActionListener(listener);
noneItem.addActionListener(listener);
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
System.exit(0);
}
});
}
protected static Image createImage(String path, String description) {//Obtain the image URL
URL imageURL = TrayIconDemo.class.getResource(path);
if (imageURL == null) {
System.err.println("Resource not found: " + path);
return null;
} else {
return (new ImageIcon(imageURL, description)).getImage();
}
}
private TrayIconDemo() {
}
}
Use MouseListener interface使用 MouseListener 接口
public class MouseEventDemo ... implements MouseListener
and implement并实施
public void mouseClicked(MouseEvent e) { }
You can find out the click value from int getClickCount()
it returns the number of quick, consecutive clicks the user has made (including this event).您可以从
int getClickCount()
中找出点击值,它返回用户进行的快速、连续点击的次数(包括此事件)。 For example, returns 2 for a double click.例如,双击返回 2。
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
//Double click code here
}
};
SystemTray tray = SystemTray.getSystemTray();
TrayIcon trayIcon = new TrayIcon(<icon stuff>);
trayIcon.addActionListener(actionListener);
tray.add(trayIcon);
This is how it worked for me这就是它对我有用的方式
if u are planning to write ur own function for this than it go something as follows:如果你打算为此编写你自己的 function 而不是 go 如下:
catch the mouse clicked event, keep a "long timeStamp" initialize it to 0L as an instance variable,捕捉鼠标点击事件,保持“long timeStamp”初始化为0L作为实例变量,
now double click is two clicks within 3 sec or 5 sec现在双击是在 3 秒或 5 秒内单击两次
so
{
if (timeStamp == 0L) {
timeStamp = System.getCurrentTime(); //please chk the exact syntax of this... it gives milli seconds
} else if ((timeStamp + 5000) <= System.getCurrentTime()) // here i am giving time window of 5 sec = 5000 milli seconds
{
//do ur double click code;
then set timeStamp back to 0;
timeStamp = 0L;
} else {
//its first click
timeStamp = System.getCurrentTime();
}
}
trayIcon.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 2) {
setVisible(true);
setExtendedState(JFrame.NORMAL);
}
}
});
tray.add(trayIcon);
This will do the job.这将完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.