<< Chapter < Page Chapter >> Page >
Listing 4 - The processMouseEvent 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 Frameadd(new BaldButton("A"));
Listing 5 - Beginning of anonymous inner class.
add(new Button("B") {//Begin class definition{//Instance initializer enableEvents(AWTEvent.MOUSE_EVENT_MASK); System.out.println("Anonymous class B name: " + getClass().getName());}//end instance initializer
Listing 6 - Overridden 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
Listing 7 - Register a MouseListener object.
Button buttonC = new Button("C"); buttonC.addMouseListener(new MouseListener(){//begin class definition //Instance initializer{System.out.println( "Anonymous class C name: " +getClass().getName());}
Listing 8 - Implementing the interface.
public void mouseClicked(MouseEvent e){ System.out.println("buttonC clicked");}//end mouseClicked //All interface methods must be definedpublic void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){}public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){}}//end class definition );//end addMouseListener calladd(buttonC);//add button to frame
Listing 9 - Registering a WindowListener on the Frame .
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 addWindowListenersetVisible(true); }//end constructor}//end GUI class

Listing 10 - Complete program listing.

/*File InnerClasses08.java Copyright 2003 R.G.BaldwinThis program is designed to illustrate the use of local classes, and anonymous classes. Itillustrates three different implementations of anonymous classes. It also illustrates the useof instance initializers as an alternative to constructors.Illustrates use of local class to instantiate object to handle mouse clicked event withlow-level event handling. This class uses constructor to enable mouse events on a newextended Button class. Also uses constructor to display the class file name.Illustrates use of anonymous class to instantiate object to handle mouse clicked event withlow-level event handling. This class uses an instance initializer to enable mouse events on anew extended Button class. Also uses instance initializer to display name of class file.Illustrates use of anonymous class, which implements MouseListener interface, toinstantiate object to handle mouse clicked event using source-listener event model. Uses instanceinitializer to display name of class file. Illustrates use of anonymous class, which extendsWindowAdapter class, to instantiate object to handle window events fired by the close button inthe upper-right corner of a Frame object, using source-listener event model. Uses instanceinitializer to display name of class file. This program produces the following class fileswhen compiled: GUI$1$BaldButton.classGUI$1.class GUI$2.classGUI$3.class GUI.classInnerClasses08.class The program produces the following output whenthe program is started, each button is clicked once in succession, and then the close buttonin the upper-right corner of the Frame is clicked:Local class name: GUI$1$BaldButton Anonymous class B name: GUI$1Anonymous class C name: GUI$2 Anonymous window listener class name: GUI$3buttonA clicked buttonB clickedbuttonC clicked Close button clickedTested using JDK 1.4.1 under Win ************************************************/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{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{ BaldButton(String text){//constructorenableEvents(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 {//Instance initializerenableEvents( 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 interfacebuttonC.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 definedpublic void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){}public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){}}//end class definition );//end addMouseListener calladd(buttonC);//add button to frame //Use an anonymous class to register a window// listener on the Frame. This class extends // WindowAdapteraddWindowListener(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//=============================================//

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask