import java.awt.*;

import javax.swing.*;


/**
 * @author Remi Forax
 *
 */
public class LayoutExamples {

  private static JPanel createPanel(LayoutManager layout) {
    JPanel panel=new JPanel(layout);
    return initPanel(panel);
  }
  
  private static JPanel createPanel(LayoutManager2 layout,Object[] constraints) {
    JPanel panel=createPanel((LayoutManager)layout);
    for(int i=0;i<panel.getComponentCount();i++) {
      Component c=panel.getComponent(i);
      layout.addLayoutComponent(c, constraints[i]);
    }
    return panel;
  }
  
  private static JPanel initPanel(JPanel panel) {
    for(int i=0;i<5;i++)
      panel.add(new JButton("button "+i));
    return panel;
  }
  
  private static JPanel createBoxPanel(int axis) {
    JPanel panel=new JPanel(null);
    BoxLayout layout=new BoxLayout(panel,axis);
    panel.setLayout(layout);
    return initPanel(panel);
  }

  public static void main(String[] args) {
    
    String[] borderConstraints=new String[] {
      BorderLayout.NORTH,
      BorderLayout.SOUTH,
      BorderLayout.WEST,
      BorderLayout.EAST,
      BorderLayout.CENTER
    };
    
    JPanel[] panels=new JPanel[]{
      createPanel(new FlowLayout()),
      createPanel(new GridLayout(2,3)),
      createPanel(new BorderLayout(2,3),borderConstraints),
      
      createBoxPanel(BoxLayout.X_AXIS),
      createBoxPanel(BoxLayout.Y_AXIS)
    };
    
    JDesktopPane pane=new JDesktopPane();
    for(int i=0;i<panels.length;i++) {
      JPanel panel=panels[i];
      JInternalFrame internalFrame=new JInternalFrame(
        panel.getLayout().getClass().getName(),true,true,true,true);
      internalFrame.setVisible(true);
      internalFrame.setSize(300,200);
      internalFrame.setLocation(10*i,10*i);
      
      internalFrame.setContentPane(panel);
      pane.add(internalFrame);
    }
    
    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(pane);
    frame.setSize(400,300);
    frame.show();
  }
}
