//: c13:GoodIdea.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // The best way to design classes using the // Java event model: use an inner class for // each different event. This maximizes // flexibility and modularity. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class GoodIdea extends JFrame { JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"); public GoodIdea() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); b1.addActionListener(new B1L()); b2.addActionListener(new B2L()); cp.add(b1); cp.add(b2); } public class B1L implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Button 1 pressed"); } } public class B2L implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Button 2 pressed"); } } public static void main(String[] args) { JFrame frame = new GoodIdea(); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.out.println("Window Closing"); System.exit(0); } }); frame.setSize(300,200); frame.setVisible(true); } } ///:~