Dr. Rajeshree Khande :Introduction to Java AWT

DrRajeshreeKhande 319 views 63 slides May 16, 2020
Slide 1
Slide 1 of 63
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63

About This Presentation

Java AWT (Abstract Window Toolkit) an API to develop GUI window-based applications in java.


Slide Content

AWT :Abstract Window ToolKit
AWT is collection of classes and methods that allows
to create & manage window.
AWT provide support for Applet.
AWT contains support for windows_ based Graphical Interface.
To use the AWT classes & method we have to import
awt package as
import java.awt.*;
1RAJESHREE KHANDE

AWT:classes
Different window classes are defined by AWT which add
functionality with each level.
Component
Container
Window Panel
Frame
Applet
2RAJESHREE KHANDE

3RAJESHREE KHANDE

AWT:classes
Component :
It’ an abstractclass.
All GUI component that interact with user are
subclasses of component class.
Defines various methods for managing eventsuch
as keyboard & mouse input.
Also defines attribute to store current foreground
& background colors and fonts
4RAJESHREE KHANDE

AWT:classes
Container:
Inherits various methods & attribute of component
class
Having additional methods of nesting other
component object.
A container position various GUI component
within it with the help of different Layout manager
5RAJESHREE KHANDE

AWT:classes
Panel :
It is a window which does not contain title bar,
menu bar, border.
It is super class of Appletclass.
Any screen output written on applet is drawn on
the surface of panel object.
AppletViewer provide titlebar & border to an
applet.
We add GUI component such as buttons ,list box
to a panel.
6RAJESHREE KHANDE

AWT:classes
Window :
Window class is used to create top level window.
Window object are not created directly.
Subclass of window is used to place on desktop.
7RAJESHREE KHANDE

AWT:classes
Frame:
It is subclass of window.
It has title bar , menu bar & borderetc
To create a normal windowin Java application Frame class
is used.
In Applet we does not use Frame class, since AppletViewer
provide titlebar, menubar & boarder to an applet.
8RAJESHREE KHANDE

AWT:classes
Canvas:
It is not subclass of window or panel.
It is another type of window.
This widow is blank window upon which we can
draw.
9RAJESHREE KHANDE

Layout Managers
It is an object which determine the way that
component are arranged in frame window.
It arranges a control within a window using some
type of an algorithm.
Each window has a layout manager associated with
it.
10RAJESHREE KHANDE

Layout Managers
LayoutManager is set by the setLayout()method
to a window.
syntax
void setLayout(LayoutManager layobj)
If it is not set then default layout manager is used
If layobjparameter set to nullthen control are
positioned manually using the setBound()method
11RAJESHREE KHANDE

LayoutManager Classes
1.FlowLayout
Default frame layout.
Places Components in successive rows in window fitting as
many as in each row as possible.
When the row is full it start fitting the components on the
next row.
This is similar to how word flow in text editor
A small space is left in each component.
12RAJESHREE KHANDE

LayoutManager Classes :FlowLayout
 Following are constructor for FlowLayout.
1.FlowLayout()
Centers all component and leves 5 pixel space between each component
2.FlowLayout(int align)
Flowlayout.LEFT
Flowlayout.CENTER
Flowlayout.RIGHT
Flowlayout.LEADING
Flowlayout.TRAILING
3.FlowLayout(int align, int hspace, int vspace)
Example
13RAJESHREE KHANDE

LayoutManager Classes :BorderLayout
Frame is divided into north, south, east, west, and
center
Components are placed by the programmer in the
desired location using the addmethod
14RAJESHREE KHANDE

15RAJESHREE KHANDE

LayoutManager Classes :BorderLayout
Following are constructorfor BorderLayout
1)BorderLayout()
2)BorderLayout(int hspace, int vspace)
To add a component in BorderLayout use following
format of add() method
Syntax :
void add(Component-obj, Object Region)
16RAJESHREE KHANDE

LayoutManager Classes :BorderLayout
Where region specifies following five
constant:
1.BorderLayout.EAST
2.BorderLayout.WEST
3.BorderLayout.SOUTH
4.BorderLayout.NORTH
5.BorderLayout.CENTER
Example
17RAJESHREE KHANDE

LayoutManager Classes
:GridLayout
2.GridLayout
Frame is declared as a grid of fixed size
(e.g. two rows and three columns)
Components are placed in the grid left to
right and top to bottom
18RAJESHREE KHANDE

