— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet346/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   342   343   344   345   346   347   348   349   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

multiple 
listeners
So now we need FOUR widgets
color-changing 
button will go here
label will 
go here
drawing panel goes 
in the center
label-changing 
button will 
be here
north
south
west
center
Uh-oh.
Is that even possible? How do 
you get two events when you 
have only one actionPerformed() 
method?
And we need to get 
TWO events
This button changes the text 
on the opposite side
This button changes the color 
of the circle


getting 
gui
you are here
4
373
How do you get action events for t wo different buttons, 
when each button needs to do something different? 
1
option one
Implement two actionPerformed() methods
Flaw: You can’t! 
You can’t implement the same method twice in a Java class. It won’t compile.
And even if you could, how would the event source know which of the two methods to call?
class MyGui implements ActionListener {
// lots of code here and then:
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
public void actionPerformed(ActionEvent event) {
label.setText(“That hurt!”);
}
}
2
option two
Register the same listener with both buttons.
Flaw: this does work, but in most cases it’s not very OO.
One event handler 
doing many different things means that you have a single method doing many different things. 
If you need to change how one source is handled, you have to mess with everybody’s event 
handler. Sometimes it is a good solution, but usually it hurts maintainability and extensibility.
Register the same listener 
with both buttons

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   342   343   344   345   346   347   348   349   ...   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