[go: up one dir, main page]

0% found this document useful (0 votes)
63 views19 pages

Lecture-3.1.2

This document discusses various layout and component managers in Java. It describes different layout managers like BorderLayout, GridLayout, FlowLayout, BoxLayout and their usage. It also covers AWT components like buttons, checkboxes, choice buttons, labels, text fields and lists. The document provides code examples and discusses concepts like how FlowLayout arranges components from left to right, top to bottom. It concludes by mentioning that the session helped learn about layout and component managers in Java.

Uploaded by

Ýäšh Gãutâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views19 pages

Lecture-3.1.2

This document discusses various layout and component managers in Java. It describes different layout managers like BorderLayout, GridLayout, FlowLayout, BoxLayout and their usage. It also covers AWT components like buttons, checkboxes, choice buttons, labels, text fields and lists. The document provides code examples and discusses concepts like how FlowLayout arranges components from left to right, top to bottom. It concludes by mentioning that the session helped learn about layout and component managers in Java.

Uploaded by

Ýäšh Gãutâm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:

Layout and component managers

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• Layout and component
managers

2
Layout managers
The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an
interface that is implemented by all the classes of layout managers.
There are following classes that represents the layout managers:
• java.awt.BorderLayout
• java.awt.FlowLayout
• java.awt.GridLayout
• java.awt.CardLayout
• java.awt.GridBagLayout
• javax.swing.BoxLayout
• javax.swing.GroupLayout
• javax.swing.ScrollPaneLayout
• javax.swing.SpringLayout etc.
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south,
east, west and center. Each region (area) may contain one component only. It is the
default layout of frame or window.

The BorderLayout provides five constants for each region:


• public static final int NORTH
• public static final int SOUTH
• public static final int EAST
• public static final int WEST
• public static final int CENTER
Constructors of BorderLayout class:
• BorderLayout(): creates a border layout but with no gaps between the
components.
• JBorderLayout(int hgap, int vgap): creates a border layout with the
given horizontal and vertical gaps between the components.
Java GridLayout

The GridLayout is used to arrange the components in rectangular grid. One


component is displayed in each rectangle.

Constructors of GridLayout class

GridLayout(): creates a grid layout with one column per component in a row.

GridLayout(int rows, int columns): creates a grid layout with the given rows
and columns but no gaps between the components.

GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout
with the given rows and columns alongwith given horizontal and vertical gaps.
Example of GridLayout class
Java FlowLayout

The FlowLayout is used to arrange the components in a line, one after another (in a
flow). It is the default layout of applet or panel.

Fields of FlowLayout class


public static final int LEFT
public static final int RIGHT
public static final int CENTER
public static final int LEADING
public static final int TRAILING
Constructors of FlowLayout class
FlowLayout(): creates a flow layout with centered alignment and a default 5 unit
horizontal and vertical gap.
FlowLayout(int align): creates a flow layout with the given alignment and a default
5 unit horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.

Example of FlowLayout class


Java BoxLayout
The BoxLayout is used to arrange the components either vertically or
horizontally. For this purpose, BoxLayout provides four constants. They are as
follows:

Note: BoxLayout class is found in javax.swing package.

Fields of BoxLayout class


public static final int X_AXIS
public static final int Y_AXIS
public static final int LINE_AXIS
public static final int PAGE_AXIS
Constructor of BoxLayout class
BoxLayout(Container c, int axis): creates a box layout that arranges the
components with the given axis.

Example of BoxLayout class with Y-AXIS:


AWT Components
A component is an object with a graphical representation that can be displayed on
the screen and that can interact with the user.
The Component class is the abstract parent of the nonmenu-related AWT
components.

Button (java.awt.Button)
The two constructors are:
Button() // Constructs a Button with no label.
Button(String label) // Constructs a Button with the specified label.

12
AWT Components
Checkboxes (java.awt.Checkbox)
Checkboxes are two states, on and off. The state of the button is returned as the
Object argument, when a Checkbox event occurs. To find out the state of a
checkbox object we can use getState() that returns a true or false value. We can
also get the label of the checkbox using getLabel() that returns a String object.

Radio Buttons (java.awt.CheckboxGroup)


Is a group of checkboxes, where only one of the items in the group can be selected
at any one time.

13
AWT Components
Choice Buttons (java.awt.Choice)
Like a radio button, where we make a selection, however it requires less space and
allows us to add items to the menu dynamically using the addItem() method.

Java AWT List


The object of List class represents a list of text items. By the help of list, user can
choose either one item or multiple items. It inherits Component class.
AWT List class Declaration
public class List extends Component

14
AWT Components
Labels (java.awt.Label)
Allow us to add a text description to a point on the applet or application.

TextFields (java.awt.TextField)
There are numerous ways that we can construct a Textfield object:
TextField text1 = new TextField(); // no properties
TextField text2 = new TextField("Some text"); // a textfield with a predefined String
TextField text3 = new TextField(40); // a textfield with a predefined size
TextField text4 = new TextField("Some text", 50); // combination of the two

15
MCQ
What pattern does the FlowLayout layout manager use to add components to a container?

Choose at least one correct answer


Left to right, top to bottom
bottom to top, right to left
top to bottom, centered in each row

16
Summary:

In this session, you were able to :


• Learn about Layout and component managers
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Video Lectures :
https://youtu.be/utouefZ6WpU
https://youtu.be/__fZuZeq620

Reference Links:
http://www.eeng.dcu.ie/~ee553/ee402notes/html/ch09s02.html
https://www.javatpoint.com/java-awt
https://www.javatpoint.com/java-layout-manager
THANK YOU

You might also like