You are on page 1of 19

Introduction to Object oriented

Programming CE-103

By: Engr. Habib Shaukat


Lecturer
EED
hshaukat@ssuet.edu.pk
Object:

• Explain the concept of window based applications (AWT)


.
.
Theory:
• AWT stands for Abstract Window Toolkit. It is a platform dependent API for
creating Graphical User Interface (GUI) for java programs.
• API stands for application programming interface. This is a concept in
software technology that essentially refers to how multiple applications can
interact with and obtain data from one another
• AWT is the package of JAVA which contain the GUI components eg.. button ,
text fields , labels , choice , option , list component
• It also contain the classes interface of different listener which are used to execute
different action act by user at run time.
• Why AWT is platform dependent? Java AWT calls native platform (Operating systems)
subroutine for creating components such as textbox, checkbox, button etc.

• For example an AWT GUI having a button would have a different look and feel across platforms
like windows, Mac OS & Unix,

• this is because these platforms have different look and feel for their native buttons and AWT
directly calls their native subroutine that creates the button.

• In simple, an application build on AWT would look like a windows application when it runs on
Windows, but the same application would look like a Mac application when runs on Mac OS.
Java AWT Hierarchy

Container Label Checkbox Choice SCO allbar

Panel

Dialog
All methods of Components class are available in all inherited class
java.lang.Object
| +--java.awt.Component
| +--java.awt.Button

void setLabel(String label) java.lang.Object


| +--java.awt.Component
String getLabel()
| +--java.awt.TextComponent
boolean echoCharIsSet() | +--java.awt.TextField
int getColumns()
char getEchoChar()
void setColumns(int columns)
void setEchoChar(char c)
void setText(String t)
Components and containers
• All the elements like buttons, text fields, scrollbars etc are known as components.
• In AWT we have classes for each component as shown in the above diagram.
• To have everything placed on a screen to a particular position, we have to add them
to a container.
• A container is like a screen wherein we are placing components like buttons, text fields,
checkbox etc.
• In short a container contains and controls the layout of components.
• A container itself is a component (shown in the above hierarchy diagram) thus we can
add a container inside container.

Types of containers:
• As explained above, a container is a place wherein we add components like text field,
button, checkbox etc. There are four types of containers available in AWT: Window,
Frame, Dialog and Panel. As shown in the hierarchy diagram above, Frame and Dialog
are subclasses of Window class.
Window: An instance of the Window class has no border and no title
Dialog: Dialog class has border and title. An instance of the Dialog class cannot
exist without an associated instance of the Frame class.
Panel: Panel does not contain title bar, menu bar or border. It is a generic container
for holding components. An instance of the Panel class provides a container to
which to add components.
Frame: A frame has title, border and menu bars. It can contain several components
like buttons, text fields, scrollbars etc. This is most widely used container while
developing an application in AWT.

Java AWT Example


We can create a GUI using Frame in two ways:
1) By extending Frame class
2) By creating the instance of Frame class
Lets have a look at the example of each
one.
Example 1 add(l1); add(l2);
import java . awt .*;
add(t1); add(t2);
class awt2 extends add(b1); add(b2);
Frame{ Label l1,l2;
TextField t1,t2; show();} }
Button b1,b2;
public awt2(String t){ class showawt2{
super(t); public static void main(String arg[]){
awt2 w =new awt2("My 2nd AWT Frame");}}
l1=new Label(“User Name");
l2=new Label("Password");
t1=new TextField(20);
t2=new TextField(20);
b1=new Button("ok");
b2=new Button("CANCEL");
setLayout (new FlowLayout());
setSize(290,200);
Java –AWT Layout

• In JAVA there are different Layouts on which you can design your frames and
place components inside the frames according to the GUI presentation you
want. Following are the Layouts which you can use while working on AWT
Package
• FlowLayout
• GridBagLayout
• BorderLayout
• GridLayout
label

F1owL ayou I

One TNo

4GE ST.ART

Six LINE ST.ART CENTER LINE END

GP US Bag Lazy Ou £

AGE END
show(); }}
import java.awt.*;
class awtLayouts extends
Frame{ Button b1,b2,b3; TextField t1;
Label l1; public awtLayouts (String
title)
{
super(title);
//setLayout(new FlowLayout());
//setLayout(new BorderLayout());
//setLayout(new GridLayout());
//setLayout(new GridBagLayout());
t1 = new TextField();
l1 = new Label(" label ");
b1 = new Button(" Save ");
b2 = new Button(" Canel ");
b3 = new Button(" Delete ");
add(t1); add(l1); add(b1); add(b2);
add(b3);
class showawtLayout{
public static void main(String arg[]){

awtLayouts in1 = new


awtLayouts("Title");
}}
Interfaces and Listeners

• In AWT Lab we also uses the different listeners


ActionListener, WindowListener, FocusListener,
MouseListener, KeyListener
Steps
• Implements interface
• add listener in the component.
• Define or override the all functions of that listener in the class.
Rct ionL1st ener
E
u Rd j ustment L1st
E . ok ,) Temp in Fahrenheit:
ener Eont a1nerL1st
N

ener FocusL1st ener

I t emL1st ener
value = -15 degree
L
KeyL1st ener
I
HouseL1st ener
E
houseHot ionL1st ener
H
E Text L1st ener
R
V1ndoldL1st ener
L L

Example a
w
i t
m .
p e
o v
r e
t n
t
j s
a .
v *
a ;
. class area
a extends Frame
w implements
t ActionListener
. {
* Label l1,l2;
;
i
m
p
o
r
t
j
a
v
a
.
{
if(e.getSource()==
b2)
public void { System.exit(0)
actionPerformed(Ac ; }
tionEvent e)
TextField t1,t2;
B t Button("Exit");
u ) setLayout(new
t { FlowLayout());
t super(t);
o l1=new
n Label("E
b nter
1 Radius")
, ;
b l2=new
2 Label("
; Area
p is");
u t1=new
b TextFiel
l d(4);
i t2=new
c TextFiel
a d(4);
r t2.setEd
e itable(fa
a lse);
( b1=ne
S w
t Button(
r "Calcul
i ate");
n b2=ne
g w
if(e.getSo *r;
urce()==b t2.setEditab
1) le(true);
{ t2.setText(
fl
b1.addActionL o .
String valu
istener(this); at eOf(a));
b2.addActionL r, t2.setEditab
istener(this); a; le(false);
add(l1); r= show(); }
add(t1); Fl } }
add(l2); oa
add(t2); t.
add( pa
b1); rs
add( e
b2); Fl
setSi oa
ze(4 t(t
00,3
00); 1.
sho ge
w(); tT
} ex
t()
);
a
=(
fl
o
at
)3
.1
4
2
*r
Area of circle

You might also like