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);
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(); } }
<script type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/js/r20101117/r20101214/show_ads_impl.js"></script><script src="http://pagead2.googlesyndication.com/pagead/expansion_embed.js"></script><script src="http://googleads.g.doubleclick.net/pagead/test_domain.js"></script><script src="http://pagead2.googlesyndication.com/pagead/render_ads.js"></script><script></script>