【问题标题】:ActionEvent and MouseEvent right-click JAVA MacActionEvent 和 MouseEvent 右键单击​​ JAVA Mac
【发布时间】:2014-04-11 07:18:15
【问题描述】:

我不确定这是 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
                        }
                    }
                }
            }
        }

【问题讨论】:

  • 在这种情况下,你最好创建一个简单的程序,一个没有按钮数组的程序,但是一个小而完整的程序,足以让我们编译、运行和测试,@ 987654321@.
  • 所以您希望鼠标事件通过 ctrl-click 触发而不是按下按钮?这是一种不寻常的行为,至少对我来说是这样。
  • 在 Mac 上的 CTRL-Click 就像在 Windows 中的右键单击一样,因此在右键单击操作很重要的游戏中,是的,这就是我希望它的工作方式。 (我不用鼠标)

标签: java mouseevent actionevent


【解决方案1】:

当我尝试用我自己的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");
              }
           }
        });

【讨论】:

  • 然后我想我的问题可能是在 Mac 上使用 CTRL-Click 时,我将使用鼠标和 Windows 进行测试。
  • 啊,是的,问题是CTRL-Click问题,否则用鼠标就可以了。
  • @Mrambo:见 编辑 2。解决方案是修改您的 ActionListener 而不是 MouseListener。
  • 这就是我要找的。谢谢你。也感谢您让我知道在未来参考创建一个最小的示例程序..而不是投票。
  • @Mrambo:不客气。当创建和发布一个体面的人时,它只会让帮助变得更加容易。例如,从今天开始查看this guy's question。这是他在这里的第一个问题,他发布了一个非常好的最小示例。向他致敬!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 2014-02-09
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 2011-05-30
相关资源
最近更新 更多