Separating Components with Space in a FlowLayout

Most LayoutManagers allow you to control the minimum amount of vertical and horizontal space between different components. To do this in a FlowLayout, you pass the horizontal and vertical space you want, in pixels, to the constructor immediately after the alignment argument.

public FlowLayout(int alignment, int horizontalSpace, int verticalSpace);

For instance to set up a FlowLayout with a ten pixel horizontal gap and a twenty pixel vertical gap, aligned with the left edge of the panel, you would use the constructor

FlowLayout fl = new FlowLayout(FlowLayout.LEFT, 20, 10);

Applet with five buttons spread out

Buttons arranged according to a center-aligned FlowLayout with a 20 pixel horizontal spacing and a 10 pixel vertical spacing

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

public class SpaceTapeDeck extends Applet {

  public void init() {
  
    this.setLayout( new FlowLayout(FlowLayout.LEFT, 20, 10) );

    this.add( new Button("Play"));
    this.add( new Button("Rewind"));
    this.add( new Button("Fast Forward"));
    this.add( new Button("Pause"));
    this.add( new Button("Stop"));
    
  }

}

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 1, 2002