Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Using Layout Managers 189

31: private voidsetLookAndFeel() {
32: try {
33: UIManager.setLookAndFeel(
34: “com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
35: );
36: } catch(Exception exc) {
37: // ignore error
38: }
39: }
40:
41: public static voidmain(String[] arguments) {
42: Crisis cr = new Crisis();
43: }
44: }


Figure 14.1 shows the application running.


LISTING 14.1 Continued


FIGURE 14.1
Arranging components using flow
layout.

The FlowLayoutclass uses the dimensions of its container as the only
guideline for how to lay out components. Resize the window of the appli-
cation to see how components are instantly rearranged. Make the window
twice as wide, and you see all of the JButtoncomponents are now shown
on the same line.


TheGridLayoutManager


The GridLayoutclass in the java.awtpackage organizes all components in
a container into a specific number of rows and columns. All components
are allocated the same amount of size in the display area, so if you specify
a grid that is three columns wide and three rows tall, the container is
divided into nine areas of equal size.


GridLayoutplaces all components as they are added into a place on a grid.
Components are added from left to right until a row is full, and then the
leftmost column of the next grid is filled.


The following statements create a container and set it to use a grid layout
that is two rows wide and three columns tall:

Free download pdf