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();
}
} |