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

סקירה

ש- ללא עיון בפתרון שלהלן, כתוב אפליקצית Java אשר מדגימה את השימוש באירועי focusGained() ו-focusLost() יחד עם אירוע keyPressed.

 

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

 

אובייקט מאזין חלון מאותחל ונרשם כדי לסיים את התוכנית כאשר המשתמש סוגר את אובייקט המסגרת. לחיצה על מקש tab גורם להודעות כדוגמת ההודעה להלן להיות מוצגות:

 

Got keyPressed event from button0
java.awt.event.FocusEvent[FOCUS_LOST,permanent] on button0
java.awt.event.FocusEvent[FOCUS_GAINED,permanent] on textfield0
Got keyPressed event from textfield0
java.awt.event.FocusEvent[FOCUS_LOST,permanent] on textfield0
java.awt.event.FocusEvent[FOCUS_GAINED,permanent] on button0

 

ת- ראה תוכנית להלן.

 

/*File SampProg124.java from lesson 86
Copyright 1997, R.G.Baldwin
*/
//=========================================================

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

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

class GUI {
  String myTextFieldName; //save component names here
  String myButtonName;
  
  public GUI(){//constructor
    //Create visual components 
    TextField myTextField = 
      new TextField("TextField Object");
    myTextFieldName = myTextField.getName();//save the name

    Button myButton = new Button("Button Object");
    myButtonName = myButton.getName();//save the name
  
    Frame myFrame = new Frame();
    myFrame.setSize(250,100);
    myFrame.setTitle("Copyright 1997, R.G.Baldwin");
    
    //Add objects to the frame using default border 
    // layout manager
    myFrame.add("North",myButton);
    myFrame.add("South",myTextField);
    
    myFrame.setVisible(true);//make the frame visible
 
    //Instantiate and register a FocusListener object
    myFocusListener focusHandler = new myFocusListener();
    myButton.addFocusListener(focusHandler);
    myTextField.addFocusListener(focusHandler);
    
    //Instantiate and register a KeyListener object 
    myKeyListener keyHandler = new myKeyListener(this);
    myTextField.addKeyListener(keyHandler);
    myButton.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 
// button and textField components. Whenever a focusLost() 
// or focusGained() event occurs, it displays a message to 
// that effect.

class myFocusListener implements FocusListener{
  
  public void focusGained(FocusEvent e){
    System.out.println(e.toString());
  }//end focusGained()

  public void focusLost(FocusEvent e){
    System.out.println(e.toString());    
  }//end focusLost()
}//end class myFocusListener
//=========================================================

//This listener class listens for key presses and displays
// a message when a keyPressed() event occurs. The visual 
// 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.

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.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
  }//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
//=========================================================

 08-12-03 / 19:41  עודכן ,  17-10-03 / 23:56  נוצר ע"י רונית רייכמן  בתאריך 
 רישום תוכנית - הקודםהבא - Swing, הבנת ()getContentPane ושכבות Jframe אחרות 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 4