//: c13:BadIdea2.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // An improvement over BadIdea1.java, since it // uses the WindowAdapter as an inner class // instead of implementing all the methods of // WindowListener, but still misses the // valuable modularity of inner classes import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class BadIdea2 extends JFrame implements ActionListener { JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"); public BadIdea2() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); addWindowListener(new WL()); b1.addActionListener(this); b2.addActionListener(this); cp.add(b1); cp.add(b2); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(source == b1) System.out.println("Button 1 pressed"); else if(source == b2) System.out.println("Button 2 pressed"); else System.out.println("Something else"); } class WL extends WindowAdapter { public void windowClosing(WindowEvent e) { System.out.println("Window Closing"); System.exit(0); } } public static void main(String[] args) { JFrame frame = new BadIdea2(); frame.setSize(300,200); frame.setVisible(true); } } ///:~