The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
< Day Day Up > |
Here's a snapshot of an application that uses a GridLayout . [19] [19] GridLayout API documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GridLayout.html. Figure 23. A simple GUI that uses GridLayout .
GridLayout places components in a grid of cells . Each component takes all of the available space within its cell, and each cell is exactly the same size . If you resize the GridLayoutDemo window, you'll see that GridLayout changes the cell size so that the cells are as large as possible, given the space in the container. Here's the code that creates the GridLayout object and the components it manages . You can find the whole program in GridLayoutDemo.java . pane.setLayout(new GridLayout(0,2)); pane.add(new JButton("Button 1")); pane.add(new JButton("Button 2")); pane.add(new JButton("Button 3")); pane.add(new JButton("Long-Named Button 4")); pane.add(new JButton("5")); The constructor tells the GridLayout class to create an instance that has two columns and as many rows as necessary. The GridLayout API
Table 10 lists the constructors available in the GridLayout API. You can find the relevant API documentation online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GridLayout.html. Table 10. GridLayout Constructors
Examples That Use GridLayout
The following table lists some GridLayout examples.
|
< Day Day Up > |