The GridLayout class has two constructors:
public GridLayout(int rows, int columns) public GridLayout(int rows, int columns, int horizontalGap, int verticalGap)
import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; public class GridLayoutTest { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("GridLayout Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridLayout(3, 2)); frame.add(new JButton("Button 1")); frame.add(new JButton("Button 2")); frame.add(new JButton("Button 3")); frame.add(new JButton("Button 4")); frame.add(new JButton("Button 5")); frame.add(new JButton("Button 6")); frame.add(new JButton("Button 7")); frame.add(new JButton("Button 8")); frame.pack(); frame.setVisible(true); } }
14.90.GridLayout | ||||
14.90.1. | Laying Out Components in a Grid | |||
14.90.2. | How to Use GridLayout | |||
14.90.3. | Sample GridLayout Application | |||
14.90.4. | Creating Rows of Buttons | |||
14.90.5. | Uses a 2-column grid. | |||
14.90.6. | Use a 1x1 grid to make a component as large as possible | |||
14.90.7. | Using a GridLayout Manager |