ActionEvent 和 MouseEvent 右键单击 JAVA Mac
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22262017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionEvent and MouseEvent right-click JAVA Mac
提问by rambossa
I'm not sure if this a Mac issue, or an issue with my code. I am creating a grid of buttons. For each button I use ActionEvent for a regular click, and MouseEvent for a right click. What happens is when I CTRL-click the mouse event performs fine, however the action even also fires. Is there a way I can get around this while also using both action and mouse events? Relevant code:
我不确定这是 Mac 问题,还是我的代码问题。我正在创建一个按钮网格。对于每个按钮,我使用 ActionEvent 进行常规单击,使用 MouseEvent 进行右键单击。当我按住 CTRL 键单击鼠标事件时,会发生什么情况,但该操作甚至也会触发。有没有办法在同时使用动作和鼠标事件的同时解决这个问题?相关代码:
View Constructor:
视图构造函数:
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]);
}
}
Action Event Class:
动作事件类:
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
Mouse Event 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
}
}
}
}
}
采纳答案by Hovercraft Full Of Eels
When I tried to reproduce your problem with my own minimal example program, I couldn't. The MouseListener worked when expected and the ActionListener worked when expected:
当我试图用我自己的最小示例程序重现您的问题时,我不能。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);
}
});
}
}
Edit: Why use SwingUtilities instead of e.getMouseButton()
?
编辑:为什么使用 SwingUtilities 而不是e.getMouseButton()
?
// ? SwingUtilities
if (SwingUtilities.isRightMouseButton(e) || e.isControlDown()) {
Note, for further help, consider creating your own minimal example programsimilar to mine above.
请注意,为了获得进一步的帮助,请考虑创建自己的与上面类似的最小示例程序。
Edit 2
编辑 2
To check the state of the ctrl key on button press, check the modifiers of the ActionEvent in your ActionListener:
要检查按下按钮时 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");
}
}
});