繁体   English   中英

哪个鼠标键是中间的?

[英]Which mouse button is the middle one?

我目前正在开发一个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上读到了我能找到的任何东西,但是我找不到一些关于它的东西。

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

  1. isLeftMouseButton
  2. isMiddleMouseButton
  3. isRightMouseButton

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM