![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Help is available for each task.
Task 1The delegation-based event handling classes are in the java.awt.event package. Import the entire contents of this package. You can make the event package accessible by importing it as follows: import java.awt.event.*; Task 2The applet will be the event source--this functionality already exists. For a
simple task such as beeping when a mouse button is clicked, it's convenient to
design the applet to function as the event target as well. With this design, one
class ( In order to function as a target for mouse button events, the applet (class)
must implement the To the class definition, add: implements MouseListener Add the methods prescribed by public void mouseClicked(MouseEvent e) { /* empty */ } public void mousePressed(MouseEvent e) { /* empty */ } // others... Hint: You can copy and paste the method signatures from the JDK online
HTML documentation.
With these methods, the applet can now function as an event target. Note, however, that event targets must register with event sources. (In this case, the source and the target are the same, but, nevertheless, the source must have a list of who to propagate events to, even if the "who" is itself.) Therefore, the next task is to register the applet as an event target for mouse events. To register as a target (put itself on the list of targets), the applet can
execute the following in the addMouseListener(this); Here, the addMouseListener(this); The method invocation is " The final task is to add the functionality to issue a beep whenever there is
a mouse click. The source (the current applet) will automatically invoke the
target's (the current applet) The applet automatically inherits the method getToolkit().beep();
Copyright 1996-2000 jGuru.com. All Rights Reserved. |