ActionEvent和MouseeEvent右键单击JAVA Mac

ActionEvent和MouseeEvent右键单击JAVA Mac,java,mouseevent,actionevent,Java,Mouseevent,Actionevent,我不确定这是Mac问题还是我的代码问题。我正在创建一个按钮网格。对于每个按钮,我使用ActionEvent进行常规单击,使用MouseeEvent进行右键单击。当我按住CTRL键并单击鼠标时,会发生什么?事件执行得很好,但操作甚至会触发。在同时使用动作和鼠标事件的情况下,有没有办法解决这个问题?相关代码: 视图构造函数: for (int i = 0; i < rows; i++) { for (int j = 0; j < columns;

我不确定这是Mac问题还是我的代码问题。我正在创建一个按钮网格。对于每个按钮,我使用ActionEvent进行常规单击,使用MouseeEvent进行右键单击。当我按住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]);

            }
        }
for(int i=0;i
动作事件类:

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
公共类changeButtonHandler实现ActionListener
{
/**
*单击按钮后执行的操作
* 
*/
@抑制警告(“未选中”)
已执行的公共无效操作(操作事件e)
{
对于(int i=0;i
鼠标事件类

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
                        }
                    }
                }
            }
        }
公共类HandlerRight实现MouseListener{
/**
*右键单击按钮后执行的操作
* 
*/
公共无效mouseClicked(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e)| | e.isControlDown()){
System.out.println(“正确工作”);
对于(int i=0;i
当我试图用我自己的问题重现你的问题时,我做不到。鼠标侦听器按预期工作,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()

注意,为了进一步帮助,考虑创建你自己类似于上面的我。


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

在这种情况下,您最好创建一个简单的程序,一个没有按钮数组的程序,但它很小但足够完整,我们可以编译、运行和测试。因此,您希望鼠标事件在按住ctrl键的同时触发,而不是按下按钮?这是一种不寻常的行为,至少对我来说是这样。在Mac上按住ctrl键可以起到右键的作用-在Windows中,单击会起作用,所以在一个右键单击动作很重要的游戏中,是的,这就是我希望它工作的方式。(我不使用鼠标)然后我想我的问题可能是在Mac上使用CTRL单击时,我将使用鼠标和Windows进行测试。啊,是的,问题是CTRL单击问题,否则使用鼠标它就可以正常工作。@Mrambo:请参见编辑2。解决方案是修改您的ActionListener,而不是MouseListener。这是我正在寻找的。谢谢。也谢谢感谢您在以后的参考中让我知道创建一个最小示例程序。而不是否决投票。@Mrambo:不客气。当一个像样的程序被创建和发布后,它会使帮助变得更容易。例如,从今天开始看。这是他在这里的第一个问题,他发布了一个非常好的最小示例。恭喜他!
           @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");
              }
           }
        });