Layout Managers

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

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
import javax.swing.*; // to use Japplet, JButton import java.awt.*; // to import FlowLayout manager // and Container public class AppletFlow extends JApplet { public void init() { Container content = getContentPane(); // to use FlowLayout to add components content.setLayout(new FlowLayout()); // create three buttons JButton button1 = new JButton("Button1"); JButton button2 = new JButton("Button2"); JButton button3 = new JButton("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.
Background image of page 2
An Example of Applet using BorderLayout import javax.swing.*; // to use Japplet, JButton 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 JButton button1 = new JButton("Button1"); JButton button2 = new JButton("Button2"); JButton button3 = new JButton("Button3"); JButton button4 = new JButton("Button4");
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online