MouseMotionListener在Java中无法正确触发

时间:2012-04-10 20:35:24

标签: java swing mouseevent

任何人都可以告诉我为什么mouseMotionListener没有触发mouseDragged事件(我已经google了几个小时,甚至从网上复制粘贴代码!)下面是该类的代码 - 我创建了一个类的对象并将其添加到名为canvas的JPanel。

PS mousePressed()是唯一被触发的方法,mouseDragged()和mouseReleased(),它们没有。

class MouseActions extends MouseInputAdapter 
{
        @Override
        public void mousePressed(MouseEvent e) 
        {
            super.mousePressed(e);//245 220
            java.awt.Point Pos = e.getPoint();
            System.out.println("at Mouse Pressed, Again");
            if(e.getButton() == MouseEvent.BUTTON3)
            {
                if(ArrayOfShapes == null)
                    return;

                for(int i = 0; i < ArrayOfShapes.length; i++)
                {
                    if(hasEntered(ArrayOfShapes[i], Pos))
                    {
                        removeAtIndex(i);
                        return;
                    }
                }
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) 
        {
           System.out.println("at Mouse Dragged");
            int MovableIndex = -1;
            java.awt.Point Pos = e.getPoint();

            if(e.getButton() == MouseEvent.BUTTON1)
            {
                bDragged = true;

                while(bDragged)
                {
                    for(int i = 0; i < ArrayOfShapes.length; i++)
                    {
                        if(hasEntered(ArrayOfShapes[i], Pos))
                        {
                            MovableIndex = i;
                            break;
                        }
                    }
                    ArrayOfShapes[MovableIndex].setX(e.getX());
                    ArrayOfShapes[MovableIndex].setY(e.getY());
                    thisCurrentWindow.repaint();
                }
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) 
        {
            System.out.println("at Mouse Release");
            bDragged = false;
        }

    }

///现在添加监听器的代码

MouseActions MA = new MouseActions();
canvas.addMouseListener(MA);
canvas.addMouseMotionListener(MA);

再次非常感谢〜

中号

PS ....对于所有怀疑我的疯狂继承技能的人

import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;


public class CustomJPanel extends JPanel
{
    class MouseActions extends MouseInputAdapter
    {

        @Override
        public void mousePressed(MouseEvent e) 
        {
            super.mousePressed(e);
            System.out.println("Pressed");
        }

        @Override
        public void mouseDragged(MouseEvent e) 
        {
            super.mouseDragged(e);
            System.out.println("Dragged");
        }

        @Override
        public void mouseReleased(MouseEvent e) 
        {
            super.mouseReleased(e);
            System.out.println("Released");
        }
    }

    /**
     * @param args the command line arguments
     */

    private CustomJPanel()
    {
        MouseActions ma = new MouseActions();
        addMouseListener(ma);
        addMouseMotionListener(ma);
    }

    public static void main(String[] args) 
    {
        // TODO code application logic here
        JFrame frame = new JFrame();
        CustomJPanel cP = new CustomJPanel();

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cP.setSize(500, 500);
        frame.add(cP);
        frame.setVisible(true);
    }
}

当我将此分配给画布时,它永远不会触发释放或拖动

中号

4 个答案:

答案 0 :(得分:1)

循环while(bDragged)看起来像一个无限循环

答案 1 :(得分:0)

到目前为止,我可以看到你需要实现鼠标监听器。参考:http://profs.etsmtl.ca/mmcguffin/learn/java/04-mouseInput/

答案 2 :(得分:0)

可怕 - 我按照编码的方式工作....除了后来在代码中我删除了画布并重新创建它 - 但忘记重新分配MIA。

感谢您的帮助,

中号

答案 3 :(得分:-2)

我会这样做:

class MouseActions implements MouseMotionListener, MouseListener
{
..
..
}

canvas.addMouseListener(new MouseActions ());
canvas.addMouseMotionListener(new MouseActions() );

这可能会奏效。