package view; import java.awt.*; // to use Container. import java.awt.event.*; // to use WindowAdpater and WindowEvent. import javax.swing.*; // to use JFrame. /** * A subclass of Frame0 containing two JButtons that have event handler * "commands" to print the button click event when the buttons are clicked upon. * @author D.X. Nguyen */ public class Frame2 extends AFrame { public Frame2(String title) { super(title); } protected void initialize() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); JButton jb1 = new JButton("Button 1"); JButton jb2 = new JButton("Button 2"); cp.add(jb1); cp.add(jb2); pack(); /** * Registers an anonymous event handler for the clicking of button jb1. * When jb1 is clicked upon, this event handler will respond by * executing its actionPerformed(...) method. */ jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jb1Clicked(e); } }); /** * Same as the above. */ jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jb2Clicked(e); } }); } /** * Asks System.out to print the event. */ protected void jb1Clicked(ActionEvent e) { System.out.println("Responding to the clicking of jb1:"); System.out.println(e); } /** * Same as jb1Clicked. */ protected void jb2Clicked(ActionEvent e) { System.out.println("Responding to the clicking of jb2:"); System.out.println(e); } }