GridLayout

Here's the source code:

import java.applet.*;
import java.awt.*;

public class Ingredients2 extends Applet {

  private TextField t;
  private double price = 7.00;

  public void init() {
  
    this.setLayout(new GridLayout(11,1));
    this.add(
     new Label("What do you want on your pizza?", Label.CENTER));
    this.add(new Checkbox("Pepperoni"));
    this.add(new Checkbox("Olives"));
    this.add(new Checkbox("Onions"));
    this.add(new Checkbox("Sausage"));
    this.add(new Checkbox("Peppers"));
    this.add(new Checkbox("Extra Cheese"));
    this.add(new Checkbox("Ham"));
    this.add(new Checkbox("Pineapple"));
    this.add(new Checkbox("Anchovies"));
    this.t = new TextField("$" + String.valueOf(price));
    // so people can't change the price of the pizza
    this.t.setEditable(false); 
    this.add(this.t);
  }
  
  /* I've removed the code to handle events
  since it isn't relevant to this example */

}

Grid layouts are very easy to use. This applet is just three lines different from the previous version, and one of those is a change in the name of the class that wasn't really necessary.


Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998, 2002, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 18, 2006