
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;


/** DemoBoxLayout - demonstrate the <code>BoxLayout</code> class.<p>
*
* Usage:<p>
*
* <pre>
*     java DemoBoxLayout arg1 arg2
*
*     where 'arg1' is one of
*         c = centre alignment
*         l = left alignment
*         r = right alignment
*     and 'arg2' is one of
*         e = enable struts and springs demo
*         d = disable struts and springs demo
* </pre>
*
* @see <a href="DemoBoxLayout.java">source code</a>
* @author Scott MacKenzie, 2002
* @author William Soukoreff, 2012
*/
public class DemoBoxLayout extends JFrame
{
   public static void main(String[] args)
   {
      // check for correct invocation (2 command-line args required)

      if (args.length != 2)
      {
         usage();
         return;
      }

      // get 1st argument (alignment)

      float hAlign = Float.NaN;
      if (args[0].equals("c")) hAlign = Component.CENTER_ALIGNMENT;
      if (args[0].equals("l")) hAlign = Component.LEFT_ALIGNMENT;
      if (args[0].equals("r")) hAlign = Component.RIGHT_ALIGNMENT;

      if (Float.isNaN(hAlign))
      {
         usage();
         return;
      }

      // get 2nd argument (enable/disable struts and springs demo)

      boolean ssDemo;

      if (args[1].equals("e"))
         ssDemo = true;
      else if (args[1].equals("d"))
         ssDemo = false;
      else
      {
         usage();
         return;
      }

      // use look and feel for my system (Win32)
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoBoxLayout frame = new DemoBoxLayout(hAlign, ssDemo);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoBoxLayout");
      frame.pack();
      frame.setVisible(true);
   }

   private static void usage()
   {
      System.out.println(
         "usage: java DemoBoxLayout arg1 arg2\n\n" +
         "where 'arg1' is one of\n" +
         "   c = centre alignment\n" +
         "   l = left alignment\n" +
         "   r = right alignment\n\n" +
         "and 'arg2' is one of\n" +
         "   e = enable struts and springs demo\n" +
         "   d = disable struts and springs demo"
      );
      return;
   }



   private JButton b1;
   private JButton b2;
   private JButton b3;
   private JButton b4;
   private JButton b5;

   public DemoBoxLayout(float alignmentArg, boolean ssArg)
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      b1 = new JButton("Carrot");
      b2 = new JButton("Yam");
      b3 = new JButton("Broccoli");
      b4 = new JButton("Brussel Sprouts");
      b5 = new JButton("Zucchini");

      // for in-class demo (equal-size buttons)
      //b1.setMaximumSize(b4.getPreferredSize());
      //b2.setMaximumSize(b4.getPreferredSize());
      //b3.setMaximumSize(b4.getPreferredSize());
      //b5.setMaximumSize(b4.getPreferredSize());

      // set horizontal alignment, as per command-line argument

      b1.setAlignmentX(alignmentArg);
      b2.setAlignmentX(alignmentArg);

      // try this...
//      b1.setAlignmentX(Component.LEFT_ALIGNMENT);
//      b2.setAlignmentX(Component.RIGHT_ALIGNMENT);
//      b2.setAlignmentX(.25f);

      b3.setAlignmentX(alignmentArg);
      b4.setAlignmentX(alignmentArg);
      b5.setAlignmentX(alignmentArg);

      // -----------------
      // layout components
      // -----------------

      // include struts ('rigid areas') and springs ('glue') as per command-line arg

      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
      if (ssArg) panel.add(Box.createVerticalGlue());                   // a 'spring'
      panel.add(b1);
      if (ssArg) panel.add(Box.createVerticalGlue());                   // a 'spring'
      if (ssArg) panel.add(Box.createRigidArea(new Dimension(0, 10)));  // a 'strut'
      panel.add(b2);
      if (ssArg) panel.add(Box.createRigidArea(new Dimension(0, 10)));  // a 'strut'
      panel.add(b3);
      if (ssArg) panel.add(Box.createRigidArea(new Dimension(0, 10)));  // a 'strut'
      panel.add(b4);
      if (ssArg) panel.add(Box.createVerticalGlue());                   // a 'spring'
      if (ssArg) panel.add(Box.createRigidArea(new Dimension(0, 10)));  // a 'strut'
      panel.add(b5);
      if (ssArg) panel.add(Box.createVerticalGlue());                   // a 'spring'

      // try this...
      //panel.setPreferredSize(new Dimension(200, 200));

      // try this...
      //System.out.println("panel : " + panel.getPreferredSize());

      // make panel this JFrame's content pane

      setContentPane(panel);
   }
}




