0

can anyone tell me why the mouseMotionListener is not firing the mouseDragged event(I have googled for hours and even copy-and-pasted code from the net!) Below is the code for the class - I create an object of the class and add it to a JPanel called canvas.

PS the mousePressed() is the only method that gets fired, mouseDragged() and mouseReleased(), they do not.

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;
        }

    }

/// Now the code to add the listeners

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

Again Thanks alot~

M

PS.... For all who are doubting my mad inheritance skills

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

Again when I assign this to canvas it never fires the release or the dragged

M

4

4 回答 4

1

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

于 2012-04-10T20:50:19.873 回答
0

据我所知,您需要实现鼠标侦听器。参考: http: //profs.etsmtl.ca/mmcguffin/learn/java/04-mouseInput/

于 2012-04-10T20:43:26.447 回答
0

够吓人的——我按照它的编码方式工作……除了后来在代码中我删除了画布并重新创建它——但忘记重新分配 MIA。

感谢你的帮助,

于 2012-04-11T18:32:11.197 回答
-2

我会这样做:

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

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

那可能会奏效。

于 2012-04-10T20:44:09.130 回答