All for Joomla All for Webmasters

BoxLayout class

The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants. They are as follows:

Note: BoxLayout class is found in javax.swing package.

Fields of BoxLayout class:

  1. public static final int X_AXIS
  2. public static final int Y_AXIS
  3. public static final int LINE_AXIS
  4. public static final int PAGE_AXIS

Constructor of BoxLayout class:

  1. BoxLayout(Container c, int axis): creates a box layout that arranges the components with the given axis.
  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class BoxLayoutExample1 extends Frame {
  4.  Button buttons[];
  5.  public BoxLayoutExample1 () {
  6.    buttons = new Button [5];
  7.    for (int i = 0;i<5;i++) {
  8.       buttons[i] = new Button (“Button “ + (i + 1));
  9.       add (buttons[i]);
  10.     }
  11. setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
  12. setSize(400,400);
  13. setVisible(true);
  14. }
  15. public static void main(String args[]){
  16. BoxLayoutExample1 b=new BoxLayoutExample1();
  17. }
  18. }

or,

  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class BoxLayoutExample2 extends Frame {
  4.  Button buttons[];
  5.  public BoxLayoutExample2() {
  6.    buttons = new Button [5];
  7.    for (int i = 0;i<5;i++) {
  8.       buttons[i] = new Button (“Button “ + (i + 1));
  9.       add (buttons[i]);
  10.     }
  11. setLayout (new BoxLayout(this, BoxLayout.X_AXIS));
  12. setSize(400,400);
  13. setVisible(true);
  14. }
  15. public static void main(String args[]){
  16. BoxLayoutExample2 b=new BoxLayoutExample2();
  17. }
  18. }

 

PinIt
submit to reddit

Leave a Reply

Top