用户点击窗口中的yellow按钮、用户按下Ctrl + Y 键,都会改变背景颜色至黄色。多个事件源调用的都是同一个类的actionPerformed()方法.
Ctrl + Y 黄
Ctrl + B 绿
Ctrl + L 黑
相关类:
public class KeyStroke extends AWTKeyStrokeA KeyStroke represents a key action on the keyboard, or equivalent input device.
public abstract class AbstractAction extends Object implements Action, Cloneable, SerializableThis class provides default implementations for the JFC
Action
interface. Standard behaviors like the get and set methods for
Action
object properties (icon, text, and enabled) are defined here. The developer need only subclass this abstract class and define the
actionPerformed
method.
相关方法:
JComponent类
public finalInputMapgetInputMap(intcondition)Returns the
InputMap
that is used during
condition
.
condition
- one of WHEN_IN_FOCUSED_WINDOW, WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
public finalActionMapgetActionMap()Returns the
ActionMap
used to determine what
Action
to fire for particular
KeyStroke
binding. The returned
ActionMap
, unless otherwise set, will have the
ActionMap
from the UI set as the parent.
代码:
package cn.youthol;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
MyFrame f = new MyFrame("改变背景颜色");
f.setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
/**
* 框架窗口
*/
class MyFrame extends JFrame
{
private JPanel mainPanel;
private final int WINDOW_WIDTH = 800;
private final int WINDOW_HEIGHT = 600;
private static String yellow = "toYellow";
private static String blue = "toBlue";
private static String black = "toBlack";
public MyFrame(String title)
{
super(title);
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
mainPanel = new JPanel();
add(mainPanel);
//设置Button和动作监听器
setComponents();
}
private void setComponents()
{
//创建监听器
Action yellowListener = new KeyListener("Yellow",Color.YELLOW);
Action blackListener = new KeyListener("Black",Color.BLACK);
Action blueListener = new KeyListener("Blue",Color.BLUE);
//创建Button
JButton btnYellow = new JButton(yellowListener);
JButton btnBlack = new JButton(blackListener);
JButton btnBlue = new JButton(blueListener);
//添加Button
mainPanel.add(btnYellow);
mainPanel.add(btnBlack);
mainPanel.add(btnBlue);
//设置Map
InputMap imap = mainPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke("ctrl Y"), MyFrame.yellow);
imap.put(KeyStroke.getKeyStroke("ctrl B"), MyFrame.blue);
imap.put(KeyStroke.getKeyStroke("ctrl L"), MyFrame.black);
ActionMap amap = mainPanel.getActionMap();
amap.put(MyFrame.yellow, yellowListener);
amap.put(MyFrame.blue, blueListener);
amap.put(MyFrame.black, blackListener);
}
/*
* 监听器类
*/
private class KeyListener extends AbstractAction
{
/*
* 构造方法
*/
public KeyListener(String actionName,Color c)
{
putValue(Action.NAME,actionName); //动作名
putValue(Action.SHORT_DESCRIPTION,"改变背景"); //这个字符串会出现在工具栏或按钮上,做为一个prompt
putValue("color",c); //把Color对象存储到"color"中,名/值对应
}
/*
* 改变背景颜色
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
Color c = (Color)getValue("color"); //取出Color
mainPanel.setBackground(c); //改变背景颜色
}
}
}