The
class that is interested in monitoring AWT events implements this
interface, and the object created with that class is registered with
the Toolkit, using the Toolkit's addAWTEventListener method. When an
event is dispatched anywhere in the AWT, that object's
eventDispatched method is invoked.
import java.awt.*; import java.awt.event.*; class AWTEventListenerInterfEx implements AWTEventListener{ int count = 0; public void eventDispatched(AWTEvent e){ System.out.println("Count is " + count++); } }
import java.awt.event.*; import javax.swing.*; import java.awt.*; class AWTEventListenerEx{ public static void main(String args[]){ JFrame myFrame = new JFrame("Event Handling Example"); /* Create a button */ JButton myButton = new JButton("Click Me"); myFrame.setVisible(true); myFrame.setSize(250, 250); /* Add Button to the frame */ myFrame.add(myButton); AWTEventListenerInterfEx obj1 = new AWTEventListenerInterfEx(); /* Registers the Listeners */ Toolkit toolkit = Toolkit.getDefaultToolkit(); toolkit.addAWTEventListener(obj1, 100); }
No comments:
Post a Comment