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
 

Help is available for each task, or you can go straight to the solution source code.

Task 1

Create a vertical box (a JPanel with a LayoutManager of BoxLayout) using the Box class.
  Box vert = Box.createVerticalBox();

Task 2

Create and add two JButtons with icons rightB and leftB. Put "glue" between the buttons to push the buttons to opposite extremes.
  JButton top = new JButton(rightB);
  JButton bottom = new JButton(leftB);
  vert.add(top);
  vert.add(Box.createGlue());
  vert.add(bottom);

Task 3

Create a horizontal box using the Box class.
  Box horiz = Box.createHorizontalBox();

Task 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.
  JButton left = new JButton(rightB);
  JButton middle = new JButton(leftB);
  JButton right = new JButton(animB);
  horiz.add(left);
  horiz.add(Box.createGlue());
  horiz.add(middle);
  horiz.add(Box.createGlue());
  horiz.add(right);
  horiz.add(Box.createHorizontalStrut(XSPACE));

Task 5

Add the two boxes to the frame. The vertical box goes in the west quadrant, while the horizontal one goes in the center.
  Container content = getContentPane();
  content.add (vert, BorderLayout.WEST);
  content.add (horiz, BorderLayout.CENTER);

Task 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.

javac BLayout.java
java BLayout

If you are using Swing with JDKTM 1.1.+, make sure your CLASSPATH environment variable has the swingall.jar file in it.

Return to Magercise 7

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.