[英]MouseMotionListener not Firing correctly in Java
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.谁能告诉我为什么 mouseMotionListener 没有触发 mouseDragged 事件(我用谷歌搜索了几个小时,甚至从 .net 复制粘贴了代码。)下面是 class 的代码 - 我创建了 class 的 object 并添加它到一个名为 canvas 的 JPanel。
PS the mousePressed() is the only method that gets fired, mouseDragged() and mouseReleased(), they do not. 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;
}
}
/// 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 PS ....对于所有怀疑我疯狂的 inheritance 技能的人
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同样,当我将其分配给 canvas 时,它永远不会触发释放或拖动
M米
The loop while(bDragged) looks like an infinite loop循环 while(bDragged) 看起来像一个无限循环
As far I can see you need to implement the mouse listener.据我所知,您需要实现鼠标侦听器。 Referring to: http://profs.etsmtl.ca/mmcguffin/learn/java/04-mouseInput/
参考: http://profs.etsmtl.ca/mmcguffin/learn/java/04-mouseInput/
Scary enough - i worked just the way it was coded to.... Except later in the code i removed the canvas and recreated it - but forgetting to reassign the MIA.够吓人的——我按照它被编码的方式工作……除了后来在代码中我删除了 canvas 并重新创建它——但忘记重新分配 MIA。
Thanks for all your help,感谢你的帮助,
M米
I would do this:我会这样做:
class MouseActions implements MouseMotionListener, MouseListener
{
..
..
}
canvas.addMouseListener(new MouseActions ());
canvas.addMouseMotionListener(new MouseActions() );
and that would probably work.这可能会奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.