學(xué)海拾遺

          生活、技術(shù)、思想無處不在學(xué)習(xí)
          posts - 52, comments - 23, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Java中的事件監(jiān)聽

          Posted on 2007-03-17 02:50 tanzek 閱讀(1849) 評論(0)  編輯  收藏

          繼續(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 {

          ???????在每個事件處理類的聲明中,其中有代碼指明此類實(shí)現(xiàn)某個監(jiān)聽器(Listener)接口或者繼承自某個實(shí)現(xiàn)了監(jiān)聽器(Listener)接口的類,例如:
          ??????
          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);

          ???????另外有代碼注冊一個事件處理類的實(shí)例到一個或多個部件的接口(Listener)上,例如:
          ?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...
          ?}

          ??????事件處理類實(shí)現(xiàn)了監(jiān)聽器(Listener)接口中的方法(抽象方法),例如:
          ?
          public ? void ?actionPerformed(ActionEvent?e)?{
          ???? ...//code that reacts to the action...
          ?}


          ?


          事件監(jiān)聽模型如下圖所示:
          ActionEventModel.bmp

          同時,其實(shí)事情監(jiān)聽的基本內(nèi)容就是這些了,不過其中應(yīng)該注意的是JAVA的多監(jiān)聽器的使用:
          MultiListener.bmp
          在上圖可以看出,JAVA中的一個事件清所產(chǎn)生的事件對象可以發(fā)送至多個事件監(jiān)聽接口,那么就有如下的程序片塊:
          ?1public?class?MultiListener??implements?ActionListener?{
          ?2????
          ?3????//where?initialization?occurs:
          ?4????????button1.addActionListener(this);
          ?5????????button2.addActionListener(this);
          ?6
          ?7????????button2.addActionListener(new?Eavesdropper(bottomTextArea));
          ?8????}

          ?9
          10????public?void?actionPerformed(ActionEvent?e)?{
          11????????topTextArea.append(e.getActionCommand()?+?newline);
          12????}

          13}
          14
          15class?Eavesdropper?implements?ActionListener?{
          16????
          17????public?void?actionPerformed(ActionEvent?e)?{
          18????????myTextArea.append(e.getActionCommand()?+?newline);
          19????}

          20}

          21
          22
          其中的button2分別注冊了MultiListener和Eavesdropper兩個類的對象,按照前面那個圖的意思,那么button2所產(chǎn)生的事件對象會被分別發(fā)至MultiListener和Eavesdropper。
          呵,大家可以測試下哦!`
          可以運(yùn)行JAVA的測試程序: Run MultiListener

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 乐平市| 绥芬河市| 榆树市| 安达市| 梁河县| 鸡东县| 盖州市| 巴楚县| 道孚县| 定日县| 繁昌县| 泾川县| 福安市| 汶上县| 汉川市| 郎溪县| 兖州市| 宣城市| 广州市| 兰西县| 阜宁县| 南开区| 太白县| 江北区| 固阳县| 德格县| 南江县| 寿光市| 伊通| 清涧县| 靖安县| 伊宁县| 新郑市| 新竹县| 泗洪县| 宁南县| 湄潭县| 广南县| 女性| 宜城市| 普宁市|