Java 如何在netbeans可视化编辑器的JFrame中添加弹出菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23392464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add popup menu in JFrame in netbeans visual editor
提问by Ahmad Vatani
I already try to add PopUp menu to JFrame by design in Netbeans visual editor, but it don't work. Can anybody step by step hint me how to add it?? Thanks a lots!
我已经尝试在 Netbeans 可视化编辑器中通过设计将弹出菜单添加到 JFrame,但它不起作用。任何人都可以一步一步地提示我如何添加它?非常感谢!
采纳答案by Paul Samsotha
The problem is a JPopupMenu
is not a component that is initially visible or added to a container. So by just dragging and dropping it into the design view frame, will have no affect visually to the design view.
问题是JPopupMenu
不是最初可见或添加到容器的组件。因此,只需将其拖放到设计视图框架中,就不会在视觉上影响设计视图。
But, if you look at the source code or the navigator, you willsee the jPopupMenu
declared as a class member and instantiated in the initComponents()
method.
但是,如果您查看源代码或导航器,您将看到jPopupMenu
声明为类成员并在initComponents()
方法中实例化。
I've attempted different things myself, and from what I've tried, it doesn't look like you can design the popup menu in a visual way. You can though, use the Navigator to design it.
我自己尝试了不同的东西,从我尝试过的东西来看,你似乎无法以可视化的方式设计弹出菜单。不过,您可以使用导航器来设计它。
- From the Navigator, you will see the
jPopupMenu1
. You can addJMenus
orJMenuItems
by right-clicking it and selectingAdd from Palette
. - You can then add listener to the
JMenuItem
by right clicking theJMenuItem
from the navigator and slectingEvents -> Actions -> actionPerformed
- 从导航器中,您将看到
jPopupMenu1
. 您可以添加JMenus
或JMenuItems
右键单击它并选择Add from Palette
。 - 然后,您可以
JMenuItem
通过JMenuItem
在导航器中右键单击并选择来将侦听器添加到Events -> Actions -> actionPerformed
To make the JPopupMenu
appear, you need to add a MouseListener
to a component, whether it's the frame or another component. For example (to the frame):
要使JPopupMenu
出现,您需要向MouseListener
组件添加a ,无论是框架还是其他组件。例如(到框架):
- Select the frame from the Navigator and right-click it and select
Events -> Mouse ->
and you will need to implement bothmousePressed
andmouseReleased
, as different platforms have different popup triggers, Windows beingmouseReleased
and I think Mac ismousePressed
(don't quote me). Create a method to show the popup menu.
private void showPopupMenu(MouseEvent e) { jPopupMenu1.show(this, e.getX(), e.getY()); }
Implement your
mousePressed
andmouseReleased
methodsprivate void formMousePressed(MouseEvent evt) { if (evt.isPopupTrigger()) { showPopupMenu(evt); } } private void formMouseReleased(MouseEvent evt) { if (evt.isPopupTrigger()) { showPopupMenu(evt); } }
- 从导航器中选择框架并右键单击它并选择
Events -> Mouse ->
,您将需要同时实现mousePressed
和mouseReleased
,因为不同的平台有不同的弹出触发器,Windows 是mouseReleased
,我认为 Mac 是mousePressed
(不要引用我的话)。 创建一个方法来显示弹出菜单。
private void showPopupMenu(MouseEvent e) { jPopupMenu1.show(this, e.getX(), e.getY()); }
实现你的
mousePressed
和mouseReleased
方法private void formMousePressed(MouseEvent evt) { if (evt.isPopupTrigger()) { showPopupMenu(evt); } } private void formMouseReleased(MouseEvent evt) { if (evt.isPopupTrigger()) { showPopupMenu(evt); } }