Java中的自定义JMenuItems [英] Custom JMenuItems in Java
问题描述
是否可以创建包含按钮的自定义 JMenuItem
?例如,是否可以使用与此类似的项创建 JMenuITem
:
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 ] |
+----------------------------------------+
推荐答案
我怀疑有一种简单的方法可以做到这一点。您可以执行以下操作:
I doubt there is an easy way to do this. You can do something like:
JMenuItem item = new JMenuItem("Edit ");
item.setLayout( new FlowLayout(FlowLayout.RIGHT, 5, 0) );
JButton copy = new JButton("Copy");
copy.setMargin(new Insets(0, 2, 0, 2) );
item.add( copy );
menu.add( item );
但有几个问题:
a)单击按钮时菜单不会关闭。所以代码需要添加到你的ActionListener
a) the menu doesn't close when you click on the button. So that code would need to be added to your ActionListener
b)菜单项不响应左/右箭头等关键事件,所以没办法使用键盘将焦点放在按钮上。这将涉及菜单项的UI更改,我不知道从哪里开始。
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.
我只是使用标准UI设计创建子菜单。
I would just use the standard UI design an create sub menus.
这篇关于Java中的自定义JMenuItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!