import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class MyFrame2 extends JFrame { // inherit JFrame private JButton button1 = new JButton("Button1"); // declare and create a button private JButton button2 = new JButton("Button2"); // declare and create a button private JButton button3 = new JButton("Button3"); // declare and create a button /** * Constructor sets up the window and creates a toolbar */ public MyFrame2() { // // setup the frames banner/title bar // super("My Frame"); // call JFrame's constructor and give it a title setSize(300,200); // set the width of the window to 300 pixels, height to 200. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // Add the buttons to the frame's content pane, for each button, create a // separate inner class object as a listener and tell the button about it. // getContentPane().add("West",button1); // add button 1 button1.addActionListener(new Button1Listener()); // create its listener and tell it getContentPane().add("Center",button2); button2.addActionListener(new Button2Listener()); getContentPane().add("East",button3); button3.addActionListener(new Button3Listener()); } /** * Inner class to handle button 1 */ class Button1Listener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Button1 was pressed"); } } /** * Inner class to handle button 2 */ class Button2Listener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Button2 was pressed"); } } /** * Inner class to handle button 3 */ class Button3Listener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.out.println("Button3 was pressed"); } } /*********************************************************************** * * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ MyFrame2 fr = new MyFrame2(); // create the frame fr.setVisible(true); // allow it to be seen } }