//: c13:ButtonApp.java // From Thinking in Java, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 1999 // Copyright notice in Copyright.txt // Creating an application import javax.swing.*; import java.awt.event.*; import java.awt.*; public class ButtonAppEvents extends JFrame { JButton b1, b2; JTextField t; MyActionListener al; public ButtonAppEvents(String name) { super(name); t = new JTextField(15); b1 = new JButton("Hello"); b2 = new JButton("Howdy"); al = new MyActionListener(t); b1.addActionListener(al); b2.addActionListener(al); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(b1); cp.add(b2); cp.add(t); } public static void main(String[] args) { JFrame frame = new ButtonAppEvents("ButtonApp"); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); frame.setSize(400,100); frame.setVisible(true); } } class MyActionListener implements ActionListener { JTextField _t; public MyActionListener (JTextField t) { _t = t; } public void actionPerformed(ActionEvent e){ String name = ((JButton)e.getSource()).getText(); _t.setText(name); } }