package com.xudeyu.event;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ActionTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
ActionFrame frame = new ActionFrame();
frame.setDefaultCloseOperation
(3);
frame.setVisible(true);
}
});
}
}
class ActionFrame extends JFrame
{
private JPanel buttonPanel;
private JComboBox box;
public ActionFrame()
{
setTitle("Action Test");
setSize(500, 400);
setLocationByPlatform(true);
buttonPanel = new JPanel();
box = new JComboBox();
addUIInComboBox();
buttonPanel.add(box);
box.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
try
{
UIManager.setLookAndFeel(box.getSelectedItem()
.toString());
SwingUtilities.updateComponentTreeUI(buttonPanel);
}
catch (Exception ee)
{
ee.printStackTrace();
}
}
});
// 用Action创建一个button的action
Action buttonActionBlue = newColorActionTest("ActionButton",
new ImageIcon("test.jpg"), Color.BLUE);
//用某一个action初始化一个按钮。多个按钮可以共用一个action
JButton btnActionBlue = new JButton(buttonActionBlue);
buttonPanel.add(btnActionBlue);
// 为组件添加快捷键,将键值y赋值;
InputMap map =buttonPanel.getInputMap(JComponent.WHEN_FOCUSED);
map.put(KeyStroke.getKeyStroke("Y"),"buttonActionBlue");
//把刚才Y的赋值,添加一个action,即通过actionmap桥连键值和动作;
ActionMap actionMap = buttonPanel.getActionMap();
actionMap.put("buttonActionBlue", buttonActionBlue);
buttonPanel.addKeyListener(new MyKeyListener());
add(buttonPanel);
}
class ColorActionTest extends AbstractAction
{
public ColorActionTest(String buttonName, Icon icon, Colorc)
{
putValue(Action.NAME, buttonName);
putValue(Action.SMALL_ICON, icon);
putValue("Color", c);
putValue(Action.SHORT_DESCRIPTION, "set panel color to "
+ buttonName.toLowerCase());
}
public void actionPerformed(ActionEvent e)
{
Color c = (Color) getValue("Color");
buttonPanel.setBackground(c);
}
}
class MyKeyListener implements KeyListener
{
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyChar() == 'A')
{
buttonPanel.setBackground(Color.black);
}
}
@Override
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == 'B')
{
buttonPanel.setBackground(Color.cyan);
}
}
@Override
public void keyTyped(KeyEvent e)
{
}
}
private void addUIInComboBox()
{
// 获取所有的界面
UIManager.LookAndFeelInfo[] infos = UIManager
.getInstalledLookAndFeels
();
for (UIManager.LookAndFeelInfo ui : infos)
{
box.addItem(ui.getClassName());
}
}
}