在java swing编程过程中,经常需要处理键盘事件,例如处理快捷键等。这里就介绍如何定义键盘事件,以及如何处理这些事件。
在jdk1.2中,分别针对jcomponent和text类的对象定制了不同的处理键盘事件的方法:在jcomponent中,定义了registerkeyboardaction方法,使用这个方法来将需要处理的键盘事件以及处理事件的行为绑定在一起。text类中具有keymap对象,同jcomponent中的处理方法类似,这个对象保存着需要处理的键盘事件和对应的行为。
而在jdk1.3中,使用一种新的方法来处理键盘事件,它将jdk1.2的两种方法整合在一起。不需要区分被处理的是jcomponent还是text类型的组件。它定义了两个新的类:inputmap和actionmap。他们均是简单的表或映射。一个inputmap将一个keystroke对应到一个对象,actionmap将一个对象对应到一个行为(action)。通常inputmap中keystroke所对应的对象是一个字符串,通过这个字符串可以在actionmap中查找到相应的行为。
inputmap和actionmap中均有put方法。inputmap的put方法可以将keystroke对应到一个对象,而actionmap的put方法可以将一个对象对应到一个行为。
在每一个jcomponent组件中,会有三个缺省的inputmap和一个缺省的actionmap。他们可以通过调用getinputmap(int condition)和getactionmap()得到。三个inputmap分别是当组件本身拥有焦点时的inputmap(when_focused),当组件的祖先拥有焦点时的inputmap(when_ancestor_of_focused_component)和组件所在的窗体具有焦点时的inputmap(when_in_focused_window)(括号内表示为了得到这些inputmap,应该在getinputmap中设置的参数)。以下分别说明这三种inputmap:
1. 组件本身拥有焦点时的inputmap:当组件拥有焦点时,键盘按键按下,则java在这个inputmap中查找键盘事件所对应的keystroke对象。
2. 组件的祖先拥有焦点时的inputmap:当组件的祖先拥有焦点时,键盘按键按下,则java查找这个inputmap。
3. 组件所在的窗口拥有焦点时的inputmap:当组件所在的窗口具有焦点时,键盘按键按下,则java查找这个inputmap。
当一个键被按下,这个事件被转化成一个keystroke对象,java会查找这个jcomponent的相应inputmap(例如,当组件的祖先具有焦点时,java就查找这个jcomponent的祖先拥有焦点的inputmap)中是否有这个keystroke,如果有,取出它所对应的对象(通常是字符串),利用这个对象在这个jcomponent的actionmap中查找,如果找到对应的行为(action),则java执行这个行为的actionperformed方法(随后介绍这个方法)。从而达到处理键盘事件的目的。
每一个inputmap可以具有parent属性,这个属性的值是一个inputmap。当在一个inputmap中查找不到键盘事件的keystroke时,java会自动在它的parent属性指定的inputmap中查找,依次向上查找,直至找到。使用parent的好处是:当有一些固定的,不希望用户进行改动的键盘映射可以存放在parent属性所指定的inputmap中,从而避免被意外修改;另外可以将多个jcomponent的缺省inputmap设置具有相同的parent,使得可以共享一些键盘绑定的设置。可以通过inputmap类的setparent()方法设置它的parent属性。actionmap也具有相同的parent属性,使用方法也相同。
以上是如何将一个键盘事件对应到一个行为,以下就简单介绍行为(action)。
行为是一个实现了action接口的类。在action接口中定义了7个方法。其中最关键的是actionperformed()方法。这个方法描述了这个行为的具体操作过程。其他几个方法包括setenabled,isenabled,putvalue,getvalue,addpropertychangelistener,和removepropertychangelistener方法。他们分别用来设置行为是否可用、判断行为可用的状态、设置和取得行为的一些属性,最后两个方法用来允许其他对象在行动对象的属性发生变化后得到通知。
通常我们使用一个实现了action接口的大部分方法的抽象类abstractaction类作为基类,重载actionperformed方法以实现我们的行为。
我们用一个例子来具体说明如何进行实际的操作。
首先编写一个具体的行为,对指定的键盘事件进行处理:
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,并创建一个名为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,并将已经建立的行为与特定的对象(字符串)进行绑定:
am = son.getactionmap();
am.put("actionfocusson",whenfocusson);
am.put("actionfocusfather",whenfocusfather);
am.put("actionancestor",ancestor);
am.put("actionwindow",window);
|
运行程序及其相应结果:
1. 单击son按钮,这时如果按下'f','f','a','w',程序均会有相应的输出。这是因为,此时的焦点在son按钮上,而son按钮组件的三个inputmap都是有效的。所以他们对应的事件都会发生。
2. 单击parent按钮,这时按下'w',程序会有相应的输出。而按下'f','f','a',程序没有反应。这是因为parent按钮具有焦点,这个按钮不是son按钮的祖先,而son所在的窗口具有焦点,所以只有组件所在窗口具有焦点的inputmap是有效的。
3. 单击其他的按钮(parentpanel外的按钮),这时按下'w',程序会有相应的输出。而按下'f','f','a',程序没有反应。这是因为这些按钮具有焦点,他们不是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();
}
}
|