English 中文(简体)
向 JPopupMenu 添加 JPopup Menu 到 JPanel 中
原标题:Adding JPopupMenu to JPanel

我的代码:

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?

最佳回答

添加到 JPannel 后,对我有用。

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();
            }
        });
    }
}
问题回答

您没有设置布局, 所以组件可能不会添加到 JFrame 中。 在 JFrame 中设置一个布局, 使用 < code> setLayout (新边框Layout () );

与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());
        }
      }

    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签