TAILIEUCHUNG - Java Programming for absolute beginner- P23
Java Programming for absolute beginner- P23:Hello and welcome to Java Programming for the Absolute Beginner. You probably already have a good understanding of how to use your computer. These days it’s hard to find someone who doesn’t, given the importance of computers in today’s world. Learning to control your computer intimately is what will separate you from the pack! By reading this book, you learn how to accomplish just that through the magic of programming. | 2 25 03 8 57 AM Page 398 Registering PlayAreaListeners q The registering of PlayAreaListeners takes place in the PlayArea class. You won t c see the entire source listing for until a bit later but I feel it is most relevant to discuss how it registers listeners here and how it fires events in the w CO next section Firing PlayAreaEvents. Registering the listeners is not that com- plicated. Basically PlayArea uses a Vector to maintain a dynamic list of PlayAre- 5 aListeners 0 q protected Vector listeners The PlayArea class also provides two methods for updating this Vector object addPlayAreaListener PlayAreaListener and removePlayAreaListener Pla-q yAreaListener . addPlayAreaListener PlayAreaListener adds the passed PlayAreaListener any class that implements the PlayAreaListener interface to C listeners and removePlayAreaListener PlayAreaListener removes the speciE fied PlayAreaListener. Here is what they look like j public void addPlayAreaListener PlayAreaListener pal gi pal 0 C public void removePlayAreaListener PlayAreaListener pal pal J You can see that they work just by calling the Vector class s methods. That s all it takes. Now you have a list of nosy classes that you need to notify when you fire PlayAreaEvents. Firing PlayAreaEvents You fire PlayAreaEvents from the PlayArea class by creating an instance of the PlayAreaEvent class and then calling the registered PlayAreaListeners block-Landed BlockEvent methods. The PlayArea class has a private method for doing this private void fireBlockLanded PlayAreaEvent pae for int l 0 l l PlayAreaListener l .blockLanded pae fireBlockLanded PlayAreaEvent loops on the elements in the listeners Vector and calls their blockLanded PlayAreaEvent methods passing in the given pae reference. As you can see the PlayAreaEvent object must be created prior to calling fireBlockLanded PlayAreaEvent . It does this by .
đang nạp các trang xem trước