有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是否在swing中显示所有鼠标悬停事件?

我正在尝试调试Java应用程序。有几个swing事件,我正试图缩小它们的范围,因为我还不知道bug是从哪里来的。有没有一种方法可以将所有swing事件以及导致它们的句柄输出到控制台


共 (1) 个答案

  1. # 1 楼答案

    Is there a way to output all swing events with the handles that caused them to the console?

    为了帮助调试找到事件的来源,可以使用AWTEventListeners来监听所有被调度的事件

    只需使用AWTEventListener即可。添加更多AWTEvent任何你想听的内容

    long eventMask = AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK
            | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK;
    
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            String paramString = e.paramString();
            System.out.println(paramString);
        }
    }, eventMask);
    

    样本输出

    MOUSE_MOVED,(131,144),absolute(131,144),clickCount=0
    MOUSE_PRESSED,(131,144),absolute(131,144),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1
    MOUSE_RELEASED,(131,144),absolute(131,144),button=1,modifiers=Button1,clickCount=1
    MOUSE_CLICKED,(131,144),absolute(131,144),button=1,modifiers=Button1,clickCount=1
    MOUSE_MOVED,(134,143),absolute(134,143),clickCount=0
    MOUSE_MOVED,(135,142),absolute(135,142),clickCount=0
    MOUSE_MOVED,(172,118),absolute(172,118),clickCount=0
    KEY_PRESSED,keyCode=70,keyText=F,keyChar='f',keyLocation=KEY_LOCATION_STANDARD,rawCode=70,primaryLevelUnicode=102,scancode=33,extendedKeyCode=0x46
    key pressed:70
    KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='f',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0
    KEY_PRESSED,keyCode=68,keyText=D,keyChar='d',keyLocation=KEY_LOCATION_STANDARD,rawCode=68,primaryLevelUnicode=100,scancode=32,extendedKeyCode=0x44
    key pressed:68
    KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='d',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0
    KEY_PRESSED,keyCode=83,keyText=S,keyChar='s',keyLocation=KEY_LOCATION_STANDARD,rawCode=83,primaryLevelUnicode=115,scancode=31,extendedKeyCode=0x53
    key pressed:83
    KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='s',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0,extendedKeyCode=0x0
    KEY_RELEASED,keyCode=70,keyText=F,keyChar='f',keyLocation=KEY_LOCATION_STANDARD,rawCode=70,primaryLevelUnicode=102,scancode=33,extendedKeyCode=0x46
    KEY_PRESSED,keyCode=70,keyText=F,keyChar='f',keyLocation=KEY_LOCATION_STANDARD,rawCode=70,primaryLevelUnicode=102,scancode=33,extendedKeyCode=0x46
    key pressed:70