What is sun.awt.windows.WToolkit?

时光毁灭记忆、已成空白 提交于 2019-12-11 02:34:18

问题


I have the following piece of code

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import javax.swing.*;
class QueueTest {
static int i=0;
    public static void main(String[] args) throws InterruptedException, 

InvocationTargetException {
        EventQueue eventQueue = 

Toolkit.getDefaultToolkit().getSystemEventQueue();
        eventQueue.push(new MyEventQueue());


    Frame f=new Frame();
    f.setSize(400,400);
    //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocation(150,150);
    f.setLayout(new FlowLayout());
    f.setVisible(true);

    Button b=new Button("button");
    //b.setEnabled(false);
    f.add(b);
/*
    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae)
        {
        System.out.println("button is clicked");
        }
    });
*/
    }

    private static class MyEventQueue extends EventQueue {
        public void postEvent(AWTEvent theEvent) {
//            System.out.println("Event Posted");
  System.out.println("The source of event is "+theEvent.getSource());
            super.postEvent(theEvent);
        }

    protected void dispatchEvent(AWTEvent event)
    {
    System.out.println("The source of event ("+(i++)+") is 

"+event.getSource());
    super.dispatchEvent(event);
    }
    }
}

In the output i could sometimes see

The source of event is (78) sun.awt.windows.WToolkit@77ef83

when I think i have only two sources, the java.awt.Button and the java.awt.Frame. Also when I am pressing the mouse, I could see two events being generated for which one is sun.awt.windows.WToolkit is the source and for the other is Button (when I clicked on button).

My questions are

  1. what is sun.awt.windows.WToolkit?
  2. why am I able to see two events on a single mouse press?

回答1:


The names speak for themselves: AWT stands for Abstract Window Toolkit which implies that the Toolkit is abstract and requires an actual implementation. sun.awt.windows.WToolkit is such an implementation for the Microsoft Windows plattform hence the W in its name. On other operating systems you will see different implementations, e.g. sun.awt.X11.XToolkit on Linux. If you just do a System.out.println(Toolkit.getDefaultToolkit()); you will see that the Toolkit’s string representation matches that of the event source which you see from time to time.

I suggest you do a print of the entire event instead of just the source. Then you will see what these events are for. You will see what kind of events the toolkit generates. And you will see that a mouse click can generate up to three events: one for the press, one for the release and one if pressing and releasing happened at the same location which is considered a click.




回答2:


You're implementing ALL of the libraries by using the * character. So the output specifies where the source of event occurred.



来源:https://stackoverflow.com/questions/21523883/what-is-sun-awt-windows-wtoolkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!