2012-01-23 10 views
22

私は現在、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には2つのボタンしか設定されません。これは、MouseEvent.BUTTON3ではなく、右ボタンがMouseEvent.BUTTON2で表されることを意味しますか?はいの場合、これに対応するためにコードをどのように変更できますか?このような何かを検出できる方法はありますか?

私はMouseListenerインターフェイスとMouseEventで何かを見つけることができましたが、これについては何も見つかりませんでした。

+0

@PetarMinchevこれは私が唯一のユーザーだった場合には問題にはならないかもしれませんが、私は自分のプログラムをオンラインで公開するので、おそらく多くの人がそれを使用するでしょう(少なくとも試してみてください)。 –

+0

正解、私はちょうど冗談を言っていました:) –

+0

スクロールホイールのない3つのボタンマウスがあります。 – Ingo

答えて

28

が押されているマウスボタンのどれかを決定するために、SwingUtilitiesからこれらの3つの方法があなたを助けることができる:

  1. isLeftMouseButton
  2. isMiddleMouseButton
  3. isRightMouseButton
+0

+1、素敵な方法:) –

+0

@mKorbel hmmm素敵な、これらのメソッドが存在するか分からなかった。だから、これで私はマウスにホイールがあるかどうかを確認することができます。しかし、私の他の質問はどうですか? (ホイールがない場合、 'MouseEvent.BUTTON3'の代わりに' MouseEvent.BUTTON2'をチェックする必要がありますか?) –

+0

私はPS2/Server Consoleマウスに​​2つのボタンがあると思います。 – mKorbel

13

あなたはユーティリティメソッドを使用することができますSwingUtilties

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

MouseEvent.isPopupTrigger()もあります。マウスの右ボタンを押すと、このメソッドはtrueを返します。

関連する問題