为什么我的系统托盘图标的弹出菜单不响应输入?

发布于 2025-01-14 18:03:06 字数 1050 浏览 0 评论 0原文

我已经在后台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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-01-21 18:03:06

事实证明,我在我几乎忘记的旧代码部分中使用了全局鼠标挂钩。该钩子来自 this 存储库,修复方法是将钩子的原始输入设置从 true 更改为 false 。

// True instead of false seems to block JavaFX events
private static GlobalMouseHook mouseHook = new GlobalMouseHook(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.

// True instead of false seems to block JavaFX events
private static GlobalMouseHook mouseHook = new GlobalMouseHook(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文