Java Reference
In-Depth Information
You can cycle through the components repeatedly using the next() or previous() methods because the
next component after the last is the first, and the component before the first is the last.
Using a Grid Layout Manager
A grid layout manager arranges components in a rectangular grid within the container. You have three con-
structors for creating GridLayout objects:
GridLayout() : Creates a grid layout manager that arranges components in a single row (that is, a
single column per component) with no gaps between components.
GridLayout(int rows, int cols) : Creates a grid layout manager that arranges components in
a grid with rows rows and cols columns, and with no gaps between components.
GridLayout( int rows,int cols,int hgap,int vgap) : C reates a grid layout manager that
arranges components in a grid with rows rows and cols columns, and with horizontal and vertical
gaps between components of hgap and vgap pixels, respectively.
In the second and third constructors, you can specify either the number of rows or the number of columns as
zero (but not both). If you specify the number of rows as zero, the layout manager provides as many rows
in the grid as are necessary to accommodate the number of components you add to the container. Similarly,
setting the number of columns to zero indicates an arbitrary number of columns. If you fix both the rows
and the columns, and add more components to the container than the grid accommodates, the number of
columns is increased.
You can try out a grid layout manager in a variation of a previous example.
TRY IT OUT: Gridlocking Buttons
Here's the code to demonstrate a GridLayout layout manager in action:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.EtchedBorder;
public class TryGridLayout {
public static void createWindow(){
JFrame aWindow = new JFrame("This is the Window Title");
Toolkit theKit = aWindow.getToolkit();
// Get the
window toolkit
Dimension wndSize = theKit.getScreenSize();
// Get screen
size
// Set the position to screen center & size to half screen size
aWindow.setSize(wndSize.width/2, wndSize.height/2);
// Set
Search WWH ::




Custom Search