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 JButton
s 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 JButton
s 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.
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.