在Java中,当我左键单击TrayIcon时,如何显示弹出菜单?

在Java中,当我左键单击TrayIcon时,如何显示弹出菜单?,java,mouselistener,trayicon,tray,Java,Mouselistener,Trayicon,Tray,当前,当我右键单击系统托盘中的TrayIcon时,弹出菜单将显示。然而,当我左键单击TrayIcon时,我希望它也这样做 我想我可以通过在TrayIcon上使用mouseListener来实现这一点,但是我不知道在mouseClicked事件中调用什么方法来实现期望的结果 icon = new TrayIcon(img, tooltip, popup); icon.addMouseListener( new MouseAdapter() {

当前,当我右键单击系统托盘中的TrayIcon时,弹出菜单将显示。然而,当我左键单击TrayIcon时,我希望它也这样做

我想我可以通过在TrayIcon上使用mouseListener来实现这一点,但是我不知道在mouseClicked事件中调用什么方法来实现期望的结果

icon = new TrayIcon(img, tooltip, popup);

     icon.addMouseListener(
           new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
                 popup.setEnabled(true);
              }
           });

当我左键单击TrayIcon时,使用setEnabled()方法不会使弹出菜单出现。它实际上没有明显的效果。我想知道我应该在mouseClicked()主体中使用什么方法,以便在单击鼠标左键时显示弹出窗口。

您试图执行的操作显然是不可能的:

你不能用
show
方法显示
PopupMenu
,因为你需要指定一个
JComponent
,但是你的
TrayIcon
不是一个(奇怪的是,
TrayIcon
仍然能够做到这一点,所以显然有一个方法,不过不要问我..)。因此,正如MadProgrammer所建议的,您应该尝试使用
jpopmpmenu
。不要将其添加到您的
TrayIcon
中,因为这样做是不可能的,而是通过在
TrayIcon
中添加
MouseListener来显示您的
jpopmenu
。这应该可以做到:

final TrayIcon tray = new TrayIcon( img, tooltip, null);
final JPopupMenu menu = new JPopupMenu();
... // your menu initialization.
tray.addMouseListener( new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent evt) {
        menu.setLocation( evt.getPoint() );
        menu.setVisible( true );
    }
}

基本上,在鼠标侦听器中,您需要确定按下了哪个按钮(可选,按下了多少次)

关键的代码是

if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) { ... }
我还包括了一些额外的代码,确保弹出窗口不会覆盖任务栏,并显示在屏幕的可视区域内(这是我的一个挑剔;)

公共类TestTrayIcon02{
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(例外情况除外){
}
试一试{
final TrayIcon ti=new TrayIcon(ImageIO.read(getClass().getResource(“/Smiley.png”),“祝您有愉快的一天”);
最终JPopupMenu弹出窗口=新建JPopupMenu();
JMenuItem mi=新的JMenuItem(“给我一些”);
mi.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
SystemTray.getSystemTray().remove(ti);
系统出口(0);
}
});
弹出。添加(mi);
ti.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
如果(e.getButton()==MouseEvent.BUTTON1&&e.getClickCount()==1){
矩形边界=getSafeScreenBounds(如getPoint());
Point=e.getPoint();
int x=点x;
int y=点y;
if(ybounds.y+bounds.height){
y=边界。y+边界。高度;
}
if(xbounds.x+bounds.width){
x=边界.x+边界.width;
}
if(x+popup.getPreferredSize().width>bounds.x+bounds.width){
x=(bounds.x+bounds.width)-popup.getPreferredSize().width;
}
如果(y+popup.getPreferredSize().height>bounds.y+bounds.height){
y=(bounds.y+bounds.height)-popup.getPreferredSize().height;
}
设置位置(x,y);
popup.setVisible(true);
}
}
});
SystemTray.getSystemTray().add(ti);
}捕获(例外情况除外){
例如printStackTrace();
}
}
});
}
公共静态矩形getSafeScreenBounds(点位置){
矩形边界=getScreenBoundsAt(位置);
插图插图=获取屏幕插图插图(位置);
bounds.x+=左插图;
bounds.y+=insets.top;
bounds.width-=(插图左+插图右);
bounds.height-=(insets.top+insets.bottom);
返回边界;
}
公共静态插图GetScreen插图(点位置){
GraphicsDevice gd=GetGraphicsDevice(位置);
Insets Insets=null;
如果(gd!=null){
insets=Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
}
返回插图;
}
公共静态矩形getScreenBoundsAt(点位置){
GraphicsDevice gd=GetGraphicsDevice(位置);
矩形边界=空;
如果(gd!=null){
bounds=gd.getDefaultConfiguration().getBounds();
}
返回边界;
}
公共静态图形设备获取图形设备(点位置){
GraphicsDevice设备=空;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[]=ge.getScreenDevices();
ArrayList lstDevices=新的ArrayList(lstGDs.length);
用于(图形设备gd:lstGDs){
GraphicsConfiguration gc=gd.getDefaultConfiguration();
矩形scre
public class TestTrayIcon02 {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }
                try {
                    final TrayIcon ti = new TrayIcon(ImageIO.read(getClass().getResource("/Smiley.png")), "Have a nice day");
                    final JPopupMenu popup = new JPopupMenu();

                    JMenuItem mi = new JMenuItem("Get me some");
                    mi.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            SystemTray.getSystemTray().remove(ti);
                            System.exit(0);
                        }
                    });

                    popup.add(mi);
                    ti.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) {
                                Rectangle bounds = getSafeScreenBounds(e.getPoint());
                                Point point = e.getPoint();
                                int x = point.x;
                                int y = point.y;
                                if (y < bounds.y) {
                                    y = bounds.y;
                                } else if (y > bounds.y + bounds.height) {
                                    y = bounds.y + bounds.height;
                                }
                                if (x < bounds.x) {
                                    x = bounds.x;
                                } else if (x > bounds.x + bounds.width) {
                                    x = bounds.x + bounds.width;
                                }
                                if (x + popup.getPreferredSize().width > bounds.x + bounds.width) {
                                    x = (bounds.x + bounds.width) - popup.getPreferredSize().width;
                                }
                                if (y + popup.getPreferredSize().height > bounds.y + bounds.height) {
                                    y = (bounds.y + bounds.height) - popup.getPreferredSize().height;
                                }
                                popup.setLocation(x, y);
                                popup.setVisible(true);
                            }
                        }
                    });

                    SystemTray.getSystemTray().add(ti);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static Rectangle getSafeScreenBounds(Point pos) {

        Rectangle bounds = getScreenBoundsAt(pos);
        Insets insets = getScreenInsetsAt(pos);

        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= (insets.left + insets.right);
        bounds.height -= (insets.top + insets.bottom);

        return bounds;

    }

    public static Insets getScreenInsetsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Insets insets = null;
        if (gd != null) {
            insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
        }
        return insets;
    }

    public static Rectangle getScreenBoundsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Rectangle bounds = null;
        if (gd != null) {
            bounds = gd.getDefaultConfiguration().getBounds();
        }
        return bounds;
    }

    public static GraphicsDevice getGraphicsDeviceAt(Point pos) {

        GraphicsDevice device = null;

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();

        ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

        for (GraphicsDevice gd : lstGDs) {

            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            Rectangle screenBounds = gc.getBounds();

            if (screenBounds.contains(pos)) {

                lstDevices.add(gd);

            }

        }

        if (lstDevices.size() > 0) {
            device = lstDevices.get(0);
        } else {
            device = ge.getDefaultScreenDevice();
        }

        return device;

    }
}