» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
רשימת התוכנית
דף ראשי  מתקדמים  Component Impersonation  רשימת התוכנית גרסה להדפסה

רשימת התוכנית

 

חלק זה מכיל רשימה שלמה של התוכנית עם הערות נוספות. התייחס לחלקים הקודמים לתיאור הפעולות של התוכנית.

 

/*File Event31.java Copyright 1997, R.G.Baldwin
Reformatted 10/5/97 to fit better on the screen.

This program was designed to be compiled and executed under
JDK 1.1.1.

This program demonstrates the use of the postEvent() method
to post ActionEvents to the SystemEventQueue.  In this 
program, a Label object impersonates a Button object by 
posting counterfeit ActionEvent objects and attributes them
to the Button object.

Unlike a previous sample program, this program does not 
override any of the processXxxxEvent() methods. Rather, 
this program works completely within the Source/Listener 
concept of the Delegation Event Model.

Two Labels and a Button are instantiated and added to a 
Frame object. When the Button object is clicked, an 
ActionEvent is generated and trapped by an ActionListener 
object registered on the Button object. Code in the 
actionPerformed() method of the Listener object toggles
the background color of one of the Label objects back and 
forth between yellow and blue.

So far, everything is pretty standard.  However, a 
MouseListener object is registered on the other Label.  
When that Label object is clicked, code in the 
mouseClicked() method of the MouseListener object generates
a synthetic or counterfeit ActionEvent object and posts it 
to the SystemEventQueue.

The code in the Label's MouseListener object impersonates 
the Button object by placing the identification of the 
Button object into the "source" field of the counterfeit 
ActionEvent object.  

The runtime system delivers the counterfeit ActionEvent 
object to the ActionListener object registered on the 
Button object.  The final result is that clicking on the 
Label object invokes the actionPerformed() method 
registered on the Button object, so clicking on the Label 
object has exactly the same result as clicking on the 
Button object.

The program was tested using JDK 1.1.1 (and later 
JDK 1.1.3) and Win95.  An interesting sidelight is that the
counterfeit button constructed from the Label object is
more responsive than the real button.  In other words, the
counterfeit button can service mouse clicks at a more rapid
rate than the real button.
*/

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

//=========================================================
public class Event31 extends Frame{
  public static void main(String[] args){
    Event31 displayWindow = new Event31();//instantiate obj
  }//end main
  //-------------------------------------------------------

  public Event31(){//constructor
    setTitle("Copyright 1997, R.G.Baldwin");
    setLayout(new FlowLayout());
    Button clickMeButton = new Button("Click Me");
    Label colorMeLabel = new Label("Color Me");
    Label clickMeLabel = new Label("Click Me");  

    add(clickMeButton);//add components to the Frame object
    add(colorMeLabel);
    add(clickMeLabel);

    setSize(250,100);//set frame size    
    setVisible(true);//display the frame

    //Register listener objects
    clickMeLabel.addMouseListener(
                       new MyMouseListener(clickMeButton));
    clickMeButton.addActionListener(
                       new MyActionListener(colorMeLabel));
    //terminate when Frame is closed                       
    this.addWindowListener(new Terminate());
   }//end constructor
}//end class Event31
//=========================================================

/*This MouseListener class is used to monitor for mouse 
clicks on a Label object.  Whenever the user clicks on the 
label, the code in an object of this class creates a 
counterfeit ActionEvent object and posts it to the 
SystemEventQueue.  The source of the event is specified to 
be a particular Button object that is passed in when an 
object of this class is instantiated.  Thus, the Label 
object "claims" to be the Button object and posts 
ActionEvent objects that are interpreted by the runtime 
system as originating at the Button object.  The type of
ActionEvents generated are ACTION_PERFORMED events.  The 
events are automatically delivered to the actionPerformed()
method of an ActionListener object registered on the 
button. */

class MyMouseListener extends MouseAdapter{
  Button clickMeButton;//reference to the Button
  //-------------------------------------------------------
  MyMouseListener(Button inButton){//constructor
    clickMeButton = inButton;//save reference to Button
  }//end constructor
  //-------------------------------------------------------
  //overridden mouseClicked() method
  public void mouseClicked(MouseEvent e){
    //Note that the following is a single statement
    Toolkit.getDefaultToolkit().
        getSystemEventQueue().
        postEvent(new ActionEvent(clickMeButton,
                                  ActionEvent.
                                  ACTION_PERFORMED,
                                  "counterfeit"));
  }//end overridden mouseClicked() method
}//end MyMouseListener

//=========================================================
/*This ActionListener class is used to instantiate a 
Listener object for the Button object.  Whenever the button
is clicked, or a counterfeit ActionEvent is posted with the
Button as the specified source object, the code in an 
object of this class toggles the background color of a 
Label object back and forth between yellow and blue.*/

class MyActionListener implements ActionListener{
  int toggle = 0;
  Label myLabel;
  //-------------------------------------------------------
  MyActionListener(Label inLabel){//constructor
    myLabel = inLabel;
  }//end constructor  
  //-------------------------------------------------------
  public void actionPerformed(ActionEvent e){
    if(toggle == 0){
      toggle = 1;
      myLabel.setBackground(Color.yellow);
    }else{
      toggle = 0;
      myLabel.setBackground(Color.blue);
    }//end else
  }//end actionPerformed()
}//end class myActionListener
//=========================================================

class Terminate extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    //terminate the program when the window is closed  
    System.exit(0);
  }//end windowClosing
}//end class Terminate
//=========================================================
.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 31-10-03 / 15:43  עודכן ,  31-10-03 / 15:39  נוצר ע"י רונית רייכמן  בתאריך 
 קטעי קוד מעניינים - הקודםהבא - חזרה 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 3