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

Using BoxLayout

 
Training Index

[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 Java source file represents a solution to this Magercise.

Solution

Short Course

About This Short Course

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