Java Reference
In-Depth Information
// Add top and bottom panel to content pane
Container content = aWindow.getContentPane();// Set container
layout mgr
BoxLayout box = new BoxLayout(content, BoxLayout.Y_AXIS);
// Vertical for
content pane
content.setLayout(box);
// Set box layout
manager
content.add(top);
content.add(bottomPanel);
aWindow.pack();
aWindow.setVisible(true); // Display the
window
}
// main() method definition as before
}
TryBoxLayout4.java
The example now displays the window shown in
Figure 17-26
.
How It Works
Both vertical boxes are now contained within a
JPanel
container. Because
JPanel
objects are Swing
components, you can add a border to them, and this time you add a
TitledBorder
border that you create
directly using the constructor. A
TitledBorder
is a combination of the border that you specify by the
first argument to the
TitledBorder
constructor and a title that is a
String
object you specify as the
second argument to the constructor. You use a border of type
EtchedBorder
here, but you can use any
type of border.
You introduce space between the two vertically aligned
Box
containers by adding a horizontal strut to the
Box
container that contains them. If you wanted space at each side of the window, you could add struts
to the container before and after the components.