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

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

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

    學海拾遺

    生活、技術、思想無處不在學習
    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

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲色欲色欱wwW在线| 精品免费AV一区二区三区| 亚洲电影一区二区| 亚洲女初尝黑人巨高清| 久久精品国产免费观看 | 亚洲av不卡一区二区三区| 亚洲精品无码久久千人斩| 97在线线免费观看视频在线观看| 日本免费网站视频www区| 久久久久av无码免费网| 国产在线国偷精品产拍免费| 999zyz**站免费毛片| 亚洲av成人一区二区三区观看在线 | 91国内免费在线视频| 免费在线中文日本| 久久精品人成免费| 黄色片在线免费观看| 在线精品免费视频| 伊人久久亚洲综合影院| 国产一精品一AV一免费孕妇 | 国产在线观看免费视频软件| 日本黄色动图免费在线观看| 免费人成在线观看网站品爱网 | 18禁男女爽爽爽午夜网站免费| 99国产精品永久免费视频| 成年人视频在线观看免费| 免费一级黄色毛片| 亚洲AV无码一区东京热久久| 亚洲天堂一区二区三区| 亚洲人成色777777老人头| 成人免费网站视频www| 男女午夜24式免费视频| 精品久久久久成人码免费动漫| 日韩免费高清视频| 成年人免费视频观看| 亚洲av日韩片在线观看| 无码专区—VA亚洲V天堂| 亚洲最大的成人网站| 黄色网页在线免费观看| 一区二区三区在线免费观看视频 | 99亚洲精品高清一二区|