LayoutManager Classes
:GridLayout
GridLayout uses following Constructor
1) GridLayout()
Creates a single column grid Layout
2) GridLayout(int nrows, int ncols)
Creates a grid layout with specified no. of rows and
columns
3) GridLayout(int nrows, int ncols, int hspcae, int vspace)
Creates a grid layout with specified no. of rows and
columns and specifies horizontal and vertical space
between the component
19RAJESHREE KHANDE

LayoutManager Classes
:GridLayout
Example1
Example1
20RAJESHREE KHANDE

AWT Component
Adding a control
1.Create an instance of desired control
2.Then add it to window by calling add() method which is define by
container
3.The add method has several form
4.Once the control has been added it will be automatically visible
whenever it’s parent window is display
Syntax
Component add(Component compObj)
21RAJESHREE KHANDE

AWT Component
Removing a control
If control no longer need we will remove that control
by remove()method
This method also defined by Container class
Syntax
void remove (Component compObj)
We can remove all controls by removeAll()method
22RAJESHREE KHANDE

AWT Component
Responding to controls
1.All control except label generates events when they
are accessed by user.
2.In general , your program simply implements the
appropriate interface & register an event listener
for each control that need to monitor.
23RAJESHREE KHANDE

AWT Component
Label
1.It is an object type Label
2.It is a passive control
3.Do not support any interaction
Following Constructor
1.Label() : Construct blank label
2.Label(String) : construct a label containing string
3.Label(String, int) :
Alignment
( Label.LEFT,Label.RIGHT,
Label.CENTER)
24RAJESHREE KHANDE

AWT Component :Label
Methods
Sr.
No
Methods Description
1void setText(String)Set or change a text of label
2 String getText() Obtain the text of label
3 void setAlignment(int)Set or change the alignment
of text in the label
4 int getAlignment()Obtain the current alignment
25RAJESHREE KHANDE

AWT Component :Push Button
1.It is push button component
2.Contain a label
3.Generate a event when it is pressed
4.It is an objectof type Button
5.Button defines two constructor
1. Button(): Create a buttton without label
2. Button (String str):Creates a button with label
26RAJESHREE KHANDE

Methods
Sr.
No
Methods Description
1 void setLabel(String)Set or change a label of
Button
2 String getLabel() Obtain the label Button
AWT Component :Push Button
27RAJESHREE KHANDE

AWT Component :TextField
TextField class is used to create a single line text
entry edit box.
Constructor
1. TextField(): Creates a default textfield
2. TextField(int): Creates a textfield which wide as
specified number of characters.
3. Textfield(String): Creates a textfield with specified
string initially.
4. TextField(String, int): Creates a textfield with initial
text and set it’s width.
28RAJESHREE KHANDE

AWT Component :TextField
Methods
Sr.
No
Methods Description
1String getText() Return the content of TextField.
2void setText(String) Set or change the text of TextField.
3String getSelectedText() To get currently selected text
4void select(int startIndex, int
endIndex)
To select portion of text
5boolean isEditable() To determine editability
6void setEditable(boolean) To set or reset editability 29RAJESHREE KHANDE

AWT Component :TextField
Example1 :Button and Label
Example2 : Conversion to binary ,Hexadecimal, Octal
Example 3: Converting String to Uppercase and
Lowercase
30RAJESHREE KHANDE

AWT Component :Check Box
Used to toggle an option
A Checkbox class is used to create an object
Constructor
1. Checkbox():
Creates a checkbox without label.
2. Checkbox(String):
Creates a checkbox with label specified initial state is
unchecked.
3 Checkbox(String, boolean):
Creates a checkbox with label specified initial state is
either true or false.
31RAJESHREE KHANDE

AWT Component :Check Box
Methods
Sr.
No
Methods Description
1 Boolean getState()Retrieve the current state
2 void setState(boolean)Set the state true or false
3 String getLabel() Retrieve the label associated
4 Void setLabel(String)Sets the label as specified
32RAJESHREE KHANDE

