lecture_20_s2005

lecture_20_s2005 - 1.00 Lecture 20 Anonymous Inner Classes

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1.00 Lecture 20 Anonymous Inner Classes Model-View-Controller Reading for next time: Big Java: review sections 4.3-4.10 Anonymous Inner Classes Java offers a shortcut for creating new classes Used when writing small, simple classes. Helpful for writing event listeners. Because they are very common, this approach makes writing them simpler. To see how anonymous inner classes can be used, look at the SwitchButton class.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
SwitchButtonFrame import javax.swing.*; import java.awt.*; public class SwitchButtonFrame extends JFrame { public SwitchButtonFrame() { SwitchButton b = new SwitchButton("Click me!", Color.BLUE, Color.WHITE); Container c = getContentPane(); c.add(b); pack(); // Pack has JFrame resize to fit components } public static void main(String[] args) { SwitchButtonFrame f = new SwitchButtonFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } SwitchButton import javax.swing.*; import java.awt.event.*; import java.awt.*; public class SwitchButton extends JButton implements ActionListener { public SwitchButton(String text, Color b, Color f) { super(text); setBackground(b); setForeground(f); addActionListener(this); } // Button listens to itself (no panel here to do it) public void actionPerformed(ActionEvent ae) { Color fore = getForeground(); setForeground(getBackground()); setBackground(fore); } }
Background image of page 2
AnonSwitchButton This code is functionally equivalent and can be used with the same SwitchButtonFrame , but it uses an anonymous inner class to serve as the ActionListener . // Same import statements public class AnonSwitchButton extends JButton { public AnonSwitchButton(String t, Color b, Color f) { super(t); setBackground(b); setForeground(f); addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { Color fore = getForeground(); setForeground(getBackground()); setBackground(fore); }); } } SwitchButton • SwitchButton no longer implements ActionListener or possesses an actionPerformed method. The anonymous inner classes takes over that function. It looks as if we are new ing an interface, which would be illegal. Actually we are creating an nameless class that will only have a single instance, the one we create right here for the addActionListener() method. The new constructor call can NOT have arguments.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 10

lecture_20_s2005 - 1.00 Lecture 20 Anonymous Inner Classes

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online