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 7:
Using BoxLayout

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

In this magercise, you will explore the BoxLayout and Box classes. Here, you'll create two boxes:

  • A vertical box with two buttons at opposite extremes
  • A horizontal box with 3 buttons evenly spaced across the area, except that 5 pixels must be unused on the right edge.

The skeleton code preloads the Icon variables rightB, leftB, and animB with the icon files.

Magercise 7 Prerequisites

Buttons With Icons

Skeleton Code

BLayout.java

Resources

Tasks

  1. Create a vertical box (a JPanel with a LayoutManager of BoxLayout) using the Box class.

  2. Create and add two JButtons with icons rightB and leftB. Put "glue" between the buttons to push the buttons to opposite extremes.

  3. Create a horizontal box using the Box class.

  4. Create and add three JButtons with icons rightB, leftB, and animB. Put "glue" between the three buttons. Also, reserve five pixels of space on the right border with a horizontal strut.

  5. Add the two boxes to the frame. The vertical box goes in the west quadrant, while the horizontal one goes in the center.

  6. Save everything and compile the program. Then run it to see the results. When you resize the screen, you'll notice how the button positions aren't fixed, but more of a relative position.

    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]

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

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.