Java Reference
In-Depth Information
Layout manager
Description
Allows GUI components to be arranged left-to-right or top-to-bottom in a
container. Class Box declares a container that uses BoxLayout and provides
static methods to create a Box with a horizontal or vertical BoxLayout .
BoxLayout
Similar to GridLayout , but the components can vary in size and can be
added in any order.
GridBagLayout
Fig. 22.15 | Additional layout managers.
The BoxLayout layout manager (in package javax.swing ) arranges GUI components
horizontally along a container's x -axis or vertically along its y -axis. The application in
Figs. 22.16-22.17 demonstrate BoxLayout and the container class Box that uses Box-
Layout as its default layout manager.
1
// Fig. 22.16: BoxLayoutFrame.java
2
// Demonstrating BoxLayout.
3
import java.awt.Dimension;
4
import javax.swing.JFrame;
5
import javax.swing.Box;
6
import javax.swing.JButton;
7
8
import javax.swing.BoxLayout;
import javax.swing.JPanel;
9
import javax.swing.JTabbedPane;
10
11
public class BoxLayoutFrame extends JFrame
12
{
13
// set up GUI
14
public BoxLayoutFrame()
15
{
16
super ( "Demonstrating BoxLayout" );
17
18
// create Box containers with BoxLayout
19
Box horizontal1 = Box.createHorizontalBox();
Box vertical1 = Box.createVerticalBox();
Box horizontal2 = Box.createHorizontalBox();
Box vertical2 = Box.createVerticalBox();
20
21
22
23
24
final int SIZE = 3 ; // number of buttons on each Box
25
26
// add buttons to Box horizontal1
27
for ( int count = 0 ; count < SIZE ; count++)
28
horizontal1.add( new JButton( "Button " + count));
29
30
// create strut and add buttons to Box vertical1
31
for ( int count = 0 ; count < SIZE ; count++)
32
{
33
vertical1.add(Box.createVerticalStrut( 25 ));
vertical1.add( new JButton( "Button " + count));
34
35
}
Fig. 22.16 | BoxLayout layout manager. (Part 1 of 2.)
 
Search WWH ::




Custom Search