java - MouseListener和KeyListener同时使用

标签 java swing awt keylistener mouselistener

如何同时使用 MouseListener 和 KeyListener?

例如,如何做这样的事情

public void keyPressed( KeyEvent e){
// If the mouse button is clicked while any key is held down, print the key
}

最佳答案

使用 boolean 值来确定鼠标按钮是否被按下,然后在 MouseListener 中更新该变量。

boolean buttonDown = false;

public class ExampleListener implements MouseListener {
    public void mousePressed(MouseEvent e) {
        buttonDown = true;
    }

    public void mouseReleased(MouseEvent e) {
        buttonDown = false;
    }

    //Other implemented methods unimportant to this post... 
}

然后,在您的 KeyListener 类中,只需测试 buttonDown 变量。

关于java - MouseListener和KeyListener同时使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057332/

相关文章:

java - 用于 Swing 的多列组合框

java - 在 SwingUtilities.invokeLater 中放置(和不放置)什么?

java - 为什么这个椭圆移动得这么快?

java - jmf 应用程序出现 NoPlayerException 运行时错误

java - If 语句在 criteriaBuilder 中

java - 当在方法级别内部使用 @PreAuthorize 或 @PostAuthorize 时,Class.getAnnotations() 不会返回类级别注释的完整列表

java - 下载的文件大小 0

java - 有没有办法过滤/搜索 HTMLEditorKit 中的内容?

java - JDialog 在由程序和用户操作创建时的不同行为

java - 尽管没有错误,NetBeans Java 项目仍不会显示 GUI 窗口