java 将 JPopupMenu 添加到 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10786037/
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-10-31 02:34:34  来源:igfitidea点击:

Adding JPopupMenu to JPanel

javaswingjpopupmenu

提问by user1304098

My code:

我的代码:

class PanelGlowny extends JPanel implements ActionListener{}

public class Formatka extends JFrame implements ActionListener{

private JMenuItem klienciMenuItem = new JMenuItem("Klienci");
private JPopupMenu menuPopup = new JPopupMenu();
private PanelGlowny panelGlowny = new PanelGlowny();


public Formatka() {

    add(panelGlowny, BorderLayout.CENTER);
    menuPopup.add(klienciMenuItem);
    panelGlowny.setComponentPopupMenu(menuPopup);


   }
}

And i do not see popupmenu when i click right button on mouse. Why?

当我单击鼠标右键时,我没有看到弹出菜单。为什么?

回答by trashgod

Works for me when added to a JPanel.

添加到JPanel.

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class Formatka extends JPanel {

    private JMenuItem klienciMenuItem = new JMenuItem("Klienci");
    private JPopupMenu menuPopup = new JPopupMenu();

    public Formatka() {
        this.add(new JLabel("Right-click for popup menu."));
        menuPopup.add(klienciMenuItem);
        this.setComponentPopupMenu(menuPopup);
    }

    private void display() {
        JFrame f = new JFrame("Formatka");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Formatka().display();
            }
        });
    }
}

回答by Hidde

You did not set a Layout, so the component is probably not added to the JFrame. Set a layout to the JFrame with setLayout(new BorderLayout());.

您没有设置布局,因此组件可能没有添加到 JFrame。使用 将布局设​​置为 JFrame setLayout(new BorderLayout());

回答by Harsha

This works with JFrame :D

这适用于 JFrame :D

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JFrame;

    public class Popup extends JFrame{
        JMenuItem item1,item2;
        static JPopupMenu pop;

        Popup(){
          item1= new JMenuItem("This is Menu Item");
          item2= new JMenuItem("This is another Menu Item");

          pop= new JPopupMenu();

          MouseListener popListener = new PopupListener();

          pop.add(item1);
          pop.add(item2);

          addMouseListener(popListener);        

          setLocationRelativeTo(null);
          pack();
          setVisible(true);
    }

      public static void main(String a []){
          new Popup();
      }

    }

    class PopupListener extends MouseAdapter{

     public void mousePressed(MouseEvent e) {
          maybeShowPopup(e);
          }

       public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
       }

       private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
            Popup.pop.show(e.getComponent(),
                    e.getX(), e.getY());
        }
      }

    }