为什么我的系统托盘图标的弹出菜单不响应输入?
我已经在后台java程序上工作了一段时间了,它几乎准备好发布了,所以我想我应该添加一种退出程序的方法。我有这个函数:
private static void setupSysTray() {
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
try {
final PopupMenu popupMenu = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(ImageIO.read(new File(workingDirectory +
fileSeparator + "tray.png")), "Multi");
final SystemTray tray = SystemTray.getSystemTray();
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(e -> {
System.out.println("Something happened!");
System.exit(0);
});
popupMenu.add(exitItem);
trayIcon.setPopupMenu(popupMenu);
tray.add(trayIcon);
} catch (IOException | AWTException e) {
e.printStackTrace();
}
}
我认为这可以处理它,我从各种堆栈溢出帖子和官方文档中将它拼凑在一起。结果是出现托盘图标,并带有正确的图像和工具提示。当我右键单击它时,我看到“退出”菜单项出现。但这就是它崩溃的地方,菜单项没有任何悬停颜色(让我相信输入完全被破坏)并且单击该项目不会显示任何结果。我是否犯了一些愚蠢的错误,例如错误地命令添加项目?这是怎么回事?
I've been working on a background java program for a while and its almost ready to be released so I thought I should probably add a way to exit the program. I have this function:
private static void setupSysTray() {
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
try {
final PopupMenu popupMenu = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(ImageIO.read(new File(workingDirectory +
fileSeparator + "tray.png")), "Multi");
final SystemTray tray = SystemTray.getSystemTray();
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(e -> {
System.out.println("Something happened!");
System.exit(0);
});
popupMenu.add(exitItem);
trayIcon.setPopupMenu(popupMenu);
tray.add(trayIcon);
} catch (IOException | AWTException e) {
e.printStackTrace();
}
}
I presumed this would handle it, I pieced it together from various stack overflow posts and the offical documentation. The result is that the tray icon appears, with the correct image and tooltip. When I right click it I see the menu item for "exit" show up. But that's where it breaks, the menu item doesn't have any hover coloring (leading me to believe input at all with it is broken) and clicking on the item turns up no results. Have I made some silly mistake like ordering the adding of items wrong? What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我在我几乎忘记的旧代码部分中使用了全局鼠标挂钩。该钩子来自 this 存储库,修复方法是将钩子的原始输入设置从 true 更改为 false 。
As it turns out I was making use of a Global Mouse Hook in an old part of the code that I had all but forgotten about. The hook is from this repository and the fix was changing the hook's raw input setting from true to false.