我想将JPopupMenu作为一个TrayIcon (i.e systemTray.add(trayIcon))添加到任务栏中,但我还没有找到一种方法来执行文档,TrayIcon的构造函数类似于:
public TrayIcon(Image image,
String tooltip,
PopupMenu popup)有什么办法我能做到吗?
发布于 2016-03-16 06:09:40
这是一个已知的问题。有一个错误报告,它包含解决方案的大纲。我对此作了如下调整:
// 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);
}
}
});发布于 2013-04-21 14:01:19
对于当前的TrayIcon实现,不可能直接向TrayIcon添加JPopupMenu或JMenu。但是,有些人确实通过实现一个定制的MouseListener来解决这个问题,它只在Trayicon上听到右键单击。当鼠标右键单击时,将弹出菜单的位置设置为event.getXOnScreen()和event.getYOnScreen(),并设置为可见位置。
发布于 2021-06-08 13:47:07
如此兴奋,我发现了一个solution.The的想法如下:
因为我今天也遇到了这个问题,我刚才想到了这个想法,现在我正在努力解决这个问题,现在我正在编写代码。也许我还会遇到其他问题。
https://stackoverflow.com/questions/12667526
复制相似问题