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