JavaSwing中如何定義鍵盤事件-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)Java Swing中如何定義鍵盤事件的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、做網(wǎng)站、大足網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、大足網(wǎng)絡(luò)營銷、大足企業(yè)策劃、大足品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供大足建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn

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

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

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

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

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

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

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

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

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

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

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

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

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

首先編寫一個具體的行為,對指定的鍵盤事件進(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);
  }
 }


建立四個TextAction對象:

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

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

TextAction window = new TextAction("window");

TextAction ancestor = new TextAction("ancestor");

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

得到son組件的三個InputMap,并創(chuàng)建一個名為focusFatherIm的InputMap,使得這個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īng)結(jié)果:

1. 單擊son按鈕,這時如果按下f,F(xiàn),a,w,程序均會有相應(yīng)的輸出。這是因為,此時的焦點在son按鈕上,而son按鈕組件的三個InputMap都是有效的。所以他們對應(yīng)的事件都會發(fā)生。

2. 單擊parent按鈕,這時按下w,程序會有相應(yīng)的輸出。而按下f,F(xiàn),a,程序沒有反應(yīng)。這是因為parent按鈕具有焦點,這個按鈕不是son按鈕的祖先,而son所在的窗口具有焦點,所以只有組件所在窗口具有焦點的InputMap是有效的。

3. 單擊其他的按鈕(parentPanel外的按鈕),這時按下w,程序會有相應(yīng)的輸出。而按下f,F(xiàn),a,程序沒有反應(yīng)。這是因為這些按鈕具有焦點,他們不是son按鈕的祖先,而son所在的窗口具有焦點,所以只有組件所在窗口具有焦點的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();
 }
}

感謝各位的閱讀!關(guān)于“Java Swing中如何定義鍵盤事件”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章標(biāo)題:JavaSwing中如何定義鍵盤事件-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article18/dejjgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號建站公司、移動網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)