代码之家  ›  专栏  ›  技术社区  ›  Skywarp

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

  •  0
  • Skywarp  · 技术社区  · 7 年前

    如果我按住两个鼠标按钮,然后拖动鼠标,然后释放其中一个按钮,我如何确定释放了哪个按钮?按下的按钮可以用鼠标按钮取回。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"
         */
    
    }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Hovercraft Full Of Eels    7 年前

    为什么不呢?

    如果希望在拖动停止后释放鼠标的状态,可以在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());
        }
    }