— Scott McNealy, Sun Microsystems Chairman, President and ceo


If you care about the button’s events



Download 40,57 Mb.
Pdf ko'rish
bet333/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   329   330   331   332   333   334   335   336   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

If you care about the button’s events, 
implement an interface
 that says,
“I’m 
listening
 
for your events.” 
When you implement a 
listener interface, you give 
the button a way to call 
you back. The interface is 
where the call-back method 
is declared.


getting 
gui
you are here
4
359
“Button, please add me to 
your list of listeners and call 
my actionPerformed() method 
when the user clicks you.”
How the listener and source 
communicate:
The Listener
The Event Source
If your class wants to know about 
a button’s ActionEvents, you 
implement the ActionListener 
interface. The button needs to 
know you’re interested, so you 
register with the button by calling its 
addActionListener(this) and passing an 
ActionListener reference to it (in this case, 
you are the ActionListener so you pass 
this).The button needs a way to call you 
back when the event happens, so it calls 
the method in the listener interface. As an 
ActionListener, you must implement the 
interface’s sole method, actionPerformed().
The compiler guarantees it.
A button is a source of ActionEvents, 
so it has to know which objects are 
interested listeners. The button has an 
addActionListener() method to give 
interested objects (listeners) a way to 
tell the button they’re interested. 
When the button’s 
addActionListener() runs (because
a potential listener invoked it), the 
button takes the parameter (a 
reference to the listener object) and 
stores it in a list. When the user clicks 
the button, the button ‘fires’ the event 
by calling the actionPerformed() 
method on each listener in the list.
“OK, you’re an ActionListener, 
so I know how to call you back 
when there’s an event -- I’ll call 
the actionPerformed() method 
that I 
know you have.”
actionPerformed(theEven
t)
bu
tto
n.ad
dActionListener(this)



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   329   330   331   332   333   334   335   336   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish