001package horstmann.ch04_action2;
002import java.awt.FlowLayout;
003import java.awt.event.ActionListener;
004
005import javax.swing.JButton;
006import javax.swing.JFrame;
007import javax.swing.JTextField;
008
009public class ActionTester
010{
011        public static void main(String[] args)
012        {
013                JFrame frame = new JFrame();
014
015                final int FIELD_WIDTH = 20;
016                textField = new JTextField(FIELD_WIDTH);
017                textField.setText("Click a button!");
018
019                JButton helloButton = new JButton("Say Hello");
020
021                helloButton.addActionListener(
022                                createGreetingButtonListener("Hello, World!"));
023
024                JButton goodbyeButton = new JButton("Say Goodbye");
025
026                goodbyeButton.addActionListener(
027                                createGreetingButtonListener("Goodbye, World!"));
028
029                frame.setLayout(new FlowLayout());
030
031                frame.add(helloButton);
032                frame.add(goodbyeButton);
033                frame.add(textField);
034
035                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
036                frame.pack();
037                frame.setVisible(true);
038        }
039
040        public static ActionListener createGreetingButtonListener(
041                        final String message)
042        {
043                return event -> textField.setText(message);
044        }
045
046        private static JTextField textField;
047}