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

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

Layout Managers Once 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 manager is 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 columns BoxLayout – 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 import java.awt.*; // to import FlowLayout manager public class AppletFlow extends JApplet { public void init() { Container content = getContentPane(); // to use FlowLayout to add components content.setLayout(new FlowLayout()); // create three buttons Button button1 = new Button("Button1"); Button button2 = new Button("Button2"); Button button3 = new Button("Button3"); // add three buttons to the content pane of JApplet object content.add(button1); content.add(button2); content.add(button3); setSize(270,70); } } An Example of Applet using FlowLayout This program using FlowLayout organizes three buttons in the following manner. If a user narrows the size of the applet, any component that did not fit in the first row will appear in the next row and so on.
An Example of Applet using BorderLayout import javax.swing.*; // to use JApplet import java.awt.*; // to import BorderLayout manager public class AppletBorder extends JApplet { public void init() { Container content = getContentPane(); // to use BorderLayout to add components content.setLayout(new BorderLayout()); // create five buttons Button button1 = new Button("Button1"); Button button2 = new Button("Button2"); Button button3 = new Button("Button3");

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture