1
import
java.awt.
*
;
2 import javax.swing. * ;
3 /**
4 * 动作 Action 示例
5 * ActionListener-->Action
6 */
7
8 import com.sun.java_cup.internal.internal_error;
9
10 import java.awt.event. * ;
11
12 public class ActionTest {
13 public static void main(String[] argu)
14 {
15 EventQueue.invokeLater( new Runnable()
16 {
17 public void run()
18 {
19 ActionFrame actionframe = new ActionFrame();
20 actionframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21 actionframe.setVisible( true );
22 }
23 });
24 }
25 }
26 class ActionFrame extends JFrame
27 {
28 public ActionFrame()
29 {
30 setTitle( " ActionTest " );
31 setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
32
33 buttonPanel = new JPanel();
34 // 实例化Action
35 Action yellowAction = new ColorAction( " Yellow " , new ImageIcon( " yellow.gif " ),Color.YELLOW);
36 Action blueAction = new ColorAction( " Blue " , new ImageIcon( " blue.gif " ),Color.BLUE);
37 Action redAction = new ColorAction( " Red " , new ImageIcon( " red.gif " ),Color.RED);
38 // 用Action制作控件(按钮)
39 buttonPanel.add( new JButton(yellowAction));
40 buttonPanel.add( new JButton(blueAction));
41 buttonPanel.add( new JButton(redAction));
42
43 add(buttonPanel);
44 // InputMap 1次映射,从键盘到控件
45 InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
46 imap.put(KeyStroke.getKeyStroke( " ctrl Y " ), " panel.yellow " );
47 imap.put(KeyStroke.getKeyStroke( " ctrl B " ), " panel.blue " );
48 imap.put(KeyStroke.getKeyStroke( " ctrl R " ), " panel.red " );
49 // ActionMap 2次映射,从控件到Action
50 ActionMap amap = buttonPanel.getActionMap();
51 amap.put( " panel.yellow " , yellowAction);
52 amap.put( " panel.blue " , blueAction);
53 amap.put( " panel.red " , redAction);
54 }
55 // 自定义Action类ColorAction(使用AbstractAction)
56 public class ColorAction extends AbstractAction
57 {
58 public ColorAction(String name,ImageIcon icon,Color c)
59 {
60 putValue(Action.NAME, name);
61 putValue(Action.SMALL_ICON, icon);
62 putValue(Action.SHORT_DESCRIPTION, " Set panel color to " + name.toLowerCase());
63 putValue( " color " , c);
64 }
65 public void actionPerformed(ActionEvent event)
66 {
67 Color c = (Color)getValue( " color " );
68 buttonPanel.setBackground(c);
69 }
70 }
71
72 private JPanel buttonPanel;
73 public static final int DEFAULT_WIDTH = 300 ;
74 public static final int DEFAULT_HEIGHT = 300 ;
75 }
2 import javax.swing. * ;
3 /**
4 * 动作 Action 示例
5 * ActionListener-->Action
6 */
7
8 import com.sun.java_cup.internal.internal_error;
9
10 import java.awt.event. * ;
11
12 public class ActionTest {
13 public static void main(String[] argu)
14 {
15 EventQueue.invokeLater( new Runnable()
16 {
17 public void run()
18 {
19 ActionFrame actionframe = new ActionFrame();
20 actionframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21 actionframe.setVisible( true );
22 }
23 });
24 }
25 }
26 class ActionFrame extends JFrame
27 {
28 public ActionFrame()
29 {
30 setTitle( " ActionTest " );
31 setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
32
33 buttonPanel = new JPanel();
34 // 实例化Action
35 Action yellowAction = new ColorAction( " Yellow " , new ImageIcon( " yellow.gif " ),Color.YELLOW);
36 Action blueAction = new ColorAction( " Blue " , new ImageIcon( " blue.gif " ),Color.BLUE);
37 Action redAction = new ColorAction( " Red " , new ImageIcon( " red.gif " ),Color.RED);
38 // 用Action制作控件(按钮)
39 buttonPanel.add( new JButton(yellowAction));
40 buttonPanel.add( new JButton(blueAction));
41 buttonPanel.add( new JButton(redAction));
42
43 add(buttonPanel);
44 // InputMap 1次映射,从键盘到控件
45 InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
46 imap.put(KeyStroke.getKeyStroke( " ctrl Y " ), " panel.yellow " );
47 imap.put(KeyStroke.getKeyStroke( " ctrl B " ), " panel.blue " );
48 imap.put(KeyStroke.getKeyStroke( " ctrl R " ), " panel.red " );
49 // ActionMap 2次映射,从控件到Action
50 ActionMap amap = buttonPanel.getActionMap();
51 amap.put( " panel.yellow " , yellowAction);
52 amap.put( " panel.blue " , blueAction);
53 amap.put( " panel.red " , redAction);
54 }
55 // 自定义Action类ColorAction(使用AbstractAction)
56 public class ColorAction extends AbstractAction
57 {
58 public ColorAction(String name,ImageIcon icon,Color c)
59 {
60 putValue(Action.NAME, name);
61 putValue(Action.SMALL_ICON, icon);
62 putValue(Action.SHORT_DESCRIPTION, " Set panel color to " + name.toLowerCase());
63 putValue( " color " , c);
64 }
65 public void actionPerformed(ActionEvent event)
66 {
67 Color c = (Color)getValue( " color " );
68 buttonPanel.setBackground(c);
69 }
70 }
71
72 private JPanel buttonPanel;
73 public static final int DEFAULT_WIDTH = 300 ;
74 public static final int DEFAULT_HEIGHT = 300 ;
75 }