Chapter 16: Layout Managers

The exercises here are taken from my forthcoming book The Java Developer's Resource.


  1. What's the difference between a BorderLayout and a FlowLayout?

  2. What's the difference between a GridLayout and a GridBagLayout?

    A GridLayout places each component into a cell of one size. Thus all components have the same size. A GridbagLayout allows components to span multiple cells. Therefore components can have different sizes. There are other differences, but that's the main one.


  1. Add a Fast Forward Button to Program 16.1.

    import java.applet.Applet;    
    import java.awt.Button;
    import java.awt.FlowLayout;
    public class TapeDeck extends Applet {
      public void init() {
        setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
    	add( new Button("Play"));
    	add( new Button("Rewind"));
    	add( new Button("Fast Forward"));
    	add( new Button("Pause"));
    	add( new Button("Stop"));
  2. Add a GridLayout to the PaymentMethod applet of last chapter.

    import java.applet.Applet;    
    import java.awt.Label;
    import java.awt.Checkbox;
    import java.awt.CheckboxGroup;
    import java.awt.GridLayout;
    public class PaymentMethod extends Applet {
      public void init() {
        setLayout(new GridLayout(6, 1));
        add(new Label("How will you pay for your pizza?"));
        CheckboxGroup cbg = new CheckboxGroup();
        add(new Checkbox("Visa", cbg, false));
        add(new Checkbox("MasterCard", cbg, false));
        add(new Checkbox("American Express", cbg, false));
        add(new Checkbox("Discover", cbg, true));
        add(new Checkbox("Cash", cbg, true));
  3. Use a CardLayout to provide a complete pizza ordering system. The first card should ask the user about their pizza, how big it is and what toppings they want. The second card should collect payment information, that is how they wish to pay, their name, and their credit card information (name, card number, and expiration date.) The final card should be for the address and delivery instructions.

  4. Make the calculator applet behave as a calculator, i.e. when you hit the keys 3 + 2 = the screen should show 5, and so on.

  5. Hard: Define a class key which includes both the display as a button and the functionality of the Calculator key. This will require some thought for what the total data structure is which represents the current calculation and how the keys relate to that. This will probably be easier if you do exercise one first.

[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996 Elliotte Rusty Harold
Last Modified September 5, 1996