Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'introduction to java: 4 -events', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 4 In this chapter Java 1.0 Event Model The Event Class The Java 1.1 Event Model Events This chapter covers Java s event-driven programming model. Unlike procedural programs windows-based programs require an event-driven model in which the underlying environment tells your program when something happens. For example when the user clicks on the mouse the environment generates an event that it sends to the program. The program must then figure out what the mouse click means and act accordingly. This chapter covers two different event models or ways of handling events. In Java 1.0.2 and earlier events were passed to all components that could possibly have an interest in them. Events themselves were encapsulated in a single Event class. Java 1.1 implements a delegation model in which events are distributed only to objects that have been registered to receive the event. While this is somewhat more complex it is much more efficient and also more flexible because it allows any object to receive the events generated by a component. In turn this means that you can separate the user interface itself from the event-handling code. In the Java 1.1 event model all event functionality is contained in a new package java.awt.event. Within this package subclasses of the abstract class AWTEvent represent different kinds of events. The package also includes a number of EventListener interfaces that are implemented by classes that want to receive different kinds of events they define the methods that are called when events of the appropriate type occur. A number of adapter classes are also included they correspond to the EventListener interfaces and provide null implementations of the methods in the corresponding listener. The adapter classes aren t essential but provide a convenient shortcut for developers rather than declaring that your class implements a particular EventListener interface you can declare that your class extends the appropriate adapter. 94 4.1 Java 1.0 Event Model 95