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

 

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

  

/*File Event12.java Copyright 1997, R.G.Baldwin
Revised 03/09/98 to fit the page better.

This program is designed to be compiled and run 
under JDK 1.1

The program supports experimentation with low-level events
and semantic events.

A Button object and a TextField object are placed in a 
Frame object.

An ActionListener object is instantiated and registered to
monitor for semantic actionPerformed() events on the Button
and the TextField.  

An actionPerformed() event can be generated on a TextField
by pressing the Enter key while the TextField object has 
the focus.  

An actionPerformed() event can be generated by a Button by
clicking on it with the mouse.

An action event cannot be generated by a Frame object.

Whenever an actionPerformed() event occurs, the Listener
object invokes the getActionCommand() method on the object
to obtain the "command name".

The getActionCommand() method returns the "command name" 
associated with the action as a String.  The string is 
displayed.  As it turns out, the "command name" associated
with a Button is simply the text, caption, or label on the
button.  The "command name" associated with a TextField is
the current text content of the TextField object.

The ActionEvent object passed to the actionPerformed() 
method includes the name of the component which can be 
used in a conditional test based on the indexOf() method 
of the String class to identify the component that 
generated the event.

Each time the actionPerformed() method is invoked, code in
the body of the method uses the indexOf() method to 
identify the component that generated the event and 
displays a message identifying that component.

A FocusListener object is instantiated and registered to 
monitor for low-level focusGained() and focusLost() events
on the Button and the TextField.

Whenever a focusGained() event occurs, a message is 
displayed identifying the object which gained the focus.  
Likewise, whenever a focusLost() event occurs, a message is
displayed identifying the object which lost the focus.  The
object that gained or lost focus is identified by 
performing conditional tests on the FocusEvent object 
passed in as a parameter.

A MouseListener object is instantiated and registered to 
monitor for low-level mousePressed() events on all three 
objects.  The Listener object differentiates among the 
three on the basis of the component name assigned to each 
object.  The approach used to obtain the component name in
this program uses the indexOf() method of the String class
on the MouseEvent object.  This is a somewhat less complex
approach than the approach used to obtain the component 
name for a mousePressed() event in an earlier lesson.  When
a mousePressed() event occurs on any of the three visual 
objects, the Listener object displays a message identifying
the object that generated the event.

Finally, a WindowListener object is instantiated and 
registered to terminate the program when the user closes 
the Frame object.

Typical outputs from the program follow:


Clicking the mouse inside the frame but outside of both the
TextField and the Button produces the following output:

Got mousePressed event from Frame object


Clicking the mouse on the TextField when the Button has the
focus produces the following output:

Got mousePressed event from TextField1 object
Got focusLost event from Button1 object
Got focusGained event from TextField1 object


Pressing the Enter key when the TextField has the focus 
produces the following output:

e.getActionCommand() = Initial String
Got actionPerformed event from TextField1 object


Clicking the mouse on the Button when the TextField has the
focus produces the following output:

Got mousePressed event from Button1 object
Got focusLost event from TextField1 object
Got focusGained event from Button1 object
e.getActionCommand() = Click me
Got actionPerformed event from Button1 object


These results were produced using JDK 1.1.3 under Win95.
**********************************************************/

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

public class Event12 {
  public static void main(String[] args){
    //instantiate a Graphical User Interface object
    GUI gui = new GUI();
  }//end main
}//end class Event12
//=======================================================//

