0% found this document useful (0 votes)
10 views18 pages

Unit-IV JAVA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Unit-IV JAVA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Uni

t-
IV
Ev
entandLi
stener(
Jav
aEv
entHandl
i
ng)
Changi
ngt
hest
ateofanobj
ecti
sknownasanev
ent
.Forexampl
e,cl
i
ckonbut
ton,
draggi
ngmouseet

Thej
ava.
awt
.ev
entpackagepr
ovi
desmanyev
entcl
assesandLi
steneri
nter
facesf
orev
enthandl
i
ng.

Jav
aEv
entcl
assesandLi
steneri
nter
faces
Ev
entCl
asses Li
stenerI
nter
faces

Act
ionEv
ent Act
ionLi
stener

MouseEv
ent MouseLi
stenerandMouseMot
ionLi
stener

MouseWheel
Event MouseWheel
List
ener

Key
Event Key
List
ener

I
temEv
ent I
temLi
stener

Text
Event Text
List
ener

Adj
ust
ment
Event Adj
ust
ment
List
ener

Wi
ndowEv
ent Wi
ndowLi
stener

Component
Event Component
List
ener
Cont
ainer
Event Cont
ainer
List
ener

FocusEv
ent FocusLi
stener

St
epst
oper
for
m Ev
entHandl
ing

Fol
l
owi
ngst
epsar
erequi
redt
oper
for
m ev
enthandl
i
ng:

1. Regi
stert
hecomponentwi
tht
heLi
stener

Regi
str
ati
onMet
hods

Forr
egist
eri
ngthecomponentwitht
heList
ener
,manycl
asses
pr
ovi
detheregi
str
ati
onmethods.Forexampl
e:

But
ton

publ
i
cvoi
daddAct
ionLi
stener
(Act
ionLi
stenera)
{}

MenuI
tem

publ
i
cvoi
daddAct
ionLi
stener
(Act
ionLi
stenera)
{}

Text
Fiel
d

publ
i
cvoi
daddAct
ionLi
stener
(Act
ionLi
stenera)
{}

publ
i
cvoi
daddText
List
ener
(Text
List
enera)
{}

Text
Area

publ
i
cvoi
daddText
List
ener
(Text
List
enera)
{}

Checkbox

publ
i
cvoi
daddI
temLi
stener
(I
temLi
stenera)
{}
Choi
ce

publ
i
cvoi
daddI
temLi
stener
(I
temLi
stenera)
{}

Li
st

publ
i
cvoi
daddAct
ionLi
stener
(Act
ionLi
stenera)
{}

publ
i
cvoi
daddI
temLi
stener
(I
temLi
stenera)
{}

_
___
___
___
___
___
___
___
___
___
___
___
___
___

Jav
aEv
entHandl
i
ngCode

Jav
aev
enthandl
ingbyi
mpl
ement
ingAct
ionLi
stener

i
mpor
tjav
a.awt
.*;

i
mpor
tjav
a.awt
.ev
ent
.*;

