Java 中的自定义 JMenuItem
是否可以创建一个包含按钮的自定义 JMenuItem
?例如,是否可以创建一个 JMenuITem
,其项目类似于:
+----------------------------------------+
| JMenuItem [ Button | Button | Button ] |
+----------------------------------------+
Would it be possible to create a custom JMenuItem
that contains buttons? For example would it be possible to create a JMenuITem
with an item similar to this:
+----------------------------------------+
| JMenuItem [ Button | Button | Button ] |
+----------------------------------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑是否有一种简单的方法可以做到这一点。您可以执行以下操作:
但存在几个问题:
a) 单击按钮时菜单不会关闭。因此,需要将该代码添加到您的 ActionListener
b) 菜单项不会响应左/右箭头等关键事件,因此无法使用键盘将焦点放在按钮上。这将涉及菜单项的 UI 更改,但我不知道从哪里开始。
我只会使用标准的 UI 设计来创建子菜单。
I doubt there is an easy way to do this. You can do something like:
But there are several problems:
a) the menu doesn't close when you click on the button. So that code would need to be added to your ActionListener
b) the menu item doesn't respond to key events like the left/right arrow, so there is no way to place focus on the button using the keyboard. This would involve UI changes to the menu item and I have no idea where to start for this.
I would just use the standard UI design an create sub menus.
我确信有,就像我个人一样,我会使用单独的菜单项,并将它们并排放置,并为每个单独的按钮设置一个动作侦听器。棘手的部分是将它们放入 JPanel 等容器中,并将它们放入流布局或网格布局中
I'm sure there is, Like personally I would use individual menuitems and just put them side by side and have an action listener for each individual button. The tricky part would be putting them inside a container like a JPanel and putting them in a flow layout or a Grid layout
老问题,但你可以使用 JToolBar 相当轻松地做到这一点......
Old question, but you can do this fairly easily with a JToolBar...