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

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

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

    隨筆 - 154  文章 - 60  trackbacks - 0
    <2007年11月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    聲明:

    該blog是為了收集資料,認(rèn)識(shí)朋友,學(xué)習(xí)、提高技術(shù),所以本blog的內(nèi)容除非聲明,否則一律為轉(zhuǎn)載!!

    感謝那些公開自己技術(shù)成果的高人們!!!

    支持開源,尊重他人的勞動(dòng)!!

    常用鏈接

    留言簿(3)

    隨筆分類(148)

    隨筆檔案(143)

    收藏夾(2)

    其他

    學(xué)習(xí)(技術(shù))

    觀察思考(非技術(shù))

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    在Java Swing編程過程中,經(jīng)常需要處理鍵盤事件,例如處理快捷鍵等。這里就介紹如何定義鍵盤事件,以及如何處理這些事件。

    在jdk1.2中,分別針對Jcomponent和Text類的對象定制了不同的處理鍵盤事件的方法:在Jcomponent中,定義了registerKeyboardAction方法,使用這個(gè)方法來將需要處理的鍵盤事件以及處理事件的行為綁定在一起。Text類中具有keymap對象,同Jcomponent中的處理方法類似,這個(gè)對象保存著需要處理的鍵盤事件和對應(yīng)的行為。

    而在jdk1.3中,使用一種新的方法來處理鍵盤事件,它將jdk1.2的兩種方法整合在一起。不需要區(qū)分被處理的是Jcomponent還是Text類型的組件。它定義了兩個(gè)新的類:InputMap和ActionMap。他們均是簡單的表或映射。一個(gè)InputMap將一個(gè)Keystroke對應(yīng)到一個(gè)對象,ActionMap將一個(gè)對象對應(yīng)到一個(gè)行為(Action)。通常InputMap中KeyStroke所對應(yīng)的對象是一個(gè)字符串,通過這個(gè)字符串可以在ActionMap中查找到相應(yīng)的行為。

    InputMap和ActionMap中均有put方法。InputMap的put方法可以將Keystroke對應(yīng)到一個(gè)對象,而ActionMap的put方法可以將一個(gè)對象對應(yīng)到一個(gè)行為。

    在每一個(gè)Jcomponent組件中,會(huì)有三個(gè)缺省的InputMap和一個(gè)缺省的ActionMap。他們可以通過調(diào)用getInputMap(int condition)和getActionMap()得到。三個(gè)InputMap分別是當(dāng)組件本身擁有焦點(diǎn)時(shí)的InputMap(WHEN_FOCUSED),當(dāng)組件的祖先擁有焦點(diǎn)時(shí)的InputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)和組件所在的窗體具有焦點(diǎn)時(shí)的InputMap(WHEN_IN_FOCUSED_WINDOW)(括號(hào)內(nèi)表示為了得到這些InputMap,應(yīng)該在getInputMap中設(shè)置的參數(shù))。以下分別說明這三種InputMap:

    1. 組件本身擁有焦點(diǎn)時(shí)的InputMap:當(dāng)組件擁有焦點(diǎn)時(shí),鍵盤按鍵按下,則java在這個(gè)InputMap中查找鍵盤事件所對應(yīng)的KeyStroke對象。

    2. 組件的祖先擁有焦點(diǎn)時(shí)的InputMap:當(dāng)組件的祖先擁有焦點(diǎn)時(shí),鍵盤按鍵按下,則java查找這個(gè)InputMap。

    3. 組件所在的窗口擁有焦點(diǎn)時(shí)的InputMap:當(dāng)組件所在的窗口具有焦點(diǎn)時(shí),鍵盤按鍵按下,則java查找這個(gè)InputMap。

    當(dāng)一個(gè)鍵被按下,這個(gè)事件被轉(zhuǎn)化成一個(gè)KeyStroke對象,java會(huì)查找這個(gè)Jcomponent的相應(yīng)InputMap(例如,當(dāng)組件的祖先具有焦點(diǎn)時(shí),java就查找這個(gè)Jcomponent的祖先擁有焦點(diǎn)的InputMap)中是否有這個(gè)KeyStroke,如果有,取出它所對應(yīng)的對象(通常是字符串),利用這個(gè)對象在這個(gè)Jcomponent的ActionMap中查找,如果找到對應(yīng)的行為(Action),則java執(zhí)行這個(gè)行為的actionPerformed方法(隨后介紹這個(gè)方法)。從而達(dá)到處理鍵盤事件的目的。

    每一個(gè)InputMap可以具有parent屬性,這個(gè)屬性的值是一個(gè)InputMap。當(dāng)在一個(gè)InputMap中查找不到鍵盤事件的KeyStroke時(shí),java會(huì)自動(dòng)在它的parent屬性指定的InputMap中查找,依次向上查找,直至找到。使用parent的好處是:當(dāng)有一些固定的,不希望用戶進(jìn)行改動(dòng)的鍵盤映射可以存放在parent屬性所指定的InputMap中,從而避免被意外修改;另外可以將多個(gè)Jcomponent的缺省InputMap設(shè)置具有相同的parent,使得可以共享一些鍵盤綁定的設(shè)置。可以通過InputMap類的setparent()方法設(shè)置它的parent屬性。ActionMap也具有相同的parent屬性,使用方法也相同。

    以上是如何將一個(gè)鍵盤事件對應(yīng)到一個(gè)行為,以下就簡單介紹行為(Action)。

    行為是一個(gè)實(shí)現(xiàn)了Action接口的類。在Action接口中定義了7個(gè)方法。其中最關(guān)鍵的是actionPerformed()方法。這個(gè)方法描述了這個(gè)行為的具體操作過程。其他幾個(gè)方法包括setEnabled,isEnabled,putValue,getValue,addPropertyChangeListener,和removePropertyChangeListener方法。他們分別用來設(shè)置行為是否可用、判斷行為可用的狀態(tài)、設(shè)置和取得行為的一些屬性,最后兩個(gè)方法用來允許其他對象在行動(dòng)對象的屬性發(fā)生變化后得到通知。

    通常我們使用一個(gè)實(shí)現(xiàn)了Action接口的大部分方法的抽象類AbstractAction類作為基類,重載actionPerformed方法以實(shí)現(xiàn)我們的行為。

    我們用一個(gè)例子來具體說明如何進(jìn)行實(shí)際的操作。

    首先編寫一個(gè)具體的行為,對指定的鍵盤事件進(jìn)行處理:

    public class TextAction extends AbstractAction
                {
                 private String a;
                 public TextAction(String a)
                 { this.a = a; }
                  public void actionPerformed(ActionEvent parm1)
                  {
                   String b = parm1.getActionCommand(); //得到行為的命令字符串
                   System.out.println("command="+b);
                   System.out.println("prompt="+this.a);
                  }
                 }

    建立四個(gè)TextAction對象:

    TextAction whenFocusSon = new TextAction("focus son");

    TextAction whenFocusFather = new TextAction("focus father");

    TextAction window = new TextAction("window");

    TextAction ancestor = new TextAction("ancestor");

    隨后,在一個(gè)窗體中加入兩個(gè)面板,名為sonPanel和parentPanel,使得parentPanel是sonPanel的祖先。并在sonPanel中加入一個(gè)名為son的button,在parentPanel中加入名為parent的button。在fatherPanel外加入幾個(gè)button。

    得到son組件的三個(gè)InputMap,并創(chuàng)建一個(gè)名為focusFatherIm的InputMap,使得這個(gè)InputMap成為focusIm的parent:

    //get default inputMap (when focus inputmap) and set a parent InputMap
                focusIm = son.getInputMap();
                focusFatherIm = new InputMap();
                focusIm.setParent(focusFatherIm);
                //get WHEN_ANCESTOR_OF_FOCUSED_COMPONENT inputMap
                ancestorIm = son.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                //get WHEN_IN_FOCUSED_WINDOW inputMap
                windowIm = son.getInputMap(WHEN_IN_FOCUSED_WINDOW);
                //在這些InputMap中分別加入鍵盤綁定:
                focusIm.put(KeyStroke.getKeyStroke('f'),"actionFocusSon");
                focusFatherIm.put(KeyStroke.getKeyStroke('F'),"actionFocusFather");
                ancestorIm.put(KeyStroke.getKeyStroke('a'),"actionAncestor");
                windowIm.put(KeyStroke.getKeyStroke('w'),"actionWindow");
                //得到son組件的缺省的ActionMap,并將已經(jīng)建立的行為與特定的對象(字符串)進(jìn)行綁定:
                am = son.getActionMap();
                am.put("actionFocusSon",whenFocusSon);
                am.put("actionFocusFather",whenFocusFather);
                am.put("actionAncestor",ancestor);
                am.put("actionWindow",window);

    運(yùn)行程序及其相應(yīng)結(jié)果:

    1. 單擊son按鈕,這時(shí)如果按下'f','F','a','w',程序均會(huì)有相應(yīng)的輸出。這是因?yàn)椋藭r(shí)的焦點(diǎn)在son按鈕上,而son按鈕組件的三個(gè)InputMap都是有效的。所以他們對應(yīng)的事件都會(huì)發(fā)生。

    2. 單擊parent按鈕,這時(shí)按下'w',程序會(huì)有相應(yīng)的輸出。而按下'f','F','a',程序沒有反應(yīng)。這是因?yàn)閜arent按鈕具有焦點(diǎn),這個(gè)按鈕不是son按鈕的祖先,而son所在的窗口具有焦點(diǎn),所以只有組件所在窗口具有焦點(diǎn)的InputMap是有效的。

    3. 單擊其他的按鈕(parentPanel外的按鈕),這時(shí)按下'w',程序會(huì)有相應(yīng)的輸出。而按下'f','F','a',程序沒有反應(yīng)。這是因?yàn)檫@些按鈕具有焦點(diǎn),他們不是son按鈕的祖先,而son所在的窗口具有焦點(diǎn),所以只有組件所在窗口具有焦點(diǎn)的InputMap是有效的。

    附:主要程序代碼:

    import java.awt.*;
                import javax.swing.*;
                import com.borland.jbcl.layout.*;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import com.sun.java.swing.plaf.motif.*;
                public class EventPanel extends JPanel implements ActionListener
                {
                 JButton btnYellow = new JButton();
                 JButton btnBlue = new JButton();
                 JButton btnRed = new JButton();
                 JPanel parentPanel = new JPanel();
                 JPanel sonPanel = new JPanel();
                 XYLayout xYLayout1 = new XYLayout();
                 JButton son = new JButton();
                 JButton parent = new JButton();
                 public EventPanel()
                 {
                  try{
                   jbInit();
                  }catch(Exception ex)
                  { ex.printStackTrace(); }
                  }
                 void jbInit() throws Exception
                 {
                  btnYellow.setText("Yellow");
                  btnYellow.setBounds(new Rectangle(35, 23, 97, 29));
                  this.setLayout(null);
                  btnBlue.setBounds(new Rectangle(154, 21, 97, 29));
                  btnBlue.setText("Blue");
                  btnRed.setBounds(new Rectangle(272, 24, 97, 29));
                  btnRed.setText("Red");
                  parentPanel.setBorder(BorderFactory.createRaisedBevelBorder());
                  parentPanel.setBounds(new Rectangle(27, 68, 358, 227));
                  parentPanel.setLayout(xYLayout1);
                  sonPanel.setBorder(BorderFactory.createLoweredBevelBorder());
                  son.setText("son");
                  parent.setText("parent");
                  this.add(btnYellow, null);
                  this.add(btnBlue, null);
                  this.add(btnRed, null);
                  this.add(parentPanel, null);
                  parentPanel.add(sonPanel, new XYConstraints(58, 22, 229, 125));
                  sonPanel.add(son, null);
                  parentPanel.add(parent, new XYConstraints(150, 167, -1, -1));
                  btnYellow.addActionListener(this);
                  btnRed.addActionListener(this);
                  btnBlue.addActionListener(this);
                  InputMap focusIm,focusFatherIm,ancestorIm,windowIm;
                  ActionMap am;
                  //create four TextAction for diff purpose
                  TextAction whenFocusSon = new TextAction("focus son");
                  TextAction whenFocusFather = new TextAction("focus father");
                  TextAction window = new TextAction("window");
                  TextAction ancestor = new TextAction("ancestor");
                  //get default inputMap (when focus inputmap) and set a parent InputMap
                  focusIm = son.getInputMap();
                  focusFatherIm = new InputMap();
                  focusIm.setParent(focusFatherIm);
                  //get WHEN_ANCESTOR_OF_FOCUSED_COMPONENT inputMap
                  ancestorIm = son.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                  //get WHEN_IN_FOCUSED_WINDOW inputMap
                  windowIm = son.getInputMap(WHEN_IN_FOCUSED_WINDOW);
                  //put the keyStroke to the InputMap
                  focusIm.put(KeyStroke.getKeyStroke('f'),"actionFocusSon");
                  focusFatherIm.put(KeyStroke.getKeyStroke('F'),"actionFocusFather");
                  ancestorIm.put(KeyStroke.getKeyStroke('a'),"actionAncestor");
                  windowIm.put(KeyStroke.getKeyStroke('w'),"actionWindow");
                  //get the actionMap
                  am = son.getActionMap();
                  am.put("actionFocusSon",whenFocusSon);
                  am.put("actionFocusFather",whenFocusFather);
                  am.put("actionAncestor",ancestor);
                  am.put("actionWindow",window);
                 }
                 public void actionPerformed(ActionEvent e)
                 {
                  //this code is used to change the backgracolor
                  Object source=e.getSource();
                  Color color=null;//=getBackground();
                  if (source==btnYellow) color=Color.yellow;
                  else if (source==btnRed) color = Color.red;
                  else if (source == btnBlue) color = Color.blue;
                  setBackground(color);
                  repaint();
                 }
                }
    posted on 2007-11-15 13:52 lk 閱讀(676) 評(píng)論(0)  編輯  收藏 所屬分類: j2se
    主站蜘蛛池模板: 在线免费观看色片| 中出五十路免费视频| 国产免费爽爽视频在线观看| 一本久久综合亚洲鲁鲁五月天| 亚洲字幕AV一区二区三区四区 | 污视频网站免费在线观看| 天天操夜夜操免费视频| 特级aa**毛片免费观看| 免费A级毛片在线播放不收费| 日本亚洲欧美色视频在线播放| 国产福利免费在线观看| 香蕉视频免费在线| 亚洲电影中文字幕| 51精品视频免费国产专区| 久久精品国产亚洲AV忘忧草18| 无码少妇一区二区浪潮免费| 亚洲色www永久网站| 手机在线看永久av片免费| 一级全免费视频播放| 国产亚洲人成无码网在线观看| 无码精品国产一区二区三区免费| 久久久久亚洲精品日久生情| 台湾一级毛片永久免费| 亚洲码欧美码一区二区三区| 久久91亚洲精品中文字幕| 免费一级肉体全黄毛片| 无码av免费毛片一区二区| 丁香花在线观看免费观看图片| 亚洲精品无码少妇30P| 亚洲视频在线观看免费视频| 亚洲一级片免费看| 91青青青国产在观免费影视| 成人亚洲国产va天堂| 又黄又爽一线毛片免费观看 | 国产裸体美女永久免费无遮挡| 国产亚洲综合色就色| 免费a级毛片无码av| 久久受www免费人成_看片中文| 另类免费视频一区二区在线观看 | 亚洲狠狠婷婷综合久久| 亚洲乱码无码永久不卡在线|