gpt4 book ai didi

java:在 Mac OsX 上禁用 TrayIcon 右键单击

转载 作者:行者123 更新时间:2023-11-30 04:23:47 28 4
gpt4 key购买 nike

我正在尝试开发一个由系统托盘图标提供的 Mac OsX 应用程序,因此在第一次尝试使用最简单的代码来实现它后,我注意到 mac osX 上的每个应用程序托盘图标(系统和用户应用程序)( 10.8) 允许通过左键和右键单击来激活相对弹出菜单,但在我的项目中,只有左侧 (MouseEvent.BOTTON1) 按钮会导致弹出菜单下拉。这是我的代码:

public class SystemTrayDemo
{
private SystemTray tray;
private TrayIcon tray_icon;

public SystemTrayDemo()
{
if (!SystemTray.isSupported())
{
JOptionPane.showMessageDialog(null, "System tray not supported!");
return;
}
else
tray = SystemTray.getSystemTray();

final PopupMenu popup = new PopupMenu();

MenuItem exit = new MenuItem("Exit");

exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (tray != null)
{
tray.remove(tray_icon);
System.exit(0);
}
}
});

popup.add(exit);

//add tray icon
tray_icon = new TrayIcon(getIcon("images/wifi.png"), "Open documents...", popup);
tray_icon.setImageAutoSize(true);

try
{
tray.add(tray_icon); // adds icon
}
catch (AWTException ex) {}
}


private Image getIcon(String name)
{
URL _url = getClass().getResource(name);
return new ImageIcon(_url).getImage();
}

public static void main(String args[])
{
new SystemTrayDemo();
}
}

但是我已经说过了,只能通过单击鼠标左键。因此,在进一步的尝试中,我尝试使用 MouseListener 模仿所有其他应用程序的托盘图标的行为,并使用dispatchEvent() 方法在右键单击事件上触发左键事件,如下所示:

    public static void fireMouseEvent(Component c)
{
MouseEvent me = new MouseEvent(c, // which
MouseEvent.MOUSE_CLICKED, // what
System.currentTimeMillis(), // when
MouseEvent.BUTTON1_MASK, // no modifiers
0, 0, // where: at (10, 10}
1, // only 1 click
true); // popup trigger

c.dispatchEvent(me);
}

该事件将由鼠标监听器处理,但显然 TrayIcon 类不是 Component 子类,因此 MouseEvent 的源为 null,我得到一个 NPE。这是我的鼠标监听器:

    class MouseAdapt extends java.awt.event.MouseAdapter
{

public void mouseClicked(java.awt.event.MouseEvent me)
{
int button = me.getButton();

if(button == java.awt.event.MouseEvent.BUTTON3)
{
fireMouseEvent(me.getComponent());
}
}
}

try
{
tray.add(tray_icon); // aggiungi l'icona
tray_icon.addMouseListener(new MouseAdapt());
}
catch (AWTException ex) {}

抱歉我的英语不好,我希望曾经有过此类项目经验的人可以帮助我。我已经搜索了几个小时但没有运气。感谢您的帮助。

最佳答案

编辑:现在有一个库正在努力解决这里的所有问题:https://github.com/dorkbox/SystemTray

<小时/>

to activate the [TrayIcon] relative popup menu with both left and right click

目前这在 Mac + Java 上根本不可能实现。使用反射来调用底层触发器似乎没有帮助。这是一个错误。

https://bugs.openjdk.java.net/browse/JDK-8041890

only the left (MouseEvent.BOTTON1) button causes the popup menu to pulldown. Here's my code

即使这在某些 Java 版本 (7u79) 中也被破坏,但已通过升级修复...

https://bugs.openjdk.java.net/browse/JDK-7158615

跨平台 TrayIcon 支持:

尽管有点偏离主题,但我想补充一点,一些项目使用 JXTrayIcon 在 Linux/Windows 等中完成一些奇特的下拉菜单。尽管有 click-bug it already suffers from today,但这些也会在 Mac 上引起问题以及错误 with Gnome3 requiring a completely separate hack 。但在 Mac 上,任何使用装饰菜单的尝试都会导致菜单停留,对于最终用户来说是一种非常糟糕的体验。我决定的解决方案是使用 AWT for Mac, Swing for everything else 。 Java TrayIcon 支持迫切需要重写。 JavaFX 声称可以帮助这一举措,但它是 staged for Java 9 。与此同时,我坚持使用 OS-dependent hacks

其他平台的相关托盘问题

此外,一些 Linux 发行版(例如 Ubuntu)已经默认删除了 Unity 桌面中的托盘图标,这导致了进一步的麻烦。 https://askubuntu.com/a/457212/412004

此外,在 Gtk/Gnome 或 Qt/KDE 支持的桌面上,图标的透明度被替换为灰色(OpenJDK 和 Oracle JRE 都受到此影响) https://stackoverflow.com/a/3882028/3196753 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6453521

此外,Gnome3 支持的桌面可能会在错误的角落显示它,或者根本不会显示它,或者它可能会显示但无法单击(OpenJDK 和 Oracle JRE 都会遇到这种情况) https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=660157 https://bugzilla.redhat.com/show_bug.cgi?id=1014448

除此之外,Windows 上的高 DPI 屏幕存在错误绘制图标的错误:Windows 8 Distorts my TrayIcon

综上所述,Java 中系统托盘的状态是可以的,但由于综合因素的影响,在 JDK6、JDK7 和 JDK8 中,系统托盘的状态相当分散且有 bug。

关于java:在 Mac OsX 上禁用 TrayIcon 右键单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16378886/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com