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

רישום תוכנית

 

להלן רישום תוכנית. אינפורמציה נוספת אודות פעולת התוכנית ניתנת בהערות.

 

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

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

Illustrates how visual components such as Label objects can
be forced to gain the focus, and can then respond to 
keyboard events while they have the focus.  (Note that, by
convention, Label objects don't typically gain the focus 
and don't usually respond to the keyboard in typical 
Windows programs.)

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

A FocusListener object is instantiated and registered to 
listen for focusGained() and focusLost() events on the 
Frame and the Label.  When these events occur, the listener
object makes a color change on the Frame or Label to 
provide a visual indication of focus gained or lost.  

When the Label has the focus, its text is red.  Otherwise,
its text is black.  When the Frame has the focus, its 
background color is blue. Otherwise, it is white.

Note that the Button and the TextField components 
automatically provide a visual indication of focus without
intervention by the programmer.

A MouseListener object is instantiated and registered to 
listen for low-level mousePressed() events on the Frame and
the Label.  When such an event occurs, the mouse listener 
object invokes requestFocus() on the object that was the 
source of the mouse event. This causes the Label and the 
Frame to receive the focus when clicked on.

Note that the Button and the TextField automatically 
receive the focus when clicked on without intervention by 
the programmer.

A KeyListener object is instantiated and registered to 
listen for keyPressed() events on the Frame, Label, Button,
and TextField objects.  

Whenever a key is pressed, the object currently holding the
focus generates a keyPressed() event.  The KeyListener 
object determines which component generated the event and 
displays a message to that effect.  Thus, the Label and the
Frame which "don't normally respond" to the keyboard are 
forced to respond to the keyboard in this program.

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


Pressing a key causes messages such as the following to be
displayed:

Got keyPressed event from textfield0
Got keyPressed event from frame0
Got keyPressed event from label0
Got keyPressed event from button0

This program retrieves and saves the component names 
automatically assigned to the visual components (under the
assumption that the system will assign unique names) and 
uses those names to determine which component generated an
event when such determination is necessary.

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

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

public class Event15 {
  public static void main(String[] args){
    GUI gui = new GUI();
  }//end main
}//end class Event15
//=======================================================//

//The following class is used to instantiate a graphical 
// user interface object.
class GUI {
  //save automatic component name here
  String myTextFieldName; 
  String myButtonName;
  String myFrameName;
  String myLabelName;
  Frame myFrame;//ref variable passed as parameter
  Label myLabel;

