简体   繁体   English

如何将子菜单添加到MenuItem

[英]How to add submenu to MenuItem

I'm trying to add submenu to a MenuItem which exists in a popup menu in system tray. 我试图将子菜单添加到存在于系统托盘弹出菜单中的MenuItem中。 Is there any way to achieve this? 有什么办法可以做到这一点? I've found some solutions about submenus but they use JMenuItem , and TrayIcon only accepts PopupMenu which only accepts MenuItem s. 我找到了一些有关子菜单的解决方案,但是它们使用JMenuItem ,并且TrayIcon仅接受PopupMenu ,后者仅接受MenuItem

Trying to achieve this with MenuItem : 试图通过MenuItem实现:

图片

A JMenuItem doesn't support submenus, you need to use another JMenu (add to you JPopupMenu ). JMenuItem不支持子菜单,您需要使用另一个JMenu (添加到JPopupMenu )。 See How to Use Menus for more details 有关更多详细信息,请参见如何使用菜单

For example... 例如...

在此处输入图片说明

JPopupMenu popupMenu = new JPopupMenu();

JMenu deviceMenu = new JMenu("Add Device");
deviceMenu.add(new JMenuItem("Add More..."));

popupMenu.add(deviceMenu);
popupMenu.add(new JMenuItem("Delete Device"));
popupMenu.add(new JMenuItem("Fire"));
popupMenu.add(new JMenuItem("Fault"));
popupMenu.add(new JMenuItem("Supress"));

(obviously, you'll still need to plugin functionality for all of this) (显然,您仍然需要为此添加插件功能)

and TrayIcon only accepts PopupMenu which only accepts MenuItems. TrayIcon仅接受PopupMenu,后者仅接受MenuItems。

There's a trick, you have to cheat a little, take a look at How do I get a PopupMenu to show up when I left-click on a TrayIcon in Java? 有一个窍门,您必须作弊,看看在Java中用鼠标右键单击TrayIcon时如何显示PopupMenu? for an example 举个例子

Sure, you just add a Menu called "Add Device" to the PopupMenu ( Menu is a subclass of MenuItem , so it can be added). 当然,你只需要添加一个Menu名为“添加设备”到PopupMenuMenu是一个子类MenuItem ,因此它可以被添加)。

PopupMenu popupMenu = new PopupMenu();
Menu subMenu = new Menu("Add Device");
subMenu.add(new MenuItem("Add More .."));
popupMenu.add(subMenu);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM