MouseListener MouseDragged无法正常工作 [英] MouseListener MouseDragged doesnt work as expected

查看:91
本文介绍了MouseListener MouseDragged无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格,当我左键单击,按住并在其上移动光标(即,拖动鼠标)时,这些框会变成红色(我基本上想绘制网格).我有下面的代码.当我做鼠标拖动时.正确调用了MouseDragged方法,但是在拖动之后,只有一个框变为红色,并且没有任何反应(尽管该方法仍被调用).有任何想法吗 ?希望我很清楚.谢谢

I have a grid where the boxes become red (I basically want to paint the grid) when I left click, hold and move the cursor on them (i.e: drag mouse). I have the code below. When I do the mouse dragging. The MouseDragged method is called properly however only one box becomes red and nothing happens while I drag after that (although the method is still called). Any ideas ? Hope I was clear. Thanks

public static class DragListener implements MouseMotionListener
{



    @Override
    public void mouseDragged(MouseEvent me) {


            JPanel current =(JPanel)me.getSource();

            current.setBackground(Color.RED);

    }
  }

这是网格的定义:

public static class GridPane extends JPanel {

    public GridPane(int row, int col) {
        int count = 0 ;
        setLayout(new GridLayout(row, col));
        setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));

        for (int i = 1; i <= (row * col); i++) {

            JPanel lab = new JPanel();

            lab.setEnabled(true);
            lab.setBackground(Color.WHITE);
            lab.setPreferredSize(new Dimension(3, 3));
            lab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            lab.addMouseMotionListener(new DragListener()); 
            lab.addMouseListener(new ClickListener());
            lab.setName(count+"");
            ++count;

            add(lab);
        }
    }
}

推荐答案

我认为您的问题源于您将鼠标拖到多个JPanels上的事实,并且是因为Java能够识别拖动的原因. Java通过以下算法识别拖动:

I think your problem is stemming from the fact that you are dragging your mouse over multiple JPanels, and because of how java recognizes dragging. Java recognizes dragging by the following algorithm:

在单个组件"c"中:

  1. 在"c"内部按下鼠标
  2. 然后在"c"内部移动鼠标-这构成了在"c"内部的拖动

由于鼠标最终会在按下时离开一个组件并进入第二个组件,因此第二个组件永远不会注册mousePressed动作,因此它不认为您在拖动鼠标.我建议维护一些标志,该标志告诉您的GridPane何时在实验室" JPanels(mousePressed())的任何面板内按下鼠标,然后实现mouseMoved()方法来检查该标志和颜色(如果已设置).然后实现mouseReleased()将您的标志重置为正常状态,以使您在停止拖动后不再继续着色.

Because your mouse is ultimately leaving one component and entering a second component while pressed, the second component never registers the mousePressed action, so it doesn't think you're dragging the mouse. I'd recommend maintaining some flag that tells your GridPane when the mouse is pressed inside ANY of your "lab" JPanels (mousePressed()), then implement the mouseMoved() method to check that flag and color if it has been set. Then implement mouseReleased() to reset your flag back to the normal state so you don't continue coloring after you stop dragging.

有关Java中鼠标移动的更多信息,请查看以下链接: http://docs.oracle.com/javase/tutorial/uiswing/events /mousemotionlistener.html

For more info on mouse motion in Java, check out this link: http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html

这篇关于MouseListener MouseDragged无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