  public GUI(){//constructor
    //Create several visual components 
    TextField myTextField = new TextField(
                                       "TextField Object");
    //save the name of the component
    myTextFieldName = myTextField.getName();

    myLabel = new Label("LabelObject");
    myLabelName = myLabel.getName();
    //make it yellow so it will show up
    myLabel.setBackground(Color.yellow);

    Button myButton = new Button("Button Object");
    myButtonName = myButton.getName();
  
    myFrame = new Frame();
    myFrame.setSize(250,300);
    myFrame.setTitle("Copyright 1997, R.G.Baldwin");
    myFrameName = myFrame.getName();
    
    //Add the other objects to the frame using default 
    // border layout manager
    myFrame.add("North",myButton);
    myFrame.add("South",myTextField);
    myFrame.add("West",myLabel);
    
    myFrame.setVisible(true);//make the frame visible
    //shift the focus to the TextField
    myTextField.requestFocus();

    //Instantiate and register a FocusListener object which
    // will process focus events on two different visual 
    // components.  In this case, the listener object makes
    // a color change on the visual component to indicate 
    // focus gained or focus lost.
    myFocusListener focusHandler = 
                                 new myFocusListener(this);
    myFrame.addFocusListener(focusHandler);  
    myLabel.addFocusListener(focusHandler);
    
    //Instantiate and register a MouseListener object which
    // will process mouse events on two different visual 
    // components.  In this case, the listener object 
    // specifically requests focus for the Label and the 
    // Frame components when those components are clicked 
    // on.
    
    myMouseListener mouseHandler = 
                                 new myMouseListener(this);
    myFrame.addMouseListener(mouseHandler);
    myLabel.addMouseListener(mouseHandler);    
    
    //Instantiate and register a KeyListener object which
    // will process key events on four different
    // components.  Whenever a key is pressed, a message is
    // displayed showing that the component that had the 
    // focus received a keyPressed event.
    myKeyListener keyHandler = new myKeyListener(this);
    myFrame.addKeyListener(keyHandler);
    myTextField.addKeyListener(keyHandler);
    myButton.addKeyListener(keyHandler);
    myLabel.addKeyListener(keyHandler);    
    

    //Instantiate and register a WindowListener 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
//=======================================================//

//This FocusListener class is used to instantiate a 
// Listener object that listens for focus events on the 
// Frame and the Label. Whenever a focusLost() or 
// focusGained() event occurs, it makes a color change on 
// the component to provide a visual indication of the
// gain or loss of focus.

class myFocusListener implements FocusListener{
  GUI thisObject;
  
  myFocusListener(GUI thisObjectIn){//constructor
    thisObject = thisObjectIn;
  }// end constructor
  
  public void focusGained(FocusEvent e){
    if( e.toString().indexOf("on " + 
                           thisObject.myFrameName) != -1 ){
      thisObject.myFrame.setBackground(Color.blue);
    }//end if
    if( e.toString().indexOf("on " + 
                           thisObject.myLabelName) != -1 ){
      thisObject.myLabel.setForeground(Color.red);
    }//end if
    thisObject.myFrame.repaint();
  }//end focusGained()

  public void focusLost(FocusEvent e){
    if( e.toString().indexOf("on " + 
                           thisObject.myFrameName) != -1 ){
      thisObject.myFrame.setBackground(Color.white);      
    }//end if       
    if( e.toString().indexOf("on " + 
                           thisObject.myLabelName) != -1 ){
      thisObject.myLabel.setForeground(Color.black);      
    }//end if       
    thisObject.myFrame.repaint();
  }//end focusLost()
}//end class myFocusListener
//=======================================================//

//This listener class listens for mouse presses on the 
// Frame and Label components.  These components do not
// automatically receive the focus when clicked with the
// mouse.  This listener class requests the focus for these
// components when they are clicked on with the mouse.

class myMouseListener extends MouseAdapter{
  GUI thisObject;
  
  myMouseListener(GUI thisObjectIn){//constructor
    thisObject = thisObjectIn;
  }//end constructor
  public void mousePressed(MouseEvent e){
    if( e.toString().indexOf("on " + 
                           thisObject.myFrameName) != -1 ){
      thisObject.myFrame.requestFocus();
    }//end if

    if( e.toString().indexOf("on " + 
                           thisObject.myLabelName) != -1 ){
      thisObject.myLabel.requestFocus();
    }//end if
    
  }//end mousePressed()
}//end class myMouseListener
//=======================================================//

//This listener class listens for key presses and displays
// a message when a keyPressed() event occurs. The
// component that has the focus when the keyPress event
// occurs generates the event.  This class identifies the
// component that generated the event and displays a 
// message identifying that component.

// The significant thing here is that any component that
// has the focus will generate a keyPress event, including
// those components, such as Label objects, which don't
// "normally" have the focus.

class myKeyListener extends KeyAdapter{
  GUI thisObject;
  
  myKeyListener(GUI thisObjectIn){//constructor
    thisObject = thisObjectIn;
  }//end constructor

  public void keyPressed(KeyEvent e){
    if( e.toString().indexOf("on " + 
                           thisObject.myFrameName) != -1 ){
      System.out.println("Got keyPressed event from " +
                                   thisObject.myFrameName);
    }//end if

    if( e.toString().indexOf("on " + 
                       thisObject.myTextFieldName) != -1 ){
      System.out.println("Got keyPressed event from " +
                               thisObject.myTextFieldName);
    }//end if

    if( e.toString().indexOf("on " + 
                          thisObject.myButtonName) != -1 ){
      System.out.println("Got keyPressed event from " +
                                  thisObject.myButtonName);
    }//end if

    if( e.toString().indexOf(
                   "on " + thisObject.myLabelName) != -1 ){
      System.out.println("Got keyPressed event from " +
                                   thisObject.myLabelName);
    }//end if
    
  }//end keyPressed()
}//end class myKeyListener
//=======================================================//

//The following listener is used to 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
//=======================================================//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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