<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    學(xué)海拾遺

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

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

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

    繼續(xù)在看Java Tutorial,不過(guò)已經(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 {

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

    ???????另外有代碼注冊(cè)一個(gè)事件處理類的實(shí)例到一個(gè)或多個(gè)部件的接口(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í),其實(shí)事情監(jiān)聽的基本內(nèi)容就是這些了,不過(guò)其中應(yīng)該注意的是JAVA的多監(jiān)聽器的使用:
    MultiListener.bmp
    在上圖可以看出,JAVA中的一個(gè)事件清所產(chǎn)生的事件對(duì)象可以發(fā)送至多個(gè)事件監(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分別注冊(cè)了MultiListener和Eavesdropper兩個(gè)類的對(duì)象,按照前面那個(gè)圖的意思,那么button2所產(chǎn)生的事件對(duì)象會(huì)被分別發(fā)至MultiListener和Eavesdropper。
    呵,大家可以測(cè)試下哦!`
    可以運(yùn)行JAVA的測(cè)試程序: Run MultiListener

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国内精品免费麻豆网站91麻豆 | 精品国产日韩久久亚洲| 午夜两性色视频免费网站| 免费人成视频在线播放| 久久综合图区亚洲综合图区| 成人免费a级毛片| 国产在线观看xxxx免费| 亚洲综合在线一区二区三区| 亚洲中文字幕无码专区| 我的小后妈韩剧在线看免费高清版| 麻豆69堂免费视频| 亚洲视频免费在线看| 亚洲日韩精品无码专区网站| 免费观看的毛片大全| 久99久无码精品视频免费播放| 亚洲一级黄色大片| 亚洲韩国精品无码一区二区三区| 毛片大全免费观看| 久久精品免费观看| 国产成人综合亚洲绿色| 亚洲另类视频在线观看| 亚洲综合网站色欲色欲| 午夜精品在线免费观看| 美女内射无套日韩免费播放| 无人视频免费观看免费视频| 亚洲中文字幕人成乱码| 亚洲精品成人av在线| 亚洲国产精品无码久久青草| 免费国产黄线在线观看 | 免费无遮挡无码永久在线观看视频| 日韩电影免费观看| 337P日本欧洲亚洲大胆精品| 亚洲国产av一区二区三区丶| 人人狠狠综合久久亚洲88| 国产男女猛烈无遮档免费视频网站| 日本人的色道免费网站| 久久精品免费一区二区三区| 一级看片免费视频囗交| 特黄特色大片免费| 亚洲а∨精品天堂在线| 亚洲欧洲日韩极速播放|