Module 6 - Graphical User Interface in JAVA

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Module 6 – Graphical User

Interface in JAVA
GUI design in Java

 A graphical user interface (GUI) presents a pictorial interface to


a program, GUI's allow the user to work in a more productive
manner.

 A GUI component is an object with which the user interacts


via the mouse or keyboard, below are some of the Java GUI
components
 Some of the basic GUI components are
 JLabelAn area where uneditable text or icons can be displayed
 JTextFieldAn area where the user inputs data from the keyboard, this area can also
display data
 JButtonAn area that triggers an event when clicked
 JCheckBoxA GUI component that is either selected or not selected
 JComboBoxA drop-down list of items from which the user can make a selection
by clicking an item in the list or typing into the box
 JListAn area where a list of items is displayed from which the user can make a
selection by clicking once on any element in the list.
Double-clicking an element in the list generates an action event, Multiple elements
can be selected
 JPanelA container in which components can be placed
Java Layout Managers

 The GUI is made up of a number of components, the


Layout Managers affect the content pane.

 Layout Managers are provided to arrange GUI components


on a container for presentation purposes. This allows the
programmer to concentrate on the basic "look and feel" and
lets the layout managers process most of the layout details
 Several AW T and Swing classes provide layout managers for general use:

1. BorderLayout

2. BoxLayout

3. CardLayout

4. FlowLayout

5. GridBagLayout

6. GridLayout

7. GroupLayout

8. SpringLayout
Here are 5 of the layout managers.
BorderLayout

 The class BorderLayout arranges the components to fit in the five regions: east,
west, north, south and center.
Each region is can contain only one component and each component in each region is
identified by the corresponding constant NORTH, SOUTH, EAST, WEST, and
CENTER.
 Class declaration
 Following is the declaration
for java.awt.BorderLayout class:
 public class BorderLayout extends Object implements LayoutManager2,
Serializable
 Here are the constructors defined by BorderLayout:

1. BorderLayout()

2. BorderLayout(int horz, int vert)

the first form creates a default border layout. The second allows you to specify the
horizontal and vertical space left between components in horz and vert, respectively.

BorderLayout defines the following constants that specify the regions.

BorderLayout.CENTER, BorderLayout.SOUTH,
BorderLayout.EAST, BorderLayout.WEST,
BorderLayout.NORTH
CardLayou t

 The class CardLayout arranges each component in the container as a card. Only
one card is visible at a time, and the container acts as a stack of cards.
 Class declaration
 Following is the declaration
for java.awt.CardLayout class:
 public class CardLayout extends Object implements LayoutManager2,
Serializable
 CardLayout provides these two constructors:

1. CardLayout()

2. CardLayout(int horz, int vert)


FlowLayou t

 The class FlowLayout components in a left-to-right flow.


 This is the most basic layout manager, components are placed from left to
right as they were added, when the edge is reached the components are put on
the next line. You can align the components left, right or center (default).
 Class declaration
 Following is the declaration
for java.awt.FlowLayout class:
 public class FlowLayout extends Object implements LayoutManager,
Serializable
 Here are the constructors for FlowLayout:

1. FlowLayout()

2. FlowLayout(int how)

3. FlowLayout(int how, int horz, int vert)

The first form creates the default layout, which centers components and
leaves five pixels of space between each component. The second form lets
you specify how each line is aligned. Valid values of how are as follows:
FlowLayout.LEFT
FlowLayout.CENTER

FlowLayout.RIGHT
GridLayou t

 Introduction
 The class GridLayout arranges components in a rectangular
grid.
 Class declaration
 Following is the declaration
for java.awt.GridLayout class:
 public class GridLayout extends Object implements
LayoutManager, Serializable
 GridLayout are shown here:

1. GridLayout()

2. GridLayout(int numRows, int numColumns)

3. GridLayout(int numRows, int numColumns, int horz, int vert)


GridBagLayout

 The class GridBagLayout arranges components in a


horizontal and vertical manner.

 Class declaration

 Following is the declaration


for java.awt.GridBagLayout class:

 public class GridBagLayout extends Object implements


LayoutManager2, Serializable
GroupLayout
 GroupLayout is a layout manager that was developed for use by GUI
builder tools, but it can also be used
manually. GroupLayout works with the horizontal and vertical layouts
separately. The layout is defined for each dimension independently.
Consequently, however, each component needs to be defined twice in the
layout. The Find window shown above is an example of a GroupLayout. .
 Class declaration
 Following is the declaration
for javax.swing.GroupLayout class:
 public class GroupLayout extends Object implements
LayoutManager2
S pringLayou t
 SpringLayout is a flexible layout manager designed for use by GUI builders.
It lets you specify precise relationships between the edges of components
under its control. For example, you might define that the left edge of one
component is a certain distance (which can be dynamically calculated) from
the right edge of a second component.SpringLayout lays out the children of
its associated container according to a set of constraints, as shall be seen in
 Class declaration
 Following is the declaration
for javax.swing.SpringLayout class:
 public class SpringLayout extends Object implements
LayoutManager2

You might also like