Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Counting Key Presses

 

Training Index

by jGuru

Help  API Docs  About This Short Course  Short Course  Exercises

For this exercise you design an applet that receives all key events, counts the number of key-press events, and displays this value in the applet's area of the browser window. The nonkey-related functionality already exists in the skeleton.

Prerequisites

Skeleton Code

Tasks

1. To receive the key events, add the applet itself as an KeyListener target. That is, the applet should implement this interface.

2. Define the keyPressed() method so that it calls increaseCount() to update the count instance variable and repaint() to display the current count in the applet window.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

This exercise only runs within browsers that support the Java Runtime Environment 1.1 and appletviewer. If you are using an appropriate browser, try this exercise. The output should look similar to the following:

Count Keys Applet Image

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.