AWT Component :CheckBoxGroup
It is also called radio buttons
CheckBoxGroupclass is used to create group of
check boxes.
From list of choices we select only one at a time.
Constructor
CheckboxGroup()
e.g.
CheckBoxGroup cbg = new CheckBoxGroup()
c1 = new Checkbox("check-1“,cbg,true);
c2 = new Checkbox("check-2“,cbg,false);
c3 = new Checkbox("check-3“,cbg,false);
33RAJESHREE KHANDE

AWT Component :CheckBoxGroup
Method :
1)getSelectedCheckbox(): determine which
checkbox in a group is currently selected
Checkbox getSelectedCheckbox()
2)setSelectedCheckbox(): set a check box
void setSelectedCheckbox(Checkbox obj))
34RAJESHREE KHANDE

AWT Component :List
Provide a list of items, which can be scrolled
From list of items single or multiple item can be
selected.
Constructor
1. List(): Creates a listbox in which single selection is
possible.
2. List(int):Creates a listbox , specified number of
items always to visible selection.
Selection is possible
35RAJESHREE KHANDE

AWT Component :List
3. List(int, boolean) :
Specified no. of items
To be visible
Specifies wheather
multiple selection is
allowed
True Indicate multiple
Selection
36RAJESHREE KHANDE

AWT Component :List
Sr.
No
Methods Description
1Void add(string) Add item to the end of list
2Void add(String,int) Add the item at the index specified
in second parameter.
3String getSelectedItem() Returns a string which is selected
4int getSelectedIndex() Return an index of selected item
5String[] getSelectedItems()Returns an array contains string
which are selected.(Multiselect)
37RAJESHREE KHANDE

AWT Component :List
Sr.
No
Methods Description
6Int[] getSelectedIndexes()Returns an array containing
indexes of selected items.
7Int getItemCount() Returns no. of items in the list
8Void select(int) Select the item of specified index
9String getItem(int) Obtain the name of item having
specified index.
38RAJESHREE KHANDE

AWT Component :List
Example1
Example2
39RAJESHREE KHANDE

AWT Component :Choice list
The class Choice is used to create a drop-down or
pop-up list of string
It requires less space than the list box control
It uses only default constructor
Choice()
40RAJESHREE KHANDE

AWT Component :Choice list
Sr.
No
Methods Description
1void add(String) To add a string in choice list
2String getSelectedItem()Determine which item is currently selected
3int getSelectedIndex()Determine the index of currently selected
item.
4int getItemCount() Returns no. of items in the list
5void select(int) Select the item whose index is specified
6void select(String) Select the item match with string parameter
7String getItem(int) Obtain the name associated with the item at
specified index
41RAJESHREE KHANDE

AWT Component :Choice list
Example of Choice list
42RAJESHREE KHANDE

AWT Component :TextArea
TextAreaclass is used to create a multilinetextbox
Following constructor are available
1. TextArea():
Creates a default TextArea
2. TextArea(int, int):
Creates a TextArea with specified no. of lines &
characters.
43RAJESHREE KHANDE

AWT Component :TextArea
3. TextArea(String) :
Create with initial string.
4. TextArea(Sring, int, int) :
Creates a TextArea with initial string and for
specified no. of lines & characters.
5. TextArea(String, int numlines, int nchar, int s bars)
Creates a TextArea with Scroll Bars. The value of
sBars must be one of the following
SCROLLBAR_BOTH, SCROLLBAR_NONE,
SCROLLBAR_HORIZONTAL_ONLY,
SCROLLBAR_VERTICAL_ONLY
44RAJESHREE KHANDE

AWT Component :TextArea
Support all the methods of TextField & some
additional method
Sr.
No
Methods Description
1void append(String) Append Specified string at the
end.
2void insert(string,int)Insert a string at specified index
3void replace(String, int
startIndex, endIndex)
It replaces the character from
start index to end with given
string
45RAJESHREE KHANDE

AWT Component :Scroll Bars
Scrollbarclass is used to create scrollbar.
Scrollbar may be either vertical or horizontal.
Constructor
1. Scrollbar() :
Create vertical scrollbar.
2. Scrollbar(int style):
Create a scrollbar of specified style. It can be
Scrollbar.HORIZONTAL
Scrollbar.VERTICAL
46RAJESHREE KHANDE

AWT Component :Scrollbar
3.Scrollbar(int style, int start, int thumb,int min, int max) :
Create a scrollbar with specified style.
Initial Value
Thumbsize
47RAJESHREE KHANDE

