Welche Maustaste ist die mittlere?

Ich bin derzeit an der Entwicklung eines Programms in Java, wo ein bestimmtes Ereignis nur ausgelöst, wenn der Benutzer klickt mit der linken und der rechten klicken Sie auf eine Schaltfläche.

Da ist es ein wenig unkonventionell, entschied ich mich, um zuerst zu testen. Hier ist es:

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 (" ");
                }
            }
        });
    }
}

Habe es getestet und es funktioniert, aber es gibt ein problem.

Wie Sie sehen können, mit der linken Maustaste wird vertreten durch MouseEvent.BUTTON1 und die Rechte Maustaste durch MouseEvent.BUTTON3.

Wenn der Benutzer eine Maus, die nicht über ein scroll-Rad (scheinbar solche Mäuse noch gibt), dann werden nur zwei Tasten sind in MouseEvent. Bedeutet das, dass die rechts-Taste, vertreten durch den MouseEvent.BUTTON2 statt MouseEvent.BUTTON3? Wenn ja, wie kann ich das ändern mein code, um Platz für diese? Gibt es eine Möglichkeit, ich kann erkennen, etwas wie das?

Lese ich alles, was ich finden konnte, auf der MouseListener-Schnittstelle und auf MouseEvent, aber ich konnte nicht finden, etwas über diese.

InformationsquelleAutor der Frage Radu Murzea | 2012-01-23

Schreibe einen Kommentar