繼續(xù)在看Java Tutorial,不過已經(jīng)到了GUI了,呵呵!~
在"Introduction to Event Listeners"中,看到了JAVA的事件監(jiān)聽機(jī)制。
實(shí)現(xiàn)事件監(jiān)聽機(jī)制,需要有如下三件事情:
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...
?}
?
事件監(jiān)聽模型如下圖所示:

同時,其實(shí)事情監(jiān)聽的基本內(nèi)容就是這些了,不過其中應(yīng)該注意的是JAVA的多監(jiān)聽器的使用:

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




?2


?3

?4

?5

?6

?7

?8

?9

10



11

12

13

14

15



16


17



18

19

20

21

22

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