學海拾遺

          生活、技術、思想無處不在學習
          posts - 52, comments - 23, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java中的事件監聽

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

          繼續在看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 {

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

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

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


          ?


          事件監聽模型如下圖所示:
          ActionEventModel.bmp

          同時,其實事情監聽的基本內容就是這些了,不過其中應該注意的是JAVA的多監聽器的使用:
          MultiListener.bmp
          在上圖可以看出,JAVA中的一個事件清所產生的事件對象可以發送至多個事件監聽接口,那么就有如下的程序片塊:
          ?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所產生的事件對象會被分別發至MultiListener和Eavesdropper。
          呵,大家可以測試下哦!`
          可以運行JAVA的測試程序: Run MultiListener

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


          網站導航:
           
          主站蜘蛛池模板: 高青县| 锦州市| 军事| 吉木乃县| 苗栗县| 杭州市| 夏邑县| 博白县| 纳雍县| 福鼎市| 十堰市| 安阳市| 靖宇县| 曲靖市| 武穴市| 阿巴嘎旗| 策勒县| 安阳市| 贡嘎县| 宁化县| 长武县| 金华市| 香格里拉县| 曲松县| 连南| 永康市| 开封市| 芦山县| 贵南县| 东乌珠穆沁旗| 金堂县| 延安市| 察隅县| 吉木萨尔县| 汉沽区| 沿河| 武隆县| 六盘水市| 潮州市| 扶绥县| 姜堰市|