共性action
来源:互联网 发布:数据流程图类型 编辑:程序博客网 时间:2024/06/01 19:01
package demo.event.swing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
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.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class ActionTest {
public static void main(String[] args) {
new ActionJframe();
}
}
class ActionJframe extends JFrame {
public ActionJframe() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds((dim.width - 400) / 2, (dim.height - 400) / 2, 400, 400);
this.add(new JpanelAction());
this.setTitle("测试action");
}
}
class JpanelAction extends JPanel {
public JpanelAction() {
Action yellowAction=new ColorAction("YELLOW", new ImageIcon("F:\\ico\\3451\\1.ico"), Color.yellow);
Action blueAction=new ColorAction("BLUE", new ImageIcon("F:\\ico\\3451\\2.ico"), Color.blue);
Action redAction=new ColorAction("RED", new ImageIcon("F:\\ico\\3451\\3.ico"), Color.red);
this.add(new JButton(yellowAction));
this.add(new JButton(blueAction));
this.add(new JButton(redAction));
InputMap imap=getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASK,true), "panel.yellow");
imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");
ActionMap amap=getActionMap();
amap.put("panel.yellow", yellowAction);
amap.put("panel.blue", blueAction);
amap.put("panel.red", redAction);
}
class ColorAction extends AbstractAction {
public ColorAction(String name, Icon icon, Color color) {
putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue("color", color);
putValue(Action.SHORT_DESCRIPTION, "set panel color to "
+ name.toLowerCase());
}
public void actionPerformed(ActionEvent e) {
Color c = (Color) getValue("color");
setBackground(c);
}
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
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.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class ActionTest {
public static void main(String[] args) {
new ActionJframe();
}
}
class ActionJframe extends JFrame {
public ActionJframe() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds((dim.width - 400) / 2, (dim.height - 400) / 2, 400, 400);
this.add(new JpanelAction());
this.setTitle("测试action");
}
}
class JpanelAction extends JPanel {
public JpanelAction() {
Action yellowAction=new ColorAction("YELLOW", new ImageIcon("F:\\ico\\3451\\1.ico"), Color.yellow);
Action blueAction=new ColorAction("BLUE", new ImageIcon("F:\\ico\\3451\\2.ico"), Color.blue);
Action redAction=new ColorAction("RED", new ImageIcon("F:\\ico\\3451\\3.ico"), Color.red);
this.add(new JButton(yellowAction));
this.add(new JButton(blueAction));
this.add(new JButton(redAction));
InputMap imap=getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASK,true), "panel.yellow");
imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");
ActionMap amap=getActionMap();
amap.put("panel.yellow", yellowAction);
amap.put("panel.blue", blueAction);
amap.put("panel.red", redAction);
}
class ColorAction extends AbstractAction {
public ColorAction(String name, Icon icon, Color color) {
putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue("color", color);
putValue(Action.SHORT_DESCRIPTION, "set panel color to "
+ name.toLowerCase());
}
public void actionPerformed(ActionEvent e) {
Color c = (Color) getValue("color");
setBackground(c);
}
}
}
- 共性action
- 计算机语言共性
- 集合共性
- 共性问题
- 基础知识-拉丁舞共性
- 共性与个性
- 设备驱动程序共性
- 成功者的九个共性
- 抽象 共性 复用
- 共性和可变性分析
- 手机游戏共性分析
- 手机游戏共性分析
- 设备驱动程序共性
- 数据库设计的共性
- 3.1 共性规则
- 个性与共性
- 集合框架共性方法
- Collection共性方法
- 对于汉诺塔问题的递归解决
- 苏宁新战略部署今天宣布,这里有几点对它的建议
- WUA API学习笔记
- opencv2.3图像基本操作
- mfc vc vs 一个控件贴到另一个控件的位置上,GetWindowRect,GetClientRect
- 共性action
- Android 开发环境配置图文教程(jdk+eclipse+android sdk)
- 判断设备是否是 iphone5
- Flex 开发air学习笔记--- 复制和粘贴
- 堪称为经典游戏设计帖整理20个点击回复超高的精品贴
- iOS 源码网站
- linux wa%过高,iostat查看io状况
- 关于性格内向者的10个误解
- void和void*