Example 1
Home ] Up ] [ Example 1 ] Example 2 ] Example 3 ] Example 4 ] Example 5 ] Example 6 ]

 

 

Here's an example of the use of BoxLayout:

package swingExamples;

import java.awt.Container;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class BoxLayoutExamplePanel extends JPanel
{
  public BoxLayoutExamplePanel()
  {
    setLayout( new BoxLayout(this, BoxLayout.Y_AXIS) );
    add(new JButton("Click me"));
    add(new JLabel("I am a label"));
    add(new JTextField("Text field"));
    add(new JButton("A very long button label is this..."));
  }
}

public class BoxLayoutExample1 extends JFrame
{
  public BoxLayoutExample1()
  {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("BoxLayout");
    Container contentPane = getContentPane();
    contentPane.add( new BoxLayoutExamplePanel() );
  }
  
  public static void main(String[] args)
  {
    JFrame frame = new BoxLayoutExample1();
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

which produces the following:

 

This page was last modified on 02 October, 2007