Java AWT:如何知道在 mouseDragged 事件后释放了哪个鼠标按钮?

Posted

技术标签:

【中文标题】Java AWT:如何知道在 mouseDragged 事件后释放了哪个鼠标按钮?【英文标题】:Java AWT: How to know which mouse button was released after a mouseDragged event? 【发布时间】:2018-03-15 05:33:41 【问题描述】:

如果我按住两个鼠标按钮,然后拖动鼠标,然后释放其中一个按钮,我如何确定哪个按钮被释放?按下的按钮可以通过 MouseEvent.getModifiersEx() 检索,但似乎没有任何方法可以判断哪个按钮被释放,因为在释放事件触发之前按下的所有按钮都存储在该掩码中:

public void mouseReleased(MouseEvent e) 

    int b1 = MouseEvent.BUTTON1_DOWN_MASK;
    int b2 = MouseEvent.BUTTON2_DOWN_MASK;

    System.out.println(e.getButton()) // prints "0" when the mousereleased
                                      // event follows a mouseDragged
                                      // event

    if ((mouseEvent.getModifiersEx() & b1) == b1) 
      System.out.println("button 1 released");
     else if ((mouseEvent.getModifiersEx() & b2) == b2) 
      System.out.println("button 2 released");
    

    /* 
       Following a mouseDragged event, assuming button 1 and button 2 
       were pressed before either was released, the first if clause 
       evaluates to true regardless of which button was actually released.
       In other words, releasing button 2 in this scenario will print
       "button 1 released"
     */


如果 mouseReleased 事件之前有 mousePressed 事件很容易,但是当它之前有 mouseDragged 时,我不能简单地调用 getButton()...

【问题讨论】:

请发布您最好的minimal reproducible example 尝试向我们展示您的代码是如何正常工作的。 你试过SwingUtilities#isLeftMouseButtonisRightMouseButton吗? @HovercraftFullOfEels 感谢您的反馈。我用一个具体的代码块替换了抽象代码块,以更好地说明我的问题。 谢谢,它有效。我不知道为什么我认为 getButton 在之前的拖动事件之后返回 0。 【参考方案1】:

但是当它前面有mouseDragged时,我不能简单地调用getButton()...

为什么不呢?

如果您想要在拖动停止后鼠标释放的状态,也许在您的 MouseAdapter 中使用一个布尔标志,并在拖动时将其设置为 true。比如:

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class MouseButtons extends JPanel 
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;

    public MouseButtons() 
        MyMouseAdapter myMouse = new MyMouseAdapter();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    

    @Override
    public Dimension getPreferredSize() 
        if (isPreferredSizeSet()) 
            return super.getPreferredSize();
        
        return new Dimension(PREF_W, PREF_H);
    

    private class MyMouseAdapter extends MouseAdapter 
        private boolean dragging = false;

        @Override
        public void mousePressed(MouseEvent e) 
        

        @Override
        public void mouseReleased(MouseEvent e) 
            String text = "";
            int button = e.getButton();
            if (button == MouseEvent.BUTTON1) 
                text = "Button 1";
             else if (button == MouseEvent.BUTTON2) 
                text = "Button 2";
             else if (button == MouseEvent.BUTTON3) 
                text = "Button 3";
            

            if (dragging) 
                System.out.println(text + " just finished dragging");
             else 
                System.out.println(text + " not recently dragging");
            
            dragging = false;
        

        @Override
        public void mouseDragged(MouseEvent e) 
            dragging = true;
        
    

    private static void createAndShowGui() 
        JFrame frame = new JFrame("MouseButtons");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MouseButtons());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    

    public static void main(String[] args) 
        SwingUtilities.invokeLater(() -> createAndShowGui());
    

除非我完全误解了你的问题,如果是这样,我请你澄清一下。

【讨论】:

以上是关于Java AWT:如何知道在 mouseDragged 事件后释放了哪个鼠标按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何正确覆盖 awt paint() 方法

如何使用 Java AWT/Swing 垂直对齐面板

如何从具有多个 TTF 文件的字体系列导入自定义 java.awt.Font? (包括一个例子)

与 Java AWT 的矩形交集

如何在java中使鼠标点击时颜色改变?

求教高手如何java绘画出一条趋势线?