我想将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/