如何捕获 JMenu 元素上的点击?
我正在开发一个带有菜单的 Java/Swing 应用程序,其中使用 JMenuBar、JMenu 和 JMenuItem。
系统外观应用于 UI。
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
我的菜单有几个顶级元素:
JMenu“文件”工作正常,并允许用户选择子菜单,如 JMenuItems“新建”、“打开”...
然后,我想添加第二个顶级元素“关于”(与“文件”级别相同),该元素在单击时显示弹出窗口然后
问题很简单:如何使这个 JMenu“About”元素可点击?
我尝试了几种方法:
将“About”元素设置为 JMenuItem。但是,此元素与其他 JMenu 项(例如“File”)的 UI 不同
将“关于”元素设置为 JMenu 并添加 MouseListener 和KeyListener 就可以了。此元素将具有与其他元素相同的 UI,但我必须单击“关于”弹出窗口的“确定”按钮两次才能捕获此事件。
还有其他解决方案吗?
I'm developping a Java/Swing application with a menu using JMenuBar, JMenu and JMenuItem.
System look and fell is applied to the UI.
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
I have several top level elements for my menu :
The JMenu "File" works fine and allows the user to select submenus like JMenuItems "New", "Open"...
Then, I wanna add a second top level element "About" (same level as "File") that displays a popup when clicking on it.
The issue is quite simple: how to make this JMenu "About" element clickable?
I try several ways:
Set the "About" element as a JMenuItem. However, this element has not the same UI that the other JMenu items like "File"
Set the "About" element as a JMenu and add a MouseListener and a KeyListener on it. This element will have the same UI that others but I have to click twice on the "Ok" button of the "About" popup to catch this event.
Any other solutions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的第一个建议是在 JMenu“关于”菜单中添加一个 JMenuItem。因为这将与其他菜单的操作方式相匹配并且看起来相同。当用户单击“关于”并期望看到菜单但看到弹出窗口时,这也不足为奇。应用程序在菜单栏末尾有一个“帮助”菜单是相当常见的,其中有一个名为“关于”的菜单项,显示一个弹出窗口。单击菜单标题时打开弹出窗口的情况并不常见。
如果您确实必须按照您的建议进行操作,那么:
或者
My first suggestion would be to just add a single JMenuItem inside the JMenu "About" menu. As this would match how the other menus operate and would look the same. It would also not be surprising when a user clicks on "About" expecting a menu but sees a popup instead. It is fairly common for applications to have a "Help" menu on the end of a menu bar that has a menu item named "About" that shows a popup. It is not common for the menu title to open a popup when clicked on.
If you really have to do what your propose then either:
or