Java Reference
In-Depth Information
Figure 12.8
Output of the GridLayoutDemo program.
buttons[i] = new JButton(“Click “ + (i + 1));
contentPane.add(buttons[i]);
}
}
public static void main(String [] args)
{
JFrame f = new GridLayoutDemo(“GridLayoutDemo”);
f.setSize(300,200);
f.setVisible(true);
}
}
In the GridLayoutDemo program, notice that the components are added to
the JFrame's content pane, not to the JFrame directly. If you invoke add()
on the JFrame instead of its content pane, your code will still compile.
However, you will get an exception at run time, stating that components
cannot be added to a JFrame directly.
BoxLayout Manager
The javax.swing.BoxLayout class represents a box layout, which is used for
displaying components either vertically or horizontally. The BoxLayout man-
ager has the following properties:
Components are displayed either vertically or horizontally, depending
on the value of its axis, which is set in the constructor of BoxLayout.
■■
The components do not wrap as they do with FlowLayout.
■■
If the components are aligned horizontally, the BoxLayout manager
attempts to give them all the same height, based on the component
with the largest height. Components in this axis get their preferred
width.
■■
Search WWH ::




Custom Search