Layout Managers - Layout Managers Once we start adding more than one component to a container we need to consider how we organize\/arrange them For this

Layout Managers - Layout Managers Once we start adding more...

This preview shows page 1 - 4 out of 12 pages.

Layout ManagersOnce we start adding more than one component to a container, we need to consider how we organize/arrange them.For this purpose, we can use layout managers.A layout manageris an object that determines the manner in which components are arranged in a container.Here is a list of commonly used layout managers: (they are defined in java.awt package.)BorderLayout – it organizes components into five areas (North, South, East, West, and Center).FlowLayout – it organizes components from left to right, starting new rows as necessary.GridLayout – it organizes components into a grid of rows and columnsBoxLayout – it organizes components into a single row or a single column.CardLayout – it organizes components into one such that only one is visible at any time.GridBagLayout – it organizes components into a grid of cells, allowing components to span more than one cell.
import javax.swing.*; // to use Japplet, JButtonimport java.awt.*; // to import FlowLayout manager// and Containerpublic class AppletFlow extends JApplet{public void init(){Container content = getContentPane();// to use FlowLayout to add componentscontent.setLayout(new FlowLayout());// create three buttonsJButton button1 = new JButton("Button1");JButton button2 = new JButton("Button2");JButton button3 = new JButton("Button3");// add three buttons to the content pane of JApplet objectcontent.add(button1);content.add(button2);content.add(button3);setSize(270,70);}}An Example of Applet using FlowLayoutThis program using FlowLayout organizes three buttons in the following manner.If a user narrows the size of the applet, any component thatdid not fit in the first row will appear in the next row and so on.
An Example of Applet using BorderLayoutimport javax.swing.*; // to use Japplet, JButtonimport java.awt.*; // to import BorderLayout managerpublic class AppletBorder extends JApplet{public void init(){Container content = getContentPane();// to use BorderLayout to add componentscontent.setLayout(new BorderLayout());// create five buttonsJButton button1 = new JButton("Button1");JButton button2 = new JButton("Button2");

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture