2012-01-23 55 views
22

Tôi hiện đang phát triển một chương trình trong Java mà một sự kiện nhất định phải được kích hoạt chỉ khi người dùng nhấp chuột với cả hai bên trái và nhấp chuột phải vào một nút.Nút chuột nào là nút giữa?

Vì đó là một chút độc đáo, tôi quyết định đầu tiên kiểm tra điều này. Dưới đây là:

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

Tôi đã thử nghiệm và hoạt động, nhưng có sự cố.

Như bạn có thể thấy, nút chuột trái được thể hiện bằng MouseEvent.BUTTON1 và nút chuột phải bằng MouseEvent.BUTTON3.

Nếu người dùng có con chuột không có bánh xe cuộn (dường như những con chuột này vẫn tồn tại), thì chỉ có hai nút được đặt trong MouseEvent. Điều đó có nghĩa là nút bên phải sẽ được đại diện bởi MouseEvent.BUTTON2 thay vì MouseEvent.BUTTON3? Nếu có, làm cách nào tôi có thể thay đổi mã của mình để chứa mã này? Có cách nào tôi có thể phát hiện một cái gì đó như thế này?

Tôi đọc bất cứ điều gì tôi có thể tìm thấy trên giao diện MouseListener và trên MouseEvent, nhưng tôi không thể tìm thấy điều gì đó về điều này.

+0

@PetarMinchev điều này sẽ không là vấn đề nếu tôi là người dùng duy nhất ... nhưng tôi sẽ xuất bản chương trình của mình trực tuyến để nhiều người có thể sử dụng nó (hoặc ít nhất là thử). –

+0

Đúng, tôi chỉ đùa thôi :) –

+0

Có 3 nút chuột không có bánh xe cuộn. – Ingo

Trả lời

28

Để xác định các nút chuột được nhấn, ba phương pháp này từ SwingUtilities có thể giúp bạn:

  1. isLeftMouseButton
  2. isMiddleMouseButton
  3. isRightMouseButton
+0

+1, phương pháp tốt đẹp :) –

+0

@mKorbel hmmm tốt đẹp, không biết những phương pháp này tồn tại. Vì vậy, với điều này tôi có thể kiểm tra nếu con chuột có một bánh xe. Nhưng còn câu hỏi khác của tôi thì sao? (nếu không có bánh xe, tôi có nên kiểm tra 'MouseEvent.BUTTON2' thay vì' MouseEvent.BUTTON3'?) –

+0

Tôi nghĩ rằng không có con chuột (PS2/Server Console) có hai nút LEFT & RIGHT – mKorbel

13

Bạn có thể sử dụng các phương pháp tiện ích từ SwingUtilties:

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

Ngoài ra còn có MouseEvent.isPopupTrigger(). Phương thức này sẽ trả về true, nếu nhấn chuột phải.

Các vấn đề liên quan