//The following class is used to instantiate a graphical 
// user interface object.
class GUI {
  public GUI(){//constructor
    //Create a visual TextField object 
    TextField myTextField = 
                           new TextField("Initial String");
    myTextField.setName("TextField1");

    //Create a visual Button object
    Button myButton = new Button("Click me");
    myButton.setName("Button1");
  
    //Create a visual Frame object and name it Frame
    Frame myFrame = new Frame();
    myFrame.setSize(200,300);
    myFrame.setTitle("Copyright 1997, R.G.Baldwin");
    myFrame.setName("Frame");
    
    //Add the Button and the TextField to the Frame object
    myFrame.add("North",myButton);
    myFrame.add("South",myTextField);
    myFrame.setVisible(true);

    //Instantiate and register an ActionListener object
    // which will monitor for action events on the
    // TextField and the Button.    
    ActionProc actionProcCmd = new ActionProc();
    myTextField.addActionListener(actionProcCmd);
    myButton.addActionListener(actionProcCmd);

    //Instantiate and register a FocusListener object which
    // will monitor for focus events on the TextField and
    // the Button.
    FocusProc focusProcCmd = new FocusProc();
    myTextField.addFocusListener(focusProcCmd);
    myButton.addFocusListener(focusProcCmd);  
    
    //Instantiate and register a MouseListener object which
    // will process mouse events on the Frame object, the
    // Button object, or the TextField object.
    MouseProc mouseProcCmd = new MouseProc();
    myFrame.addMouseListener(mouseProcCmd);
    myTextField.addMouseListener(mouseProcCmd);
    myButton.addMouseListener(mouseProcCmd);

    //Instantiate and register a Listener object which will
    // terminate the program when the user closes the Frame
    // object
    WProc1 winProcCmd1 = new WProc1();
    myFrame.addWindowListener(winProcCmd1);
  }//end constructor
}//end class GUI definition
//=======================================================//

//Semantic event monitor.
// This ActionListener class is used to instantiate a
// Listener object that monitors for action events on the
// TextField and the Button.  Whenever an actionPerformed()
// event occurs, it displays the ActionCommand and the
// identification of the component that generated the
// event. The listener object distinguishes between the
// components on the basis of their component names which
// are embedded in the object passed in as a parameter
// when an event occurs.

class ActionProc implements ActionListener{
  public void actionPerformed(ActionEvent e){
    System.out.println("e.getActionCommand() = " + 
                                     e.getActionCommand());
    
    if( e.toString().indexOf("on TextField1") != -1 ){
      System.out.println(
       "Got actionPerformed event from TextField1 object");
    }//end if
    
    if( e.toString().indexOf("on Button1") != -1 ){
      System.out.println(
          "Got actionPerformed event from Button1 object");
    }//end if
  }//end actionPerformed()
}//end class ActionProc
//=======================================================//

//Semantic event monitor.
// This FocusListener class is used to instantiate a
// Listener object that monitors for focus events on the
// TextField and the Button.  Whenever a focusLost() or 
// focusGained() event occurs, it displays the 
// identification of the component that generated the
// event.  The listener object distinguishes between the
// components on the basis of their component names which
// are embedded in the object passed in as a parameter when
// an event occurs.

class FocusProc implements FocusListener{
  public void focusGained(FocusEvent e){
    if( e.toString().indexOf("on TextField1") != -1 ){
      System.out.println(
           "Got focusGained event from TextField1 object");
    }//end if
    
    if( e.toString().indexOf("on Button1") != -1 ){
      System.out.println(
              "Got focusGained event from Button1 object");
    }//end if    
  }//end focusGained()

  public void focusLost(FocusEvent e){
    if( e.toString().indexOf("on TextField1") != -1 ){
      System.out.println(
             "Got focusLost event from TextField1 object");
    }//end if
    if( e.toString().indexOf("on Button1") != -1 ){
      System.out.println(
                "Got focusLost event from Button1 object");
    }//end if
  }//end focusLost()
}//end class FocusProc
//=======================================================//

//Low-level event monitor.
// This listener class monitors for mouse presses and 
// displays a message when a mousePressed() event occurs on
// the Frame object, the Button object, or the TextField
// object.  The message identifies the component that 
// generated the event. The listener object distinguishes
// between the components on the basis of their component
// names which are embedded in the object passed in as a
// parameter when an event occurs.

class MouseProc extends MouseAdapter{
  public void mousePressed(MouseEvent e){
    if( e.toString().indexOf("on Frame") != -1 ){
      System.out.println(
               "Got mousePressed event from Frame object");
    }//end if

    if( e.toString().indexOf("on TextField1") != -1 ){    
      System.out.println(
          "Got mousePressed event from TextField1 object");
    }//end if

    if( e.toString().indexOf("on Button1") != -1 ){    
      System.out.println(
             "Got mousePressed event from Button1 object");
    }//end if
  }//end mousePressed()
}//end class MouseProc
//=======================================================//

//The following listener is used to display a message and
// terminate the program when the user closes the Frame
// object.
class WProc1 extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.exit(0);
  }//end windowClosing()
}//end class WProc1
//=======================================================//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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