There is an event queue (EventQuery), all events are sent to the event parallelism (mouse event, keyboard events, etc.), so the principle is to monitor this queue, and an event updates a certain value, and the background is also There is a line to check this value, just like the management of the Session in the web container, the specific code is, for example, below:
Public class demo6 {
Public static void main (String [] args) {
JFrame frame = new JFrame ( "Test"); frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); Listener listener = new Listener (); Toolkit.getDefaultToolkit () addAWTEventListener (listener, AWTEvent.MOUSE_EVENT_MASK);. Toolkit.getDefaultToolkit () addAWTEventListener. (Listener, AWTEVENT.FOCUS_EVENT_MASK); Toolkit.getDefaultToolkit (). addawteventlistener (listener, awtevent.key_event_mask);
Frame.setsize (New Dimension (500, 500)); frame.setvisible (TRUE);
Static Class Listener Implements AWTEVENTLISTENER {
Public void Eventdispatched (AWTEVENT EVENT) {
System.out.println ("Handle Event:" Event.getClass ());}}}
The above code is just an example of monitoring all events, and there is no example of time check. It is also very simple, you can use Timer or
Directly use the thread.