» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
דיון בתכנית לדוגמה שניה
דף ראשי  מתקדמים  Container Events and More on Inner Classes  דיון בתכנית לדוגמה שניה גרסה להדפסה

דיון בתכנית לדוגמה שניה

תכנית זו מעתיקה את הפונקציונליות של התכנית הקודמת המכונה Event32 java. אולם, היא עושה כן תוך שימוש רק במחלקות פנימיות אנונימיות. תכנית זו מסופקת כך שתוכל להשוות את התחביר תוך שימוש  במחלקות פנימיות בלבד עם הגרסה הניתנת להשוואה המכונה Event32  העושה שימוש במחלקות פנימיות בעלות שם עבור מחלקות המאזין.

תכנית זו נבחנה תוך שימוש ב 1.1.3 JDK תחת חלונות 95.

 

רישום תכנית לדוגמה שניה

המחלקות הפנימיות האנונימיות בתכנית זו מודגשות תוך שימוש או באותיות שחורות ומובלטות או באותיות נטויות . שים לב כי תכנית זו ממחישה מחלקות פנימיות אנונימיות  מקוננות. בעיקר, המחלקה הפנימית האנונימית עבור הActionListener (אותיות משופעות) מקוננת בתוך המחלקה הפנימית האנונימית עבור הContainerListener (מודגש באותיות שחורות ובולטות).

 

/*File Event33.java
Copyright 1997, R.G.Baldwin

This program replicates the functionality of the program
named Event32.java.  However, it does so using only
anonymous inner classes.  This program is provided so
that you can compare the syntax using only anonymous
inner classes with a comparable version named Event32 that
uses named inner classes for the listener classes.
        
This program was tested using JDK 1.1.3 under Win95.

**********************************************************/
import java.awt.*;
import java.awt.event.*;

//Note that this controlling class does not implement any
// listener interfaces.  They are effectively implemented
// in the inner classes.
class Event33 extends Frame{
  
  Label displayLabel;//reference to a Label for display
  
  //=====================================================//
  public static void main(String[] args){
    new Event33();//instantiate this object
   }//end main
  //=====================================================//
  
  //This method adds a series of Button objects to this
  // Frame object.  The number of buttons added and the 
  // labels on the buttons are determines from the length 
  // and the substring values of an incoming String object.
  void addButtons(String labels){
    for(int cnt = 0; cnt < labels.length();cnt++){
      this.add(new Button(labels.substring(cnt,cnt+1)));
    }//end for loop
  }//end addButtons()  
  //=====================================================//
  
  public Event33(){//constructor
  
    this.setLayout(new GridLayout(0,4,3,3));
    this.setSize(280,280);
    this.setTitle("Copyright 1997 R.G.Baldwin");

 
    //The following anonymous class definition registers an 
    // anonymous ContainerListener object on this Frame
    // object.  The ContainerListener object in turn 
    // registers an anonymous Action Listener object on
    // each of the buttons that are added to the Frame
    // object.
    this.addContainerListener(//add anonymous Listener
      new ContainerListener(){
        public void componentAdded(ContainerEvent e){
          if(e.getID() == ContainerEvent.COMPONENT_ADDED){
            if(e.getChild() instanceof Button){
              Button button = (Button)e.getChild();
              //The following code registers an anonymous
              // ActionListener object on each button 
              // object that is added to the Frame object.
              button.addActionListener(
                new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                    displayLabel.setText(e.getActionCommand());
                  }//end actionPerformed
                }//end new actionListener
              );//end add anonymous ActionListener
            }//end if statement
          }//end if statement  
        }//end componentAdded()
                                
        //The following empty method is required to satisfy
        // the requirement to define all of the methods
        // that are declared in the ContainerListener
        // interface.        
        public void componentRemoved(ContainerEvent e){}
      }//end new ContainerListener    
    );//end add anonymous ContainerListener

    //---------------------------------------------------//
    //Add a series of buttons to this Frame object with
    // the labels determined by the individual characters
    // in the String object passed as a parameter.
    this.addButtons("789+456-123=0.X/C");

    //Add a display label and make it yellow.
    displayLabel = new Label("00000");
    this.add(displayLabel);
    displayLabel.setBackground(Color.yellow);
    this.setVisible(true);
    
    //---------------------------------------------------//
    //Anonymous inner-class listener to terminate program
    this.addWindowListener(
      new WindowAdapter(){//anonymous class definition
        public void windowClosing(WindowEvent e){
          System.exit(0);//terminate the program
        }//end windowClosing()
      }//end WindowAdapter
    );//end addWindowListener
  }//end constructor 
  //=====================================================//
}//end class Event33   
//=======================================================//

 

 18-10-03 / 01:05  נוצר ע"י רונית רייכמן  בתאריך 
 רישום תכנית לדוגמה ראשונה - הקודםהבא - Program-Generated Events and the AWTEventMulticaster Class 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 7