【问题标题】:How to listen to mouse movements and left button at the same time如何同时听鼠标移动和左键
【发布时间】:2014-04-09 17:40:08
【问题描述】:

我想要的是:当鼠标移动到单元格(JPanels)上并单击左键(在移动鼠标时按住),单元格应该改变状态。正是您在画布上使用鼠标绘图时所期望的。我就是这样做的:

this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent arg0) {
                if(SwingUtilities.isLeftMouseButton(arg0)) {
                    setItem(MapItems._W_);
                } else {
                    setItem(MapItems.___);
                }
                DrawableCell.this.repaint();
            }
        });

这不起作用(没有任何反应)。使用 mouseMoved() 并没有什么不同。

唯一能做任何事情的是:

public void mouseMoved(MouseEvent arg0) {
    if(arg0.isControlDown()) {
        setItem(MapItems._W_);
    } else {
        setItem(MapItems.___);
    }
    DrawableCell.this.repaint();
}
});

问题在于,由于 mouseMoved 多次触发,因此单元格的状态正在迅速变化,结果是随机的。

怎么做?

【问题讨论】:

    标签: java awt mouseevent mouse-listeners


    【解决方案1】:

    您必须改用mouseListener。请参阅 oracle 文档以获取帮助。

    【讨论】:

    • @jackthehipster 欢迎您 :)。
    • 也许将您的答案作为代码发布?一开始我很困惑,因为我以为你的意思是(new MouseListener()),但是 this.addMouseListener 是需要的……也许其他人偶然发现了这个
    【解决方案2】:

    好吧.. 可能在这里你可以找到一些解脱。 举个例子。。 或者你可以去source解决方案寻求更多帮助

    public static void main ( String[] args )
    {
        JFrame paint = new JFrame ();
    
        paint.add ( new JComponent ()
        {
            private List<Shape> shapes = new ArrayList<Shape> ();
            private Shape currentShape = null;
    
            {
            MouseAdapter mouseAdapter = new MouseAdapter ()
            {
                public void mousePressed ( MouseEvent e )
                {
                currentShape = new Line2D.Double ( e.getPoint (), e.getPoint () );
                shapes.add ( currentShape );
                repaint ();
                }
    
                public void mouseDragged ( MouseEvent e )
                {
                Line2D shape = ( Line2D ) currentShape;
                shape.setLine ( shape.getP1 (), e.getPoint () );
                repaint ();
                }
    
                public void mouseReleased ( MouseEvent e )
                {
                currentShape = null;
                repaint ();
                }
            };
            addMouseListener ( mouseAdapter );
            addMouseMotionListener ( mouseAdapter );
            }
    
            protected void paintComponent ( Graphics g )
            {
            Graphics2D g2d = ( Graphics2D ) g;
            g2d.setPaint ( Color.BLACK );
            for ( Shape shape : shapes )
            {
                g2d.draw ( shape );
            }
            }
        } );
    
        paint.setSize ( 500, 500 );
        paint.setLocationRelativeTo ( null );
        paint.setVisible ( true );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2020-05-17
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      相关资源
      最近更新 更多