20-CS-694 Advanced Programming Techniques Spring 2012
Layout

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next      Applet Source      All lectures

Grid Layout

3X3 Grid Layout with no spacing on the left, 20 pixel spacing on the right

import javax.swing.*;
import java.awt.*;
import java.applet.*;

class GridlayoutAppletFrame extends JFrame {
   public GridlayoutAppletFrame () {
      // Grid Layout for the whole applet
      setLayout(new GridLayout(1,2,40,40));

      // First Panel for a 3X3 layout with no spacing
      Panel p = new Panel();
      p.setLayout(new GridLayout(3,3));
      p.add(new Button("1"));
      p.add(new Button("2"));
      p.add(new Button("3"));
      p.add(new Button("4"));
      p.add(new Button("5"));
      p.add(new Button("6"));
      p.add(new Button("7"));
      p.add(new Button("8"));
      p.add(new Button("9"));
      add(p);

      // Second Panel for a 3X3 layout with 20 pixel spacing
      Panel q = new Panel();
      q.setLayout(new GridLayout(3, 3, 20,20));
      q.add(new Button("1"));
      q.add(new Button("2"));
      q.add(new Button("3"));
      q.add(new Button("4"));
      q.add(new Button("5"));
      q.add(new Button("6"));
      q.add(new Button("7"));
      q.add(new Button("8"));
      q.add(new Button("9"));
      add(q);
   }
}

public class GridlayoutApplet extends Applet {
   public void init () {
      GridlayoutAppletFrame ff = new GridlayoutAppletFrame();
      ff.setSize(450,250);
      ff.setVisible(true);
   }
}