从popmenu动作事件中获取右键单击位置

时间:2012-12-01 15:22:26

标签: java swing coordinates jcomponent jpopupmenu

我有一个java程序,可以在JPanel中右键单击时打开一个弹出菜单。单击任何弹出菜单项时,我想打印在终端中触发弹出菜单的右键单击位置。我该怎么做呢?如何在弹出操作事件中获取右键单击的位置?

如果弹出菜单位于JComponent中,代码如何更改?

这是该计划。

    import java.awt.EventQueue;
    import java.awt.event.*;
    import javax.swing.*;

    public class MenuTest
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    MenuFrame frame = new MenuFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }

    class MenuFrame extends JFrame
    {
        public MenuFrame()
        {
            setTitle("MenuTest");
            setSize(300, 200);

            Action cutAction = new TestAction("Cut");
            Action copyAction = new TestAction("Copy");
            Action pasteAction = new TestAction("Paste");

            JPopupMenu popup = new JPopupMenu();
            popup.add(cutAction);
            popup.add(copyAction);
            popup.add(pasteAction);

            JPanel panel = new JPanel();
            panel.setComponentPopupMenu(popup);
            add(panel);

            panel.addMouseListener(new MouseAdapter() {});
        }

        class TestAction extends AbstractAction
        {
            public TestAction(String name)
            {
                super(name);
            }

            public void actionPerformed(ActionEvent event)
            {
                System.out.println("Right click happened at ?"); // How do I get right click location?          
            }
        }
    }

4 个答案:

答案 0 :(得分:4)

为已按下的事件添加鼠标侦听器,(弹出窗口捕获点击的事件):

    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            clickLocation.setSize(e.getX(), e.getY());
        }
    });
    Action cutAction = new TestAction("Cut", clickLocation);
    Action copyAction = new TestAction("Copy", clickLocation);
    Action pasteAction = new TestAction("Paste", clickLocation);

打印尺寸:

    private Dimension clickLocation;

    public TestAction(String name, Dimension clickLocation) {
        super(name);
        this.clickLocation = clickLocation;
    }

    public void actionPerformed(ActionEvent event) {
        System.out.println("Right click happened at " + clickLocation);
    }

答案 1 :(得分:3)

你走在正确的轨道上。我个人更喜欢在MouseAdapter中手动显示它,所以我可以在其他鼠标事件上添加方法。为此,您可能需要删除panel.setComponentPopupMenu(popup);


panel.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (arg0.getButton() == MouseEvent.BUTTON3) { //Button3 is rightclick
                popup.show(panel, arg0.getX(), arg0.getY());
            }
        }
});

答案 2 :(得分:0)

这是我正在寻找的代码。谢谢Schippi和Garrett的帮助。

    import java.awt.EventQueue;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;

    public class MenuTest
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    MenuFrame frame = new MenuFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }

    class MenuFrame extends JFrame
    {
        public MenuFrame()
        {
            setTitle("MenuTest");
            setSize(300, 200);

            Action cutAction = new TestAction("Cut");
            Action copyAction = new TestAction("Copy");
            Action pasteAction = new TestAction("Paste");

            JPopupMenu popup = new JPopupMenu();
            popup.add(cutAction);
            popup.add(copyAction);
            popup.add(pasteAction);

            JPanel panel = new JPanel();
            panel.setComponentPopupMenu(popup);
            add(panel);

            panel.addMouseListener(new MouseAdapter() { 
                @Override
                public void mousePressed(MouseEvent e) {
                    clickLocation= e.getPoint();
                }
            });
        }

        class TestAction extends AbstractAction
        {
            public TestAction(String name)
            {
                super(name);
            }

            public void actionPerformed(ActionEvent event)
            {
                System.out.println("Right click happened at (" + clickLocation.getX()+"," + clickLocation.getY()+ ")");         
            }
        }

        private Point2D clickLocation;
    }

答案 3 :(得分:0)

或者如果您不想从活动中获取它。

Point mousepospoint=null;

if((mousepospoint=componentname.getMousePosition()) != null){
//mouseposArray[0]=mousepospoint.x;
//mouseposArray[1]=mousepospoint.y;
mousepoints(mousepospoint.x,mousepospoint.y);
}//enif


int[] mouseposArray={0,0};
// requires a function to return it if mouseposArray[] is global
protected int[] mousepoints(int xpo,int ypo){
mouseposArray=new int[2];
mouseposArray[0]=xpo;
mouseposArray[1]=ypo;
return mouseposArray;
}//enmeth