//: c13:Button2NewB.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // An application and an applet // import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Button2NewB extends JApplet { JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"); JTextField t = new JTextField(20); public void init() { b1.addActionListener(new B1()); b2.addActionListener(new B2()); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(b1); cp.add(b2); cp.add(t); } class B1 implements ActionListener { public void actionPerformed(ActionEvent e) { t.setText("Button 1"); } } class B2 implements ActionListener { public void actionPerformed(ActionEvent e) { t.setText("Button 2"); } } // To close the application: static class WL extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } // A main() for the application: public static void main(String[] args) { JApplet applet = new Button2NewB(); JFrame frame = new JFrame("Button2NewB"); frame.addWindowListener(new WL()); frame.getContentPane().add(applet); frame.setSize(300,100); applet.init(); applet.start(); frame.setVisible(true); } } ///:~