Layout Managers

Add to Favourites
Post to:

Description
automatically arranges your controls within a window by using some type of algorithm.
Each Container object has a layout manager associated with it.
The layout manager is set by the setLayout( ) method. If no call to setLayout( ) is made, then the default layout manager is used. Whenever a container is resized (or sized for the first time), the layout manager is used to position each of the components within it.
The setLayout( ) method has the following general form:
void setLayout(LayoutManager layoutObj)
FLOW LAYOUT:Default layout manager
Implements simple layout style, similar to how words flow in a text editor.
Components are laid out from upper left, left to right, top to bottom
When no more components are fit to line, next component will be in next line.
Constructors:
FlowLayout.LEFT : left alignment
FlowLayout.CENTER : center alignment
FlowLayout.RIGHT : right alignment


Type: ppt

Discussion
Presentation Transcript Presentation Transcript

Layout Managers : Layout Managers Neenu

Layout Manager : Layout Manager automatically arranges your controls within a window by using some type of algorithm. Each Container object has a layout manager associated with it. The layout manager is set by the setLayout ( ) method. If no call to setLayout ( ) is made, then the default layout manager is used. Whenever a container is resized (or sized for the first time), the layout manager is used to position each of the components within it. The setLayout ( ) method has the following general form: void setLayout ( LayoutManager layoutObj )

FlowLayout : FlowLayout Default layout manager Implements simple layout style, similar to how words flow in a text editor. Components are laid out from upper left, left to right, top to bottom When no more components are fit to line, next component will be in next line. Constructors: FlowLayout.LEFT : left alignment FlowLayout.CENTER : center alignment FlowLayout.RIGHT : right alignment

PowerPoint Presentation : import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class FlowLayoutDemo extends Applet implements ItemListener { String msg = ""; Checkbox Win98, winNT, solaris, mac; public void init() { // set left-aligned flow layout setLayout(new FlowLayout(FlowLayout.LEFT)); Win98 = new Checkbox("Windows 98/XP", null, true); winNT = new Checkbox("Windows NT/2000"); solaris = new Checkbox("Solaris"); mac = new Checkbox("MacOS"); add(Win98); add(winNT); add(solaris); add(mac); // register to receive item events Win98.addItemListener(this); winNT.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } // Repaint when status of a check box changes. public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current state: "; g.drawString(msg, 6, 80); msg = " Windows 98/XP: " + Win98.getState(); g.drawString(msg, 6, 100); msg = " Windows NT/2000: " + winNT.getState(); g.drawString(msg, 6, 120); msg = " Solaris: " + solaris.getState(); g.drawString(msg, 6, 140); msg = " Mac: " + mac.getState(); g.drawString(msg, 6, 160); } }

BorderLayout : BorderLayout Implements a common style for toplevel windows 4 narrow,fixed width components at the edges and one large area in the center 4 sides referred to north,south,east,west. Middle area is called center. Constructors: BorderLayout() : default border layout BorderLayout(int horz,int vert) :specifies horizontal and vertical space between elements.

PowerPoint Presentation : 4 border layout constants BorderLayout.CENTER BorderLayout.SOUTH BorderLayout.EAST BorderLayout.NORTH BorderLayout.WEST

PowerPoint Presentation : import java.awt.*; import java.applet.*; import java.util.*; /* */ public class BorderLayoutDemo extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button("This is across the top."), BorderLayout.NORTH); add(new Label("The footer message might go here."), BorderLayout.SOUTH); add(new Button("Right"), BorderLayout.EAST); add(new Button("Left"), BorderLayout.WEST); String msg = "The reasonable man adapts" + "himself to the world;\n" + "the unreasonable one persists in " + "trying to adapt the world to himself.\n" + "Therefore all progress depends " + "on the unreasonable man.\n\n" + " - George Bernard Shaw\n\n"; add(new TextArea(msg), BorderLayout.CENTER); } }

GridLayout : GridLayout Layout components in 2 dimensional grid. Specify number of rows and columns Constructors: GridLayout() :single column layout GridLayout(int numRows,int numCols) :with specified no of rows and cols GridLayout(int numRows,int numCols,int horz,int vert) :specify horizontal and vertical space between components.

PowerPoint Presentation : import java.awt.*; import java.applet.*; /* */ public class GridLayoutDemo extends Applet { static final int n = 4; public void init() { setLayout(new GridLayout(n, n)); setFont(new Font("SansSerif", Font.BOLD, 24)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int k = i * n + j; if(k > 0) add(new Button("" + k)); } } } }

CardLayout : CardLayout Different layouts 2constructors: CardLayout() :default card layout CardLayout(int horz,int vert) :Specifies horizontal and vertical space between elements.

PowerPoint Presentation : THANK YOU

Neenu Prasad
Looking to Learn for computer subjects
User
25 Followers

Your Facebook Friends on WizIQ