AWT Component :Scrollbar
Sr.
No
Methods Description
1int getValue() To get the current value of scroll bar
2void setValue(int) Set new value
3int getMinimum() To get minimum value of scrollbar
4int getMaximum() To get maximum value of scrollbar
5void setUnitIncrement(int)To change unit increment.
Default is 1
6void setBlockIncrement(int)To change block increment.
Default is 10
48RAJESHREE KHANDE

Graphics
AWT support a rich assortment of Graphics methods.
All graphics are drawn relative to window.
This can be main window of an applet, Child window
of an applet or stand alone application window
The origin of window is at the Top-left corner and is
(0,0).
Co-ordinates is specified in pixel.
All output to a window takes place through a
graphics context.
49RAJESHREE KHANDE

Graphics
Graphics Context is encapsulated by Graphics class&
is obtained in two ways.
1. It is passed to an applet when one of it’s various
methods, such as paint()or update() .
2. It is returned by getGraphics()method of Component
The Graphicsclass defines the a number of
drawing functions
Each shape can be drawn edge-only or filled
50RAJESHREE KHANDE

Graphics
Object are drawn and filled in currently selected
graphics color, which is black by default.
When a graphics object is drawn that exceedsthe
dimension of the window, output is automatically
clipped
51RAJESHREE KHANDE

Drawing Lines
Lines are drawn by means of the drawLine()method
void drawLine(int startX, int startY,int endX, endY)
drawLine() display a line in the current drawing
color.
Example
52RAJESHREE KHANDE

Drawing Rectangle
drawRect()method display an outline of rectangle
fillRect()method filled the rectangle.
void drawRect(int top,int left,int width, int height)
void fillRect(int top, int left, int width, int height)
The upper-left corner of the rectangle is top,left
The dimension of the rectangle are specified by
width and height.
53RAJESHREE KHANDE

Drawing Rounded Rectangle
To draw Rounded Rectangle use drawRoundRect()of
fillRoundRect().
void drawRect(int top,int left,int width, int height)
void fillRect(int top, int left, int width, int height)
54RAJESHREE KHANDE

LayoutManager Classes
:CardLayout
CardLayout
--Display containers you pass to it as card.
--You give each card a name.
--The card are typically held in an object of type Panel
--We can movefrom card to card with the Card’s
layout show()method.
--We can also display specific cardsusing first , last, next & previous
method of CardLayout.
55RAJESHREE KHANDE

is encapsulated by graphics class & is obtained in two
ways.
56RAJESHREE KHANDE

LayoutManager Classes
:CardLayout
Constructor
1) CardLayout() :
creates new card layout
2) CardLayout(int hspace,int vspace)
creates new card layout with given horizontal and
vertical space
57RAJESHREE KHANDE

LayoutManager Classes
:CardLayout
Step for CardLayout
1.Create a panel that contains a deck and panel for each card
in the deck.
2.Add to the appropriate panel the components that form
each card.
3.Then add these panels to the panel for which CardLayout is
the layout manager
4.Finally you add this panel to window
5.To select between the cards ,include one push button for
each card in the deck.
58RAJESHREE KHANDE

LayoutManager Classes
:CardLayout
When panels are added to panel, they are given a name.
So use the following add method
Void add(Component panelObj, Object name)
Name of the card whose
panel is specified by
panelObj
59RAJESHREE KHANDE

LayoutManager Classes
:CardLayout
After deck creation, you can activate a card by
calling one of the method defined by CardLayout
1.void first(Container deck)
2.void last(Container deck)
3.void next(Container deck)
4.void previous(Container deck)
5.void show(Container deck,String cardName)
60RAJESHREE KHANDE

LayoutManager Classes
:CardLayout
deck: is the reference to the container(usually panel)
that holds the card
cardName : is the name of the card
Example
61RAJESHREE KHANDE

Scrollbars
The leftmost position of a horizontal scrollbar
corresponds to some integer value and the rightmost
position corresponds to a larger integer value
Scrollbars can be displayed vertically
User movement options
Clicking either end button causes bubble to move in
unit increments.
Clicking the are between bubble and end button
causes movement in 10 unit increments
Clicking and dragging the bubble in the desired
direction
62RAJESHREE KHANDE

Using Inheritance
It is possible to use inheritance to allow
TwoButtonsto become a frame in its own
right
Note that the UML diagram will show
TwoButtonsas a subclass of Frame
You will need to use super()to set the
frame title
63RAJESHREE KHANDE