繼續在看Java Tutorial,不過已經到了GUI了,呵呵!~
在"Introduction to Event Listeners"中,看到了JAVA的事件監聽機制。
實現事件監聽機制,需要有如下三件事情:
1、In the declaration for the event handler class, one line of code specifies that the class either implements a listener interface or extends a class that implements a listener interface. For example:
?public class MyClass implements ActionListener {
?????? public ? class ?MyClass?implement?ActionListener?{
2、Another line of code registers an instance of the event handler class as a listener on one or more components. For example:
?someComponent.addActionListener(instanceOfMyClass);
?someComponent.addActionListener(instanceOfMyClass);
3、The event handler class has code that implements the methods in the listener interface. For example:
?public void actionPerformed(ActionEvent e) {
???? ...//code that reacts to the action...
?}
? public ? void ?actionPerformed(ActionEvent?e)?{
???? ...//code that reacts to the action...
?}
?
事件監聽模型如下圖所示:

同時,其實事情監聽的基本內容就是這些了,不過其中應該注意的是JAVA的多監聽器的使用:

在上圖可以看出,JAVA中的一個事件清所產生的事件對象可以發送至多個事件監聽接口,那么就有如下的程序片塊:




?2


?3

?4

?5

?6

?7

?8

?9

10



11

12

13

14

15



16


17



18

19

20

21

22

呵,大家可以測試下哦!`
可以運行JAVA的測試程序: Run MultiListener