//: c13:BadIdea1.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // Some literature recommends this approach, but // it's missing the point of the Java event model import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class BadIdea1 extends JFrame implements ActionListener, WindowListener { JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"); public BadIdea1() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); addWindowListener(this); 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"); } public void windowClosing(WindowEvent e) { System.out.println("Window Closing"); System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public static void main(String[] args) { JFrame frame = new BadIdea1(); frame.setSize(300,200); frame.setVisible(true); } } ///:~