— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet352/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   348   349   350   351   352   353   354   355   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

getting 
gui
you are here
4
379
public class TwoButtons {
JFrame frame;
JLabel label;
public static void main (String[] args) {
TwoButtons gui = new TwoButtons ();
gui.go();
}
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton = new JButton(“Change Label”);
labelButton.addActionListener(new LabelListener());
JButton colorButton = new JButton(“Change Circle”);
colorButton.addActionListener(new ColorListener());
label = new JLabel(“I’m a label”);
MyDrawPanel drawPanel = new MyDrawPanel();

frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, labelButton);
frame.getContentPane().add(BorderLayout.WEST, label);
frame.setSize(300,300);
frame.setVisible(true);
}

class LabelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
label.setText(“Ouch!”);
}
} // close inner class
class ColorListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
} // close inner class

}
the main GUI class doesn’t 
implement ActionListener 
now
instead of passing (this) to the 
button’s listener registration 
method, pass a new instance of 
the appropriate listener class.
Now we get to have 
TWO ActionListeners 
in a single class!
the inner class gets to use the ‘frame’ 
instance variable, without having an 
explicit reference to the outer class 
object.
inner class knows 
about ‘label’
Now we can get the two-button 
code working
TwoButtons 
object
ColorListener 
object
LabelListener 
object
outer
inner
inner



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   348   349   350   351   352   353   354   355   ...   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