» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
חזרה
דף ראשי  מתקדמים  Creating, Trapping, and Processing Custom Event Types  חזרה גרסה להדפסה

חזרה

 

ש- מבלי להסתכל בפתרון המפורט  כתוב אפליקציית Java, המשתמשת בהגדרות שבמחלקה SampProg130, ונותנת את הפלט הבא

 

CustomEvent[source=ClassA[,0,0,0x0,invalid]]
CustomEvent[source=ClassA[,0,0,0x0,invalid]]
on ClassA Object
CustomEvent[source=ClassB[,0,0,0x0,invalid]]
on ClassB Object

 

ת-

 

/*File SampProg130.java from lesson 100
Copyright 1997, R.G.Baldwin

Without viewing the solution that follows, write a Java
application that uses the definition given for the class
named SampProg130 and produces the output shown below.

Copyright 1997, R.G.Baldwin
CustomEvent[source=ClassA[,0,0,0x0,invalid]]
on ClassA Object
CustomEvent[source=ClassB[,0,0,0x0,invalid]]
on ClassB Object  

The program was tested using JDK 1.1.3 running under Win95.
*/
//=========================================================
import java.awt.*;
import java.awt.event.*;
import java.util.*;

//=========================================================

public class SampProg130 {
  public static void main(String[] args){
    new SampProg130();//instantiate an object of this type
  }//end main
//---------------------------------------------------------
  public SampProg130(){//constructor
    System.out.println("Copyright 1997, R.G.Baldwin");
    
    ClassA classAObject = new ClassA("ClassA Object");
    classAObject.addCustomEventListener(
                           new CustomEventListenerClass());
    classAObject.generateCustomEvent();//create an event

    ClassB classBObject = new ClassB("ClassB Object");
    classBObject.addCustomEventListener(
                           new CustomEventListenerClass());
    classBObject.generateCustomEvent();//create an event
    
  }//end constructor
}//end class SampProg130
//=========================================================

//One 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 ClassA extends Component {
  String ID; // The ID of this object
  //Reference to single Listener object
  CustomEventListenerClass customListener;
  //-------------------------------------------------------
  public ClassA(String ID) {//Constructs a ClassA 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;
    else{
      System.out.println(
               "No support for multiple Listener objects");
      //terminate on attempt to register multiple Listeners
      System.exit(0);
    }//end else
  }//end addCustomEventListener()
  //-------------------------------------------------------
  public void generateCustomEvent() {
    customListener.customEventTrapped(
        new CustomEvent(this, ID));
  }//end generateCustomEvent
}//end class ClassA
//=========================================================


//Another 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 ClassB extends Component {
  String ID; // The ID of this object
  //Reference to single Listener object
  CustomEventListenerClass customListener;
  //-------------------------------------------------------
  public ClassB(String ID) {//Constructs a ClassB 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;
    else{
      System.out.println(
               "No support for multiple Listener objects");
      //terminate on attempt to register multiple Listeners
      System.exit(0);
    }//end else
  }//end addCustomEventListener()
  //-------------------------------------------------------
  public void generateCustomEvent() {
    customListener.customEventTrapped(
        new CustomEvent(this, ID));
  }//end generateCustomEvent
}//end class ClassB
//=========================================================

//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
    //pass the Object parameter to the superclass
    super(obj);
    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("" + e.toString());      
    System.out.println("on " + e.getCustomID());
  }//end customEventTrapped
}//end class CustomEventListenerClass
//=========================================================
 31-10-03 / 14:27  נוצר ע"י רונית רייכמן  בתאריך 
 רישום התוכנית - הקודםהבא - Paint Events 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 5