ActionEvent和MouseEvent右键单击JAVA Mac

时间:2014-03-07 22:35:44

标签: java mouseevent actionevent

我不确定这是Mac问题,还是我的代码问题。我正在创建一个按钮网格。对于每个按钮,我使用ActionEvent进行常规单击,使用MouseEvent进行右键单击。当我按CTRL-单击鼠标事件执行正常时会发生什么,但是操作甚至也会触发。有没有办法解决这个问题,同时同时使用动作和鼠标事件?相关代码:

查看构造函数:

for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                button[i][j] = new Cell();
                button[i][j].addActionListener( new changeButtonHandler() );
                button[i][j].addMouseListener( new handleRight() );
                playArea.add(button[i][j]);

            }
        }

动作事件类:

public class changeButtonHandler implements ActionListener
    {
        /**
         * Action performed after button is clicked
         * 
         */
        @SuppressWarnings("unchecked")
        public void actionPerformed(ActionEvent e)
        {

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (button[i][j] == e.getSource())
                    {   
                    //do stuff
                        }
                        else if(button[i][j].mine==false){
                            //do other stuff
                        }   
                    }

                }
            }   
        }   
    }//end changeButtonHandler class

鼠标事件类

public class handleRight implements MouseListener {

           /**
             * Action performed after button is right-clicked
             * 
             */
        public void mouseClicked(MouseEvent e)
        {
            if (SwingUtilities.isRightMouseButton(e) || e.isControlDown())      {
                System.out.println("Right Worked");
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        if (button[i][j] == e.getSource())
                        {   
                                  //do stuff
                        }
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

当我尝试使用自己的minimal example program重现您的问题时,我无法做到。 MouseListener在预期时工作,ActionListener在预期时工作:

import java.awt.event.*;
import javax.swing.*;

public class TestButtonRightClick {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JButton button = new JButton("Test Me!");
            button.addActionListener(new ActionListener() {

               @Override
               public void actionPerformed(ActionEvent e) {
                  System.out.println("ActionListener invoked");
               }
            });
            button.addMouseListener(new MouseAdapter() {
               @Override
               public void mousePressed(MouseEvent e) {
                  if (e.getButton() == MouseEvent.BUTTON3) {
                     System.out.println("Right Button Pressed");
                  }
               }
            });

            JPanel panel = new JPanel();
            panel.add(button);
            JOptionPane.showMessageDialog(null, panel);
         }
      });
   }
}

编辑:为什么使用SwingUtilities而不是e.getMouseButton()

// ? SwingUtilities
if (SwingUtilities.isRightMouseButton(e) || e.isControlDown())      {

注意,要获得进一步的帮助,请考虑创建自己的minimal example program,与上面的相似。


修改2

要在按下按钮时检查ctrl键的状态,请检查ActionListener中ActionEvent的修饰符:

           @Override
           public void actionPerformed(ActionEvent e) {
              if ((e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
                 System.out.println("control pressed");
              } else {
                 System.out.println("ActionListener invoked");
              }
           }
        });