// TestActionEvent.java: Test ActionEvent package chapter10; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestActionEvent extends JFrame implements ActionListener { // Create two buttons private JButton jbtOk = new JButton("OK"); private JButton jbtCancel = new JButton("Cancel"); /**Default constructor*/ public TestActionEvent() { // Set the window title setTitle("TestActionEvent"); // Set FlowLayout manager to arrange the components // inside the frame getContentPane().setLayout(new FlowLayout()); // Add buttons to the frame getContentPane().add(jbtOk); getContentPane().add(jbtCancel); // Register listeners jbtOk.addActionListener(this); jbtCancel.addActionListener(this); } /**Main method*/ public static void main(String[] args) { TestActionEvent frame = new TestActionEvent(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(100, 80); frame.setVisible(true); } /**This method will be invoked when a button is clicked*/ public void actionPerformed(ActionEvent e) { if (e.getSource() == jbtOk) { System.out.println("The OK button is clicked"); } else if (e.getSource() == jbtCancel) { System.out.println("The Cancel button is clicked"); } } }