不是的JPopupMenu在屏幕上显示?

不是的JPopupMenu在屏幕上显示?

问题描述:

所以,为我校的项目我创建一个类图壶。我用它做95%,我需要的是让Jpopup菜单出现。在核心我有3个文件。延伸JPanel并ClassModel这使得矩形(图中)的应用模型延伸JFrame中,类别图出现。渲染的核心是对矩形物体,中间和底部的矩形内的文本被另一个看不见的矩形,这是正确的,可点击包围。

So for my school project I am creating a Class Diagram maker. I am 95% done with it and all I need is to make the Jpopup menu appear. In the core I have 3 files. The ApplicationModel which extends the JFrame, ClassDiagram which extends the JPanel and ClassModel which makes the Rectangles (in the picture) appear. The core of the rendering is on Rectangle objects and the text inside the middle and bottom rectangles are surrounded by another invisible rectangle, which is right-clickable.

这档节目是什么样子(减去油漆EDITTING)
现在对于处理该点击是DiagramMouseListener文件,这里是code吧。

This is what the program looks like (Minus the paint editting) Now for the file that handles the clicking is DiagramMouseListener, here is the code for it.

package edu.mville.cs.classdiagram;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class DiagramMouseListener extends MouseAdapter
{
    ClassDiagram diagram;
    Field field;
    Method method;
    int x;
    int y;
    ClassModel elementBeingDragged;

    JPopupMenu fieldPopupMenu = new JPopupMenu();
    JPopupMenu methodPopupMenu = new JPopupMenu();

    JMenuItem editFieldNameItem;
    JMenuItem createFieldItem;
    JMenuItem deleteFieldItem;

    JMenuItem editMethodNameItem;
    JMenuItem createMethodItem;
    JMenuItem deleteMethodItem;

    public DiagramMouseListener(ClassDiagram diagram) { this.diagram = diagram; }

    public void addPopupMenu()
    {
        editFieldNameItem = new JMenuItem("Edit Field Name");
        createFieldItem = new JMenuItem("New Field");
        deleteFieldItem = new JMenuItem("Delete Field");

        editMethodNameItem = new JMenuItem("Edit Method Name");
        createMethodItem = new JMenuItem("New Method");
        deleteMethodItem = new JMenuItem("Delete Method");

        methodPopupMenu.add(editMethodNameItem);
        methodPopupMenu.add(createMethodItem);
        methodPopupMenu.add(deleteMethodItem);

        fieldPopupMenu.add(editFieldNameItem);
        fieldPopupMenu.add(createFieldItem);
        fieldPopupMenu.add(deleteFieldItem);

        editFieldNameItem.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {

            }
        });
        /*
        createFieldItem.addActionListener(this);
        deleteFieldItem.addActionListener(this);
        editMethodNameItem.addActionListener(this);
        createMethodItem.addActionListener(this);
        deleteMethodItem.addActionListener(this);
        */
    }

    @Override
    public void mouseClicked(MouseEvent me)
    {        
        if(SwingUtilities.isLeftMouseButton(me) && me.getClickCount() == 2)
        {
            diagram.doubleClick(me.getPoint());
        }
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        x = e.getX();
        y = e.getY();

        DiagramElement elt = diagram.containsPoint(e.getPoint());
        if (elt instanceof ClassModel)
        {
            elementBeingDragged = (ClassModel) elt;
        }
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        int dx = e.getX() - x;
        int dy = e.getY() - y;

        if (elementBeingDragged != null)
        {
            elementBeingDragged.move(dx, dy);

            diagram.repaint();
        }

        x += dx;
        y += dy;
    }

    @Override
    public void mouseReleased(MouseEvent me)
    {
        elementBeingDragged = null;

        DiagramElement de = diagram.containsPoint(me.getPoint());

        if (SwingUtilities.isRightMouseButton(me) && me.getClickCount() == 1 && de instanceof Field)
        {
            if (me.isPopupTrigger())
            {
                System.out.println("it is");
                fieldPopupMenu.show(me.getComponent(), me.getX(), me.getY());
            }
        }
        else if (SwingUtilities.isRightMouseButton(me) && me.getClickCount() == 1 && de instanceof Method)
        {
            if (me.isPopupTrigger())
            {
                System.out.println("it is");
                methodPopupMenu.show(me.getComponent(), me.getX(), me.getY());
            }
        }  
    }
}

