Friday, February 20, 2009

BoxLayout Manager

BoxLayout arranges components either horizontally or vertically in a panel. You can control alignment and spacing of the components. Complicated layouts can be made by combining many panels, some with horizontal layout and some with vertical layouts.

Constructor:-
new BoxLayout(componenet obj,AXIS)

Explanation:-

Componenet obj--->name of obect to be added.
AXIS--->name of axis,X_AXIS for horizontal and Y_AXIS for vertical

Example
Source Code:-
For Horizontal:-


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

public class boxx extends JFrame{
public static void main(String a[]){
JFrame frm=new JFrame("BoxLayout HORIZONTAL");
JPanel panel=new JPanel();
JButton but=new JButton("Button1");
JButton but1=new JButton("Button2");
JButton but2=new JButton("Button3");
JButton but3=new JButton("Button4");
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(but);
panel.add(but1);
panel.add(but2);
panel.add(but3);
frm.getContentPane().add(panel,"Center");
frm.setSize(350,150);
frm.setVisible(true);

}
}


For Vertical:-

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

public class boxy extends JFrame{
public static void main(String a[]){
JFrame frm=new JFrame("BoxLayout VERTICAL");
JPanel panel=new JPanel();
JButton but=new JButton("Button1");
JButton but1=new JButton("Button2");
JButton but2=new JButton("Button3");
JButton but3=new JButton("Button4");
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.add(but);
panel.add(but1);
panel.add(but2);
panel.add(but3);
frm.getContentPane().add(panel,"Center");
frm.setSize(350,150);
frm.setVisible(true);

}
}

Other Layout Managers Links :-

Layout Manager Basics
Flow Layout
Grid Layout
Border Layout
Card Layout
GridBag Layout

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey(Read More about him) .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP