The Java Program: GridLayoutDemo.java

  1 // GridLayoutDemo.java -- Illustrate "grid" layout; adapted from Naughton
  2 
  3 /*
  4  * <applet code="GridLayoutDemo.class" width=200 height=200></applet>
  5  */
  6 
  7 import java.applet.Applet;
  8 import java.awt.*;
  9 
 10 public class GridLayoutDemo extends Applet {
 11 
 12    static final int n = 4;
 13 
 14    public void init() {
 15       setLayout (new GridLayout(n, n));
 16       setFont (new Font("Helvetica", Font.BOLD, 24));
 17 
 18       Dimension d = this.size();
 19       int width  = d.width;
 20       int height = d.height;
 21 
 22       for (int i=0; i<n; i++) {
 23          for (int j=0; j<n; j++) {
 24              int k = i*n + j;
 25              if (k > 0)  add (new Button(String.valueOf(k)));
 26          }
 27       }
 28    }
 29 }