目前线118,它说的System.out.println(是);它成功地显示在控制台上的文本,它告诉我,code顺利到达那一部分,但是当我用鼠标右键单击该文本(也就是内部由5个像素的空间分隔无形矩形),在弹出菜单中永远不会显示出来。

At line 118 where it says System.out.println("it is"); It successfully displays the text on console, which tells me the code successfully reached that part, but the popup menu is never displayed when I right click the text (which is inside invisible Rectangles separated by 5 pixels of space).

我想这个问题的多种解决方案。我甚至看了看oracle的教程和其他用户的例子,看看什么是错我的code。但搜索的无数个小时后,我没能解决这个问题。任何帮助将是AP preciated。此外,如果你需要更多的信息,我会很乐意提供!谢谢你。

I tried multiple solutions to this problem. I even looked at the oracle tutorials and other user's examples to see what was wrong with my code. But after countless hours of searching, I failed to fix the problem. Any help would be appreciated. Also if you need more information, I will be glad to provide! Thanks.

有几件事情;

首先,在弹出的坐标应该是相对于您所触发弹出组件,而不是屏幕坐标。正在发生的事情是,API被计算组件的屏幕位置,增加您通过X / Y值,这可能是推动弹出关闭屏幕

First off, the popup coordinates should be relative to the component you are triggering the popup on, not the screen coordinates. What is happening is, the API is calculating the screen location of the component and adding the x/y values you pass, which is possibly pushing the popup off the screen

fieldPopupMenu.show(me.getComponent(), me.getX(), me.getY());

其次,弹出窗口,可以通过不同的事件不同的系统触发。你应该pssed 小鼠$ P $来检查的isPopupTrigger 的mouseReleased ,甚至的mouseClicked

最后,弹出窗口,可以通过不同的鼠标按键(甚至可能还有其他条件)触发的,所以应该只需要检查的isPopupTrigger

Lastly, popups can be triggered by different mouse buttons (and even possibly other conditions), so should only need to check isPopupTrigger

此外,你可以只使用 的JComponent#setComponentPopupMenu

Additionally, you could just use JComponent#setComponentPopupMenu

更新与 setComponentPopupMenu 例如

Updated with setComponentPopupMenu example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PopupMenuTest {

    public static void main(String[] args) {
        new PopupMenuTest();
    }

    public PopupMenuTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPopupMenu popupMenu;

        public TestPane() {
            popupMenu = new JPopupMenu();
            popupMenu.add(new JMenuItem("Open..."));
            popupMenu.add(new JMenuItem("Save..."));
            popupMenu.add(new JMenuItem("Close..."));
            popupMenu.add(new JMenuItem("Give Blood..."));
            popupMenu.add(new JMenuItem("Give Money..."));

            setComponentPopupMenu(popupMenu);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

更新与的MouseListener 例如

Updated with MouseListener example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PopupMenuTest {

    public static void main(String[] args) {
        new PopupMenuTest();
    }

    public PopupMenuTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPopupMenu popupMenu;

        public TestPane() {
            popupMenu = new JPopupMenu();
            popupMenu.add(new JMenuItem("Open..."));
            popupMenu.add(new JMenuItem("Save..."));
            popupMenu.add(new JMenuItem("Close..."));
            popupMenu.add(new JMenuItem("Give Blood..."));
            popupMenu.add(new JMenuItem("Give Money..."));

            addMouseListener(new MouseAdapter() {

                protected void doPopup(MouseEvent evt) {
                    if (evt.isPopupTrigger()) {
                        popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
                    }
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    doPopup(e);
                }

                @Override
                public void mousePressed(MouseEvent e) {
                    doPopup(e);
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    doPopup(e);
                }

            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}