gpt4 book ai didi

java - 将 JPopupMenu 添加到 TrayIcon

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:04 24 4
gpt4 key购买 nike

我想将 JPopupMenu 作为 TrayIcon 添加到任务栏( systemTray.add(trayIcon)) 但我还没有找到这样做的方法。来自 docs TrayIcon 的构造函数如下所示:

public TrayIcon(Image image,
String tooltip,
PopupMenu popup)

有什么办法可以做到这一点吗?

最佳答案

这是一个已知问题。有一个 bug report ,其中包含解决方法的概述。我在下面进行了调整:

// Build your popup menu
final JPopupMenu trayPopup = new JPopupMenu();
// I'm using actions, there are other ways of doing this.
trayPopup.add(someFantaticAction);

// Get your tray icon
trayIcon = new TrayIcon(icon, "My awesome application");

trayIcon.addMouseListener(new MouseAdapter() {

@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}

@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}

private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
trayPopup.setLocation(e.getX(), e.getY());
trayPopup.setInvoker(trayPopup);
trayPopup.setVisible(true);
}
}
});

关于java - 将 JPopupMenu 添加到 TrayIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12667526/

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