— Scott McNealy, Sun Microsystems Chairman, President and ceo


Define the event-handling method (implement  the actionPerformed() method from the  ActionListener interrface) 3



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

2
Define the event-handling method (implement 
the actionPerformed() method from the 
ActionListener interrface)
3
getting 
events
Getting a button’s ActionEvent


getting 
gui
you are here
4
361
As an event source, my job is to 
accept 
registrations (from listeners), 
get
events from the user. and
call 
the listener’s event-handling 
method (when the user clicks me)
For most of your stellar Java career, you will not be the source 
of events. 
(No matter how much you fancy yourself the center of your social 
universe.) 
Get used to it. Your job is to be a good listener. 
(Which, if you do it sincerely, can improve your social life.)
Listeners, Sources, and Events
As a listener, my job is to 
implement 
the interface,
register 
with the button, and 
provide 
the event-handling.
Listener GETS the 
event
Source SENDS 
the event
Event object 
HOLDS DATA 
about the event
E
ven
t
obje
c
t
Hey, what about me? I’m a player too, you 
know! As an event object, I’m the 
argument 
to the event call-back method (from the 
interface) and my job is to 
carry data
about 
the event back to the listener.


362
 
chapter 12
there are no
Dumb Questions
Q: 
Why can’t I be a source of events?
A: 
You CAN. We just said that most of the time 
you’ll be the receiver and not the originator of the 
event (at least in the early days of your brilliant Java 
career). Most of the events you might care about 
are ‘fired’ by classes in the Java API, and all you have 
to do is be a listener for them. You might, however, 
design a program where you need a custom event, say, 
StockMarketEvent thrown when your stock market 
watcher app finds something it deems important. In 
that case, you’d make the StockWatcher object be an 
event source, and you’d do the same things a button
(or any other source) does—make a listener interface 
for your custom event, provide a registration method 
(addStockListener()), and when somebody calls it, add 
the caller (a listener) to the list of listeners. Then, when 
a stock event happens, instantiate a StockEvent object 
(another class you’ll write) and send it to the listeners 
in your list by calling their stockChanged(StockEvent 
ev) method. And don’t forget that for every event type 
there must be a matching listener interface (so you’ll 
create a StockListener interface with a stockChanged()
method).
Q: 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   331   332   333   334   335   336   337   338   ...   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