חזרה
ש- מבלי לראות את הפתרון בהמשך, כתוב אפליקציית Java אשר ממלאה אחר הדרישות הבאות: מחלקת אובייקט לא ויזואלי (נקראת NonVisual) מוגדרת. אובייקטים של מחלקה זו יכולים לייצר אירועי פעולה. אובייקט אחד מטיפוס מחלקה זו מובזק. מחלקת מאזין לפעולה אחת מוגדרת. אובייקט ממחלקה זו מובזק ונרשם להאזין לאירועי פעולה על האובייקט הלא ויזואלי המוזכר לעיל. מניעים את המשתמש להדפיס מספר תווים במקלדת. בכל פעם שהמשתמש מקיש את התו X המתודה generateActionEvent() מובזקת על האובייקט הלא ויזואלי וגורמת לאירוע פעולה להתרחש, להלכד ולהיות מעובד ע"י אובייקט מאזין לפעולה (ActionListener). זה גורם למסר הבא להיות מוצג על המסך: actionPerformed() method invoked on NonVisualObject.
ת – להלן הפתרון התוכנית נבחנה תוך שימוש ב JDK 1.1.3 שרצה תחת Win95 .
/*File SampProg129.java Copyright 1997, R.G.Baldwin This program illustrates Program Generated Events under JDK 1.1. A non-visual object class (named NonVisual) is defined. Objects of this class are capable of generating Action events. One object of the NonVisual class is instantiated. One ActionListener class is defined. An object of this class is instantiated and registered to listen for Action events on the NonVisual object mentioned above. The user is prompted to enter some characters at the keyboard. Whenever the user enters the character 'x' the generateActionEvent() method is invoked on the NonVisual object causing an Action event to occur and to be trapped and processed by the ActionListener object. This causes the following message to be displayed on the screen: actionPerformed() method invoked on NonVisualObject The program was tested using JDK 1.1.3 running under Win95. */ //========================================================= import java.awt.*; import java.awt.event.*; import java.io.*; //========================================================= public class SampProg129 { NonVisual nonVisualObject; //reference to a NonVisual obj //------------------------------------------------------- public static void main(String[] args){ //instantiate an object of this type SampProg129 thisObj = new SampProg129(); int data = 0; System.out.println("Enter some characters"); System.out.println( "Enter an 'x' to generate an Action Event"); System.out.println("Enter Ctrl-z to terminate"); try{ while((data = System.in.read()) != -1){ if( (char)data == 'x') //When the user enters an 'x', cause an Action // Event to be generated on the NonVisual object // referred to by the instance variable of this // class named nonVisualObject. thisObj.nonVisualObject.generateActionEvent(); }//end while loop }catch(IOException e){} }//end main //--------------------------------------------------------- public SampProg129(){//constructor System.out.println("Copyright 1997, R.G.Baldwin"); nonVisualObject = new NonVisual("NonVisualObject"); nonVisualObject.makeActionListenerList( new AnActionListenerClass()); }//end constructor }//end class SampProg129 //========================================================= //Class to respond to action events class AnActionListenerClass implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println( "actionPerformed() method invoked on " + e.getActionCommand()); }//end actionPerformed }//end class AnActionListenerClass //========================================================= //Class to create object capable of generating // Action events. //-------------------------------------------------------- class NonVisual extends Component { String ID; // The ID of the object //list of registered action listener objects ActionListener actionListener; //------------------------------------------------------- public NonVisual(String ID) {//Construct a NonVisual obj this.ID = ID; }//end constructor //------------------------------------------------------- //Method to construct a list of registered listeners public void makeActionListenerList( ActionListener listener) { actionListener = AWTEventMulticaster.add(actionListener, listener); }//end makeActionListenerList() //------------------------------------------------------- //Method to dispatch an action event to the registered // listeners. public void generateActionEvent() { actionListener.actionPerformed( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, ID)); }//end generateActionEvent }//end class NonVisual //========================================================= |