System Tray Pop up menu item with Image(Icon to the MenuItem)

血红的双手。 提交于 2019-12-10 11:41:16

问题


I read the system tray tutorial and this similar Stack Overflow question but can't find a good answer. I want to add an image to menu item in J2SE application. In the tutorial, MenuItem is used, but I couldn't find how to add icons to menu items in SystemTray pop up. If JMenuItem is used, icons can easily be placed in MenuItems, but there is MenuItem. How can I add an image to my system tray popmenu?

UpdatedHere, I want to add an image to MenuItem in the popup menu(not to the SystemTray.)


回答1:


You can use a JPopupMenu with your TrayIcon (read here).

trayIcon.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                jpopup.setLocation(e.getX(), e.getY());
                jpopup.setInvoker(jpopup);
                jpopup.setVisible(true);
            }
        }
    });



回答2:


SystemTray have gor implemented simple syntax

TrayIcon(Image, "Narrative", JPopupMenu);

there no required add any additional Listener for displaying JPopupMenu



来源:https://stackoverflow.com/questions/8864035/system-tray-pop-up-menu-item-with-imageicon-to-the-menuitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!