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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:46:59  来源:igfitidea点击:

How to add popup menu in JFrame in netbeans visual editor

javaswinguser-interfacenetbeansjpopupmenu

提问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 JPopupMenuis 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 jPopupMenudeclared 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.

我自己尝试了不同的东西,从我尝试过的东西来看,你似乎无法以可视化的方式设计弹出菜单。不过,您可以使用导航器来设计它。

enter image description here

在此处输入图片说明

  1. From the Navigator, you will see the jPopupMenu1. You can add JMenusor JMenuItemsby right-clicking it and selecting Add from Palette.
  2. You can then add listener to the JMenuItemby right clicking the JMenuItemfrom the navigator and slecting Events -> Actions -> actionPerformed
  1. 从导航器中,您将看到jPopupMenu1. 您可以添加JMenusJMenuItems右键单击它并选择Add from Palette
  2. 然后,您可以JMenuItem通过JMenuItem在导航器中右键单击并选择来将侦听器添加到Events -> Actions -> actionPerformed

To make the JPopupMenuappear, you need to add a MouseListenerto a component, whether it's the frame or another component. For example (to the frame):

要使JPopupMenu出现,您需要向MouseListener组件添加a ,无论是框架还是其他组件。例如(到框架):

  1. Select the frame from the Navigator and right-click it and select Events -> Mouse ->and you will need to implement both mousePressedandmouseReleased, as different platforms have different popup triggers, Windows being mouseReleasedand I think Mac is mousePressed(don't quote me).
  2. Create a method to show the popup menu.

    private void showPopupMenu(MouseEvent e) {
       jPopupMenu1.show(this, e.getX(), e.getY());
    }
    
  3. Implement your mousePressedand mouseReleasedmethods

    private void formMousePressed(MouseEvent evt) {                                  
        if (evt.isPopupTrigger()) {
            showPopupMenu(evt);
        }
    }                                 
    
    private void formMouseReleased(MouseEvent evt) {                                   
        if (evt.isPopupTrigger()) {
            showPopupMenu(evt);
        }
    } 
    
  1. 从导航器中选择框架并右键单击它并选择Events -> Mouse ->,您将需要同时实现mousePressedmouseReleased,因为不同的平台有不同的弹出触发器,Windows 是mouseReleased,我认为 Mac 是mousePressed(不要引用我的话)。
  2. 创建一个方法来显示弹出菜单。

    private void showPopupMenu(MouseEvent e) {
       jPopupMenu1.show(this, e.getX(), e.getY());
    }
    
  3. 实现你的mousePressedmouseReleased方法

    private void formMousePressed(MouseEvent evt) {                                  
        if (evt.isPopupTrigger()) {
            showPopupMenu(evt);
        }
    }                                 
    
    private void formMouseReleased(MouseEvent evt) {                                   
        if (evt.isPopupTrigger()) {
            showPopupMenu(evt);
        }
    } 
    

enter image description here

在此处输入图片说明