【问题标题】:Which mouse button is the middle one?哪个鼠标按钮是中间的?
【发布时间】:2012-01-23 13:19:44
【问题描述】:

我目前正在用 Java 开发一个程序,其中只有当用户同时单击一个按钮的左键和右键单击时才能触发某个事件。

由于它有点不合常规,我决定先测试一下。这里是:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class GUI
{
    private JFrame mainframe;
    private JButton thebutton;

    private boolean left_is_pressed;
    private boolean right_is_pressed;

    private JLabel notifier;

    public GUI ()
    {
        thebutton = new JButton ("Double Press Me");
        addListen ();
        thebutton.setBounds (20, 20, 150, 40);

        notifier = new JLabel (" ");
        notifier.setBounds (20, 100, 170, 20);

        mainframe = new JFrame ("Double Mouse Tester");
        mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        mainframe.setResizable (false);
        mainframe.setSize (400, 250);

        mainframe.setLayout (null);

        mainframe.add (thebutton);
        mainframe.add (notifier);

        mainframe.setVisible (true);

        left_is_pressed = right_is_pressed = false;
    }

    private void addListen ()
    {
        thebutton.addMouseListener (new MouseListener ()
        {
            @Override public void mouseClicked (MouseEvent e) { }
            @Override public void mouseEntered (MouseEvent e) { }
            @Override public void mouseExited (MouseEvent e) { }

            @Override public void mousePressed (MouseEvent e)
            {
                //If left button pressed
                if (e.getButton () == MouseEvent.BUTTON1)
                {
                    //Set that it is pressed
                    left_is_pressed = true;

                    if (right_is_pressed)
                    {
                        //Write that both are pressed
                        notifier.setText ("Both pressed");
                    }

                }
                //If right button pressed
                else if (e.getButton () == MouseEvent.BUTTON3)
                {
                    //Set that it is pressed
                    right_is_pressed = true;

                    if (left_is_pressed)
                    {
                        //Write that both are pressed
                        notifier.setText ("Both pressed");
                    }
                }
            }

            @Override public void mouseReleased (MouseEvent e)
            {
                //If left button is released
                if (e.getButton () == MouseEvent.BUTTON1)
                {
                    //Set that it is not pressed
                    left_is_pressed = false;

                    //Remove notification
                    notifier.setText (" ");
                }
                //If right button is released
                else if (e.getButton () == MouseEvent.BUTTON3)
                {
                    //Set that it is not pressed
                    right_is_pressed = false;

                    //Remove notification
                    notifier.setText (" ");
                }
            }
        });
    }
}

我测试了它,它可以工作,但是有一个问题。

如您所见,鼠标左键由MouseEvent.BUTTON1 表示,鼠标右键由MouseEvent.BUTTON3 表示。

如果用户有一个没有滚轮的鼠标(显然这样的鼠标仍然存在),那么在 MouseEvent 中只设置了两个按钮。这是否意味着右键将由MouseEvent.BUTTON2 而不是MouseEvent.BUTTON3 表示?如果是,我该如何更改我的代码以适应这一点?有什么办法可以检测到这样的事情吗?

我在 MouseListener 界面和 MouseEvent 上阅读了我能找到的任何内容,但我找不到关于此的任何内容。

【问题讨论】:

  • @PetarMinchev 如果我是唯一的用户,这不会是一个问题......但我会在网上发布我的程序,所以很多人可能会使用它(或至少尝试一下)。跨度>
  • 有3个无滚轮的按钮鼠标。
  • 还有只有两个按钮的滚轮鼠标。

标签: java swing mouse mouseevent mouselistener


【解决方案1】:

要确定按下了哪个鼠标按钮,SwingUtilities 中的这三种方法可以帮助您:

  1. isLeftMouseButton
  2. isMiddleMouseButton
  3. isRightMouseButton

【讨论】:

  • @mKorbel 嗯不错,不知道这些方法存在。所以有了这个我可以检查鼠标是否有滚轮。但是我的另一个问题呢? (如果没有轮子,我应该检查MouseEvent.BUTTON2 而不是MouseEvent.BUTTON3 吗?)
  • 我认为没有(PS2 / 服务器控制台)鼠标有两个按钮 LEFT 和 RIGHT
  • 请注意,如果用户按住 Alt 键单击左键,isMiddleMouseButton 将返回 TRUE。此外,如果用户使用左键按住 Ctrl 键单击,isRightMouseButton 将返回 TRUE。
  • @texclayton:所以你是说如果你需要多个带有修饰符的按钮,这些方法是非常不可用的......有什么解决方法吗?
  • 这些方法的实现只是检查天气与否,按钮掩码 (1
【解决方案2】:

您可以使用SwingUtilties中的实用程序方法:

SwingUtilities.isLeftMouseButton(MouseEvent anEvent)   
SwingUtilities.isRightMouseButton(MouseEvent anEvent) 
SwingUtilities.isMiddleMouseButton(MouseEvent anEvent)

【讨论】:

    【解决方案3】:

    还有MouseEvent.isPopupTrigger()。如果按下鼠标右键,此方法应返回 true。

    【讨论】:

    • 唯一的缺点是该函数的发布事件返回 false,因此在该上下文中无效。
    猜你喜欢
    • 2013-05-21
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多