cl
assAEv
entext
endsFr
amei
mpl
ement
sAct
ionLi
stener
{

Text
Fiel
dtf
;

AEv
ent
(){

/
/cr
eat
ecomponent
s

t
f=newText
Fiel
d()
;

t
f.
set
Bounds(
60,
50,
170,
20)
;

But
tonb=newBut
ton(
"cl
i
ckme"
);

b.
set
Bounds(
100,
120,
80,
30)
;

/
/regi
sterl
i
stener

b.
addAct
ionLi
stener
(thi
s);
//passi
ngcur
renti
nst
ance
/
/addcomponent
sandsetsi
ze,
lay
outandv
isi
bil
i
ty

add(
b);

add(
tf)
;

set
Size(
300,
300)
;

set
Lay
out
(nul
l
);

set
Visi
ble(
true)
;

publ
i
cvoi
dact
ionPer
for
med(
Act
ionEv
ente)
{

t
f.
set
Text
("
Wel
come"
);

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{

newAEv
ent
();

publi
cvoidsetBounds(
intxaxi
s,i
ntyaxis,
intwidt
h,i
nthei
ght
);hav
e
beenusedintheaboveexamplethatsetstheposit
ionoft
hecomponentit
maybebutton,text
fi
eldet
c.
Di
spl
ayi
ngGr
aphi
csi
nAppl
et
j
ava.
awt
.Gr
aphi
cscl
asspr
ovi
desmanymet
hodsf
orgr
aphi
cspr
ogr
ammi
ng.

Commonl
yusedmet
hodsofGr
aphi
cscl
ass:
1.publ
icabst
ractv
oiddr
awSt
ri
ng(
Str
ingst
r,i
ntx,i
nty
):i
susedt
odr
aw t
hespeci
fi
ed
st
ri
ng.
2.publ
icv
oid dr
awRect
(i
ntx,i
nty
,intwi
dth,i
nthei
ght
):dr
awsar
ect
angl
ewi
tht
he
speci
fi
edwi
dthandhei
ght
.
3.publ
icabst
ractv
oidf
il
lRect
(i
ntx,
inty
,intwi
dth,
inthei
ght
):i
susedt
ofi
l
lrect
angl
ewi
th
t
hedef
aul
tcol
orandspeci
fi
edwi
dthandhei
ght
.
4.publ
icabst
ractv
oiddr
awOv
al(
intx,
inty
,intwi
dth,
inthei
ght
):i
susedt
odr
awov
alwi
th
t
hespeci
fi
edwi
dthandhei
ght
.
5.publ
icabst
ractv
oidf
il
lOv
al(
intx,i
nty
,intwi
dth,i
nthei
ght
):i
susedt
ofi
l
lov
alwi
tht
he
def
aul
tcol
orandspeci
fi
edwi
dthandhei
ght
.
6.publ
icabst
ractv
oiddr
awLi
ne(
intx1,i
nty
1,i
ntx2,i
nty
2):i
susedt
odr
awl
i
nebet
ween
t
hepoi
nts(
x1,
y1)and(
x2,
y2)
.
7.publ
icabst
ractbool
eandr
awI
mage(
Imagei
mg,
intx,
inty
,ImageObser
verobser
ver
):i
s
useddr
awt
hespeci
fi
edi
mage.
8.publ
ic abst
ractv
oid dr
awAr
c(i
ntx,i
nty
,intwi
dth,i
nthei
ght
,intst
art
Angl
e,i
nt
ar
cAngl
e):i
suseddr
awaci
rcul
arorel
l
ipt
ical
arc.
9.publ
ic abst
ractv
oidf
il
lAr
c(i
ntx,i
nty
,intwi
dth,i
nthei
ght
,intst
art
Angl
e,i
nt
ar
cAngl
e):i
susedt
ofi
l
laci
rcul
arorel
l
ipt
ical
arc.
10.publ
icabst
ractv
oidset
Col
or(
Col
orc)
:isusedt
osett
hegr
aphi
cscur
rentcol
ort
othe
speci
fi
edcol
or.
11.publ
icabst
ractv
oidset
Font
(Fontf
ont
):i
susedt
osett
hegr
aphi
cscur
rentf
ontt
othe
speci
fi
edf
ont
.

Exampl
eofGr
aphi
csi
nappl
et:
i
mpor
tjav
a.appl
et.
Appl
et;
i
mpor
tjav
a.awt
.*;

publ
iccl
assGr
aphi
csDemoext
endsAppl
et{

publ
icv
oidpai
nt(
Graphi
csg)
{
g.
set
Col
or(
Col
or.
red)
;
g.
drawSt
ri
ng(
"Wel
come"
,50,
50)
;
g.
drawLi
ne(
20,
30,
20,
300)
;
g.
drawRect
(70,
100,
30,
30)
;
g.
fi
ll
Rect
(170,
100,
30,
30)
;
g.
drawOv
al(
70,
200,
30,
30)
;

g.
set
Col
or(
Col
or.
pink)
;
g.
fi
ll
Oval
(170,
200,
30,
30)
;
g.
drawAr
c(90,
150,
30,
30,
30,
270)
;
g.
fi
ll
Arc(
270,
150,
30,
30,
0,
180)
;

}
}

my
appl
et.
html
<ht
ml>
<body
>
<appl
etcode="
Graphi
csDemo.
class"wi
dth="
300"hei
ght
="300"
>
</
appl
et>
</
body
>
</
html
>

Jav
aAWT
JavaAWT( Abst
ractWi
ndowTool
ki
t)isanAPIt
odev
elopGr
aphi
calUserI
nter
face(
GUI
)
orwindows-
basedappl
i
cati
onsi
nJava.

Jav
aAWTcomponent
sar
epl
atf
orm-
dependenti
.e.component
sar
edi
spl
ayed
accor
dingtotheviewofoperat
ingsy
stem.AWTi sheav
ywei
ghti
.e.i
tscomponent
sar
e
usi
ngtheresourcesofunder
lyi
ngoperat
ingsy
stem (
OS).

Thej
ava.
awtpackagepr
ovi
desclassesforAWTAPIsuchasText
Fiel
d,Label
,Text
Area,
Radi
oBut
ton,
CheckBox,
Choice,
Listet
c.

TheAWTwi l
lhel
ptheusert
ounder
standJav
aGUIpr
ogr
ammi
ngi
nsi
mpl
e
andeasyst
eps.

Jav
aAWTHi
erar
chy
Thehi
erar
chyofJav
aAWTcl
assesar
egi
venbel
ow.
Component
s
Allthe el
ementsli
ke t
he button,textf i
elds,scrol
lbars,etc.ar
e cal
led
components.I
nJavaAWT,thereareclassesf oreachcomponentasshownin
abovediagr
am.Inor
dertopl
aceev er
ycomponenti napart
icul
arposi
ti
onona
screen,
weneedtoaddthem toacontainer.

Cont
ainer
TheContai
nerisacomponentinAWTt hatcancontai
nanothercomponents
l
ikebutt
ons,t
extf
iel
ds,l
abelset
c.Theclassesthatext
endsContai
nerclass
areknownascontai
nersuchasFrame,
DialogandPanel
.

Iti
sbasical
lyascr eenwherethewherethecomponentsareplacedatthei
r
speci
fi
clocat
ions.Thusitcont
ainsandcont
rol
sthel
ayoutofcomponents.

Ther
ear
efourt
ypesofcont
ainer
sinJav
aAWT:

1.Wi
ndow
2.Panel
3.Fr
ame
4.Di
alog

Wi
ndow

Thewindowisthecontai
nerthathavenobor
dersandmenubars.Youmustuseframe,
di
alogoranotherwindow forcreati
ngawindow.Weneedt ocreateaninst
anceof
Windowclasst
ocreatethi
scontainer
.

Panel

ThePaneli
sthecontai
nerthatdoesn'tcont
ainti
tl
ebar,bor
derormenubar .I
tisgeneric
contai
nerf
orholdi
ngthecomponent s.I
tcanhav eothercomponentsli
kebutton,text
fi
eld et
c.An inst
ance ofPanelcl ass cr
eates a cont
ainer
,in which we can add
components.
Fr
ame

TheFrameisthecont
ainerthatcont
ainti
tl
ebarandbor derandcanhav
emenubar
s.I
t
canhaveothercomponentslikebut
ton,t
extfi
eld,scr
ollbaret
c.Fr
ameismostwi
del
y
usedcont
ainerwhi
l
edev el
opinganAWTappl i
cat
ion.

Usef
ulMet
hodsofComponentCl
ass

Met
hod Descr
ipt
ion

publ
i
cvoi
dadd(
Componentc) I
nser
tsacomponentont
hiscomponent
.

publ
i
cvoi
dset
Size(
intwi
dth,
i
nthei
ght
) Set
sthesi
ze(
widt
handhei
ght
)oft
hecomponent
.

publ
i
cvoi
dset
Lay
out
(Lay
out
Managerm) Def
inest
hel
ayoutmanagerf
ort
hecomponent
.

publ
i
cvoi
dset
Visi
ble(
bool
eanst
atus) Changest
hev
isi
bil
i
tyoft
hecomponent
,bydef
aul
tfal
se.

Jav
aAWTExampl
e
Tocreat
esi
mpleAWTexampl
e,y
ouneedaf
rame.Ther
ear
etwoway
stocr
eat
eaGUI
usi
ngFramei
nAWT.

1.Byext
endi
ngFr
amecl
ass(
inher
it
ance)
2.Bycr
eat
ingt
heobj
ectofFr
amecl
ass(
associ
ati
on)

AWTEx
ampl
ebyAssoci
ati
on
Let
'sseeasi
mpleexampleofAWTwher ewearecreat
inginst
anceofFr
amecl
ass.
Here,
wear
ecreat
ingaText
Fiel
d,Label
andBut
toncomponentontheFr
ame.

AWTExampl
e2.
jav
a

/
/impor
ti
ngJav
aAWTcl
ass
i
mpor
tjav
a.awt
.*;
/
/cl
assAWTExampl
e2di
rect
lycr
eat
esi
nst
anceofFr
amecl
ass
cl
assAWTExampl
e2
{

/
/ini
ti
ali
zi
ngusi
ngconst
ruct
or
AWTExampl
e2(
)
{

/
/cr
eat
ingaFr
ame
Fr
amef=newFr
ame(
);

/
/cr
eat
ingaLabel
Label
l=newLabel
("
Empl
oyeei
d:"
);

/
/cr
eat
ingaBut
ton
But
tonb=newBut
ton(
"Submi
t"
);

/
/cr
eat
ingaText
Fiel
d
Text
Fiel
dt=newText
Fiel
d()
;

/
/set
ti
ngposi
ti
onofabov
ecomponent
sint
hef
rame
l
.set
Bounds(
20,
80,
80,
30)
;
t
.set
Bounds(
20,
100,
80,
30)
;
b.
set
Bounds(
100,
100,
80,
30)
;

/
/addi
ngcomponent
sint
ofr
ame
f
.add(
b);
f
.add(
l)
;
f
.add(
t);

/
/fr
amesi
ze300wi
dthand300hei
ght
f
.set
Size(
400,
300)
;

/
/set
ti
ngt
het
it
leoff
rame
f
.set
Tit
le(
"Empl
oyeei
nfo"
);

/
/nol
ayout
f
.set
Lay
out
(nul
l)
;

/
/set
ti
ngv
isi
bil
i
tyoff
rame
f
.set
Visi
ble(
true)
;
}

/
/mai
nmet
hod
publ
icst
ati
cvoi
dmai
n(St
ri
ngar
gs[
]){

/
/cr
eat
ingi
nst
anceofFr
amecl
ass
AWTExampl
e2awt
_obj
=newAWTExampl
e2(
);

Out
put
:
Jav
aLay
out
Manager
s

TheLay outManagersareusedt oarrangecomponent si


napar ti
cularmanner
.
TheJav aLayoutManager sfaci
li
tat
esust ocontrolt
heposit
ioningandsizeof
the componentsi n GUIf orms.Lay outManageris an interf
ace thatis
i
mpl ementedbyal ltheclassesoflay outmanagers.Ther
ear ethef ol
l
owing
classesthatr
epresentthelayoutmanager s:

1.j
ava.
awt
.Bor
der
Lay
out
2.j
ava.
awt
.Fl
owLay
out
3.j
ava.
awt
.Gr
idLay
out
4.j
ava.
awt
.Car
dLay
out
5.j
ava.
awt
.Gr
idBagLay
out
6.j
avax.
swi
ng.
BoxLay
out
7.j
avax.
swi
ng.
GroupLay
out
8.j
avax.
swi
ng.
Scr
oll
PaneLay
out
9.j
avax.
swi
ng.
Spr
ingLay
outet
c.

Jav
aBor
der
Lay
out
TheBor derLay
outisusedt oarr
angethecomponent
sinfiveregi
ons:north,
south,east
,west,andcenter
.Eachregi
on(
area)maycont
ainonecomponent
only
.Itisthedefaul
tlay
outofaf r
ameorwindow.TheBor
derLayoutpr
ovides
fi
veconstantsforeachregi
on:

1.publ
icst
ati
cfi
nali
ntNORTH
2.publ
icst
ati
cfi
nali
ntSOUTH
3.publ
icst
ati
cfi
nali
ntEAST
4.publ
icst
ati
cfi
nali
ntWEST
5.publ
icst
ati
cfi
nali
ntCENTER

Const
ruct
orsofBor
der
Lay
outcl
ass:
o Bor
der
Lay
out
():cr
eat
esabor
derl
ayoutbutwi
thnogapsbet
weent
he
component
s.
o Bor
der
Lay
out
(i
nthgap,i
ntv
gap)
:cr
eat
esabor
derl
ayoutwi
tht
hegi
ven
hor
izont
alandv
ert
ical
gapsbet
weent
hecomponent
s.

PRACTI
CALPROGRAM (
LAYOUT)

Example of Bor
der
Lay
out cl
ass:Usi
ng Bor
der
Lay
out
()
const
ruct
or
Fi
leName:Bor
der
.j
ava

i
mpor
tjav
a.awt.
*;
i
mpor
tjav
ax.swi
ng.*
;

publi
cclassBorder
{
JFramef ;
Border(
)
{
f=newJFr ame()
;

//cr
eati
ngbut
tons
JBut
tonb1=newJBut
ton(
"NORTH"
);
;//t
hebut
tonwi
l
lbel
abel
edasNORT
H
JBut
tonb2=newJBut
ton(
"SOUTH"
);
;//t
hebut
tonwi
l
lbel
abel
edasSOUTH

JBut
tonb3=newJBut
ton(
"EAST"
);
;//t
hebut
tonwi
l
lbel
abel
edasEAST
JBut
tonb4=newJBut
ton(
"WEST")
;;/
/thebutt
onwil
lbelabel
edasWEST
JBut
tonb5=newJBut
ton(
"CENTER")
;;/
/thebutt
onwil
lbelabel
edasCENT
ER

f
.add(
b1,
Bor
der
Lay
out
.NORTH)
;//b1wi
l
lbeplacedi
ntheNort
hDir
ecti
on
f
.add(
b2,
Bor
der
Lay
out
.SOUTH)
;//b2wi
llbepl
acedi
ntheSouthDi
rect
ion

f
.add(
b3,
Bor
der
Lay
out
.EAST)
;//b2wil
lbepl
acedi nt
heEastDir
ecti
on
f
.add(
b4,
Bor
der
Lay
out
.WEST);/
/b2wil
lbeplacedintheWestDir
ecti
on
f
.add(
b5,
Bor
der
Lay
out
.CENTER)
;//b2wil
lbeplacedintheCent
er

f
.set
Size(300,300)
;
f
.set
Visibl
e(tr
ue);
}
publi
cst
aticvoi
dmai
n(St
ri
ng[
]ar
gs){
newBorder
();
}
}

Out
put
:
Jav
aAWTPopupMenu
PopupMenu can be dynami
cal
l
ypopped up atspeci
fi
cposi
ti
on wi
thi
na
component
.Iti
nheri
tstheMenuclass

AWTPopupMenucl
assdecl
arat
ion

publ
iccl
assPopupMenuext
endsMenui
mpl
ement
sMenuCont
ainer
,Accessi
bl
e
Jav
aAWTPopupMenuExampl
e
i
mpor
tjav
a.awt
.*;
i
mpor
tjav
a.awt
.ev
ent
.*;
cl
assPopupMenuExampl
e
{
PopupMenuExampl
e()
{
f
inalFr
amef
=newFr
ame(
"PopupMenuExampl
e")
;
f
inalPopupMenupopupmenu=newPopupMenu(
"Edi
t"
);
MenuI
tem cut=newMenuI
tem(
"Cut
");
cut
.set
Act
ionCommand(
"Cut
");
MenuI
tem copy=newMenuI
tem(
"Copy
");
copy
.set
Act
ionCommand(
"Copy
");
MenuI
tem past
e=newMenuI
tem(
"Past
e")
;
past
e.set
Act
ionCommand(
"Past
e")
;
popupmenu.
add(
cut
);
popupmenu.
add(
copy
);
popupmenu.
add(
past
e);
f
.addMouseLi
stener
(newMouseAdapt
er(
){
publ
icv
oidmouseCl
i
cked(
MouseEv
ente){
popupmenu.
show(
f,e.
get
X()
,e.
get
Y()
);
}
}
);
f
.add(
popupmenu)
;
f
.set
Size(
400,
400)
;
f
.set
Lay
out
(nul
l)
;
f
.set
Visi
ble(
true)
;
}
publ
icst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])
{
newPopupMenuExampl
e()
;
}
}

Out
put
:

You might also like