/** BoxPanel.java -- show components in a single file vertically or horizontally. * When using a "Box" layout, we can insert a fixed or variable amount of * space between components. */ import java.awt.*; import javax.swing.*; public class BoxPanel extends JPanel { public BoxPanel() { // For box layout, we need to specify the panel and either horiz/vert direction. setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setBackground(Color.green); JLabel caption = new JLabel("feed the birds"); JLabel picture = new JLabel(new ImageIcon("lagoon.jpg")); JButton b1 = new JButton("Button 1"); JButton b2 = new JButton("Button 2"); JButton b3 = new JButton("Button 3"); add(caption); add(Box.createRigidArea(new Dimension(0, 30))); // hard-coded space add(picture); add(Box.createVerticalGlue()); // put any extra space here add(b1); add(b2); add(b3); } }