Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 

The skeleton code currently displays a Button in an applet. This applet should also count the number of times the Button is pressed and display this value in the applet's area of the browser window.

Prerequisites

Skeleton Code

Tasks


  1. Add the instance variable count to the applet and define the method increaseCount() so that it simply increments count each time it's invoked.

  2. Add the method getCount() so that it returns count each time it's invoked.

  3. The core event handling classes are in the java.awt.event package. Import the entire contents of this package.

  4. To handle the command button action event, add the applet itself as an ActionListener target for the button. That is, the applet should implement this interface.

  5. Define the actionPerformed() method so that it calls increaseCount() to update the count instance variable and repaint() to display the current count in the applet window. (You do not have to define repaint().)

  6. Define the paint() method so that it uses drawString(), from the Graphics object that's passed as an argument, to display the current count. Your paint() method will be called by repaint() in actionPerformed().

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 Applet Viewer

Next Exercise

Exercises

Short Course

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


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.