JDC">
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
 
Training Index

Magercise 3:
Creating Buttons With Icons

[Help | Solution | API Docs | Short Course | Magercises]

In this magercise, you'll learn about adding an Icon, or small image, to a JButton and placing components within a JFrame. You'll create two buttons with images on them and respond to the selection of each. In addition to physically selecting the button with a mouse, you'll support keyboard selection through accelerators.

Magercise 3 Prerequisites

None.

Skeleton Code

FirstSwing.java

Resources

Tasks

  1. Import the swing package.

  2. Define two Icon instance variables in class FirstSwing called bee and dog. Then, load the bee.gif and dog.gif image files.

  3. Create buttons top and bottom with icons bee and dog.

  4. Add an accelerator for each button.

  5. When working with a JFrame, you add components to an internal area of the JFrame, instead of the actual frame. Get a reference to this Container via the getContentPane() method.

  6. To have the buttons appear one on top of each other, modify the skeleton's constructor so that it sets the content's layout to GridLayout with 2 rows and 1 column.

  7. Add the buttons to the grid layout.

  8. Save everything and compile the program. Then run it to see the results. Clicking a button or using the accelerator displays a message to the screen.

    As a precaution, the Save command appends a "1" to the end of the filename you want to save. This can help prevent you from accidently overwriting your source code.

result.gif

Note: The accelerator key is ALT under Win32 and Solaris.

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

The following JavaTM source file represents a solution to this Magercise.

Solution

Magercise 4

Short Course

About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 19-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | 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-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.