— Scott McNealy, Sun Microsystems Chairman, President and ceo


class MyGui implements ActionListener {



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

class MyGui implements ActionListener {
// declare a bunch of instance variables here
public void go() {
// build gui
colorButton = new JButton();
labelButton = new JButton();
colorButton.addActionListener(this);
labelButton.addActionListener(this);
// more gui code here ...
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == colorButton) {
frame.repaint();
} else {
label.setText(“That hurt!”);
}
}
}
Query the event object 
to find out which button 
actually fired it, and use 
that to decide what to do.
But this is impossible!


374
 
chapter 12
3
option three
Create two separate ActionListener classes
Flaw: these classes won’t have access to the variables they need 
to act on, ‘frame’ and ‘label’.
You could fix it, but you’d have to give each of the 
listener classes a reference to the main GUI class, so that inside the actionPerformed() 
methods the listener could use the GUI class reference to access the variables of the GUI 
class. But that’s breaking encapsulation, so we’d probably need to make getter methods 
for the gui widgets (getFrame(), getLabel(), etc.). And you’d probably need to add a 
constructor to the listener class so that you can pass the GUI reference to the listener at 
the time the listener is instantiated. And, well, it gets messier and more complicated.
There has got to be a better way!
class MyGui {
JFrame frame;
JLabel label;
void gui() {
// code to instantiate the two listeners and register one

Download 40,57 Mb.

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