package Anon; import java.awt.*; import java.awt.event.*; public class InnerClasses08 { public static void main(String[] args){ new GUI(); }//end main }//end class InnerClasses08 //=============================================// class GUI extends Frame{ /** * */ private static final long serialVersionUID = 3990842979081966365L; public GUI(){//constructor setLayout(new FlowLayout()); setSize(250,75); setTitle("Copyright 2003 R.G.Baldwin"); //Local class w/mouse events enabled. The new // class extends Button, and uses low-level // event handling to handle mouse clicked // events on the button. class BaldButton extends Button{ /** * */ private static final long serialVersionUID = -2469128161023919728L; BaldButton(String text){//constructor enableEvents(AWTEvent.MOUSE_EVENT_MASK); setLabel(text); //Display the name of the class file System.out.println("Local class name: " + getClass().getName()); }//end constructor //This is the event handling method. public void processMouseEvent( MouseEvent e){ if (e.getID() == MouseEvent.MOUSE_CLICKED){ System.out.println("buttonA clicked"); }//end if //The following is required of overridden // processMouseEvent method. super.processMouseEvent(e); }//end processMouseEvent }//end class BaldButton //Add button to Frame add(new BaldButton("A")); //This code defines an anonymous Inner Class // w/mouse events enabled. The new class // extends Button. This class uses low-level // event handling to handle mouse clicked // events on the button. This is an // anonymous alternative to the local class // defined above. add(new Button("B") {//Begin class definition /** * */ private static final long serialVersionUID = -9126989704966253101L; {//Instance initializer enableEvents( AWTEvent.MOUSE_EVENT_MASK); System.out.println( "Anonymous class B name: " + getClass().getName()); }//end instance initializer //Override the inherited // processMouseEvent method. public void processMouseEvent( MouseEvent e){ if (e.getID() == MouseEvent.MOUSE_CLICKED){ System.out.println( "buttonB clicked"); }//end if //Required of overridden // processMouseEvent method. super.processMouseEvent(e); }//end processMouseEvent }//end class definition );//end add method call Button buttonC = new Button("C"); //Anonymous inner class that implements // MouseListener interface buttonC.addMouseListener(new MouseListener() {//begin class definition //Instance initializer {System.out.println( "Anonymous class C name: " + getClass().getName());} public void mouseClicked(MouseEvent e){ System.out.println("buttonC clicked"); }//end mouseClicked //All interface methods must be defined public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} }//end class definition );//end addMouseListener call add(buttonC);//add button to frame //Use an anonymous class to register a window // listener on the Frame. This class extends // WindowAdapter addWindowListener(new WindowAdapter() {//begin class definition //Instance initializer {System.out.println( "Anonymous window listener class " + "name: " + getClass().getName());} public void windowClosing(WindowEvent e){ System.out.println( "Close button clicked"); System.exit(0); }//end windowClosing }//end class definition );//end addWindowListener setVisible(true); }//end constructor }//end GUI class //=============================================//