רישום התוכנית
חלק זה מכיל רישום מלא של התוכנית. יש להתייחס לחלקים הקודמים לתיאור הפעולות של התוכנית.
/*File Event26.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 This is a bare-bones program that illustrates the ability to create, trap, and process events of new custom types. The output from the program for one particular run was: Copyright 1997, R.G.Baldwin customEventTrapped() method invoked on First NonVisualObject Source of event was NonVisual[,0,0,0x0,invalid] customEventTrapped() method invoked on Second NonVisualObject Source of event was NonVisual[,0,0,0x0,invalid] The program was tested using JDK 1.1 running under Win95. */ //========================================================== import java.awt.*; import java.awt.event.*; import java.util.EventListener; import java.util.EventObject; //========================================================== public class Event26 { public static void main(String[] args){ new Event26();//instantiate an object of this type }//end main //---------------------------------------------------------- public Event26(){//constructor System.out.println("Copyright 1997, R.G.Baldwin"); NonVisual firstNonVisualObject = new NonVisual( "First NonVisualObject"); firstNonVisualObject.addCustomEventListener( new CustomEventListenerClass()); //create an event firstNonVisualObject.generateCustomEvent(); NonVisual secondNonVisualObject = new NonVisual( "Second NonVisualObject"); secondNonVisualObject.addCustomEventListener( new CustomEventListenerClass()); //The following statement causes the program to terminate // with the message // "No support for multiple Listener objects" because it // violates the restriction that this bare-bones program // only allows one Listener object to be registered on // each NonVisual object. Therefore, the statement // has been disabled by making it a comment. It is // included here for illustration purposes only. //secondNonVisualObject.addCustomEventListener( new CustomEventListenerClass()); //create an event secondNonVisualObject.generateCustomEvent(); }//end constructor }//end class Event26 //========================================================== //Class to define a new type of event class CustomEvent extends EventObject{ String id;//instance variable for an object of this type //-------------------------------------------------------- CustomEvent(Object obj, String id){//constructor super(obj);//pass the Object parameter to the superclass this.id = id;//save the String parameter }//end constructor //-------------------------------------------------------- //method to return the saved String parameter String getCustomID(){ return id; }//end getCustomID }//end class CustomEvent //========================================================== //Define interface for the new type of event listener interface CustomEventListener extends EventListener{ void customEventTrapped(CustomEvent e); }// //========================================================== //Listener class to respond to custom events class CustomEventListenerClass implements CustomEventListener{ public void customEventTrapped(CustomEvent e){ System.out.println( "customEventTrapped() method invoked on " + e.getCustomID()); System.out.println("Source of event was " + e.getSource()); }//end customEventTrapped }//end class CustomEventListenerClass //========================================================== //Class to create object capable of generating Custom events. // Note: This is a bare-bones version which can only support // a single Listener object for the Custom event type. class NonVisual extends Component { String ID; // The ID of this object //Reference to single Listener object CustomEventListenerClass customListener; //-------------------------------------------------------- public NonVisual(String ID) {//Construct a NonVisual object this.ID = ID; }//end constructor //-------------------------------------------------------- public void addCustomEventListener( CustomEventListenerClass listener){ //Do not attempt to add more than one Listener object. if(customListener == null) customListener = listener;//one listener only else{ System.out.println( "No support for multiple Listener objects"); //terminate on attempt to register multiple Listeners System.exit(0); } }//end addCustomEventListener() //-------------------------------------------------------- public void generateCustomEvent() { customListener.customEventTrapped( new CustomEvent(this, ID)); }//end generateCustomEvent }//end class NonVisual //========================================================= |