java 使用java左键单击任务托盘图标上的弹出窗口(菜单)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11428042/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:04:56  来源:igfitidea点击:

Open popup(Menu) on task tray icon with left click using java

javaswingtrayiconjpopupmenu

提问by NoNaMe

I am working on task tray Icon in java, I like to open a popup Menu using left click same popup Menu as I open on right click, and please help me with a quick response.

我正在 Java 中处理任务托盘图标,我喜欢使用左键单击与右键单击相同的弹出菜单来打开弹出菜单,请帮助我快速响应。

Thanks in advance...

提前致谢...

here is the code working for right click need to show same popup on left click... don't forget to place any image @ "src/img" folder with name "titleImg.jpg"

这是用于右键单击的代码需要在左键单击时显示相同的弹出窗口...不要忘记放置名称为“titleImg.jpg”的任何图像@“src/img”文件夹

Just run this... it is a working example but i have to show same popup using left click

只需运行它...这是一个工作示例,但我必须使用左键单击显示相同的弹出窗口

i have checked the Mouse Listener, it listen the left click on tray icon but how to show popup menu using that ???

我检查了鼠标监听器,它监听托盘图标上的左键单击,但如何使用它显示弹出菜单???

    package com.abc.dao;

import java.awt.AWTException;
import java.awt.CheckboxMenuItem;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;

public class MyTaskTray {
    public static void main(String arg[]){

        //Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon =
                new TrayIcon(Toolkit.getDefaultToolkit().getImage(new java.io.File("").getAbsolutePath()+"/bin/img/titleImg.jpg"), "Library Drop");
        final SystemTray tray = SystemTray.getSystemTray();

        // Create a pop-up menu components
        MenuItem aboutItem = new MenuItem("About");
        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");

        //Add components to pop-up menu
        popup.add(aboutItem);
        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.");
        }

    }
}

回答by Guillaume Polet

What you actually lack is a parent component to show your PopupMenu. One way to achieve this, is to use an "invisible" frame (actually it is visible but with 0-bounds and undecorated, so you can't see it) like this:

您实际上缺少的是显示您的 PopupMenu 的父组件。实现此目的的一种方法是使用“不可见”框架(实际上它是可见的,但具有 0 边界且未装饰,因此您看不到它),如下所示:

import java.awt.AWTException;
import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URL;

public class MyTaskTray {
    public static void main(String arg[]) throws MalformedURLException {
        final Frame frame = new Frame("");
        frame.setUndecorated(true);
        // Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().getImage(
                new URL("http://home.comcast.net/~supportcd/Icons/Java_Required.jpg")), "Library Drop");
        final SystemTray tray = SystemTray.getSystemTray();

        // Create a pop-up menu components
        final PopupMenu popup = createPopupMenu();
        trayIcon.setPopupMenu(popup);
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    frame.add(popup);
                    popup.show(frame, e.getXOnScreen(), e.getYOnScreen());
                }
            }
        });
        try {
            frame.setResizable(false);
            frame.setVisible(true);
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
        }

    }

    protected static PopupMenu createPopupMenu() {
        final PopupMenu popup = new PopupMenu();
        MenuItem aboutItem = new MenuItem("About");
        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");
        // Add components to pop-up menu
        popup.add(aboutItem);
        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);
        return popup;
    }
}

As of Java 1.7, you can add the following line to remove the application bar from the taskbar:

从 Java 1.7 开始,您可以添加以下行以从任务栏中删除应用程序栏:

frame.setType(Type.UTILITY);

回答by mKorbel

you can add ActionListenerto the TrayIcon, mouse double_click can showing JOptionPane

您可以添加ActionListenerTrayIcon,鼠标双击可以显示JOptionPane

trayIcon.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
       JOptionPane.showMessageDialog(null, "This dialog box is run from System Tray");
   }
});

回答by David Kroukamp

I think you are looking for a MouseListenerwhich you will add to your TrayIconand will activate when a button on the mouse is clicked,moved etc. To get it to operate for left clicks only have a look at the ButtonMasks on MouseEvent(BUTTON1) being for left mouse clicks.

我认为您正在寻找一个MouseListener您将添加到您的TrayIcon并将在单击、移动鼠标​​上的按钮时激活的按钮。要让它为左键单击操作,只需查看MouseEvent( BUTTON1)上的 ButtonMasks用于鼠标左键点击。

回答by MadProgrammer

回答by acdcjunior

This should work:

这应该有效:

trayIcon.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        JOptionPane.showMessageDialog(null, "This shows after a left-click on tray icon");
    }
});

Override any other methods if you want a different kind of event (not just the click event from the example above).

如果您想要不同类型的事件(不仅仅是上面示例中的点击事件),请覆盖任何其他方法。