基于AWT、Swing的GUI编程 - 多个命令共享同一个监听器类
来源:互联网 发布:python界面设计 编辑:程序博客网 时间:2024/06/16 09:07
用户点击窗口中的yellow按钮、用户按下Ctrl + Y 键,都会改变背景颜色至黄色。多个事件源调用的都是同一个类的actionPerformed()方法.
Ctrl + Y 黄
Ctrl + B 绿
Ctrl + L 黑
相关类:
public class KeyStrokeextends AWTKeyStroke
A KeyStroke represents a key action on the keyboard, or equivalent input device.public abstract class AbstractActionextends Objectimplements Action, Cloneable, Serializable
This class provides default implementations for the JFC Action
interface. Standard behaviors like the get and set methods forAction
object properties (icon, text, and enabled) are defined here. The developer need only subclass this abstract class and define theactionPerformed
method.相关方法:
JComponent类
public final InputMap getInputMap(int condition)Returns the
InputMap
that is used during condition
.condition
- one of WHEN_IN_FOCUSED_WINDOW, WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
public final ActionMap getActionMap()Returns the
ActionMap
used to determine what Action
to fire for particularKeyStroke
binding. The returnedActionMap
, unless otherwise set, will have theActionMap
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);//创建ButtonJButton btnYellow = new JButton(yellowListener);JButton btnBlack = new JButton(blackListener);JButton btnBlue = new JButton(blueListener);//添加ButtonmainPanel.add(btnYellow);mainPanel.add(btnBlack);mainPanel.add(btnBlue);//设置MapInputMap 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,"改变背景"); //这个字符串会出现在工具栏或按钮上,做为一个promptputValue("color",c); //把Color对象存储到"color"中,名/值对应}/* * 改变背景颜色 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */public void actionPerformed(ActionEvent e){Color c = (Color)getValue("color"); //取出ColormainPanel.setBackground(c); //改变背景颜色}}}
- 基于AWT、Swing的GUI编程 - 多个命令共享同一个监听器类
- 基于AWT、Swing的GUI程序 - 改变观感
- Java GUI编程中AWT/swing/SWT的优缺点
- Java GUI编程中AWT/swing/SWT的优缺点
- AWT的GUI编程
- Java的GUI发展-AWT/SWT/Swing
- 基于GUI的AWT,Swing写的一个餐厅点菜系统
- AWT,SWT和Swing都有相似的事件监听器
- 基于Swing的GUI客户端
- java中GUI的awt和Swing的知识点
- Java GUI:SWT/Swing/AWT的介绍及比较
- GUI开发之AWT、SWING、SWT和JFACE的比较
- 多个按钮用同一个监听器监听
- 细说Java GUI:AWT,SWT,Swing
- 细说Java GUI:AWT,SWT,Swing
- 细说Java GUI:AWT,SWT,Swing
- 细说Java GUI:AWT,SWT,Swing
- 细说Java GUI:AWT,SWT,Swing
- IBATIS SQL文取不到值 用 as 可以解决
- javascript onclick时候 对象不支持此属性或方法
- 在MFC编辑框中追加文本
- Highlight WebElement
- compile openjpeg guide
- 基于AWT、Swing的GUI编程 - 多个命令共享同一个监听器类
- 基于Windows CE平台的GPS移动设备设计
- 虚函数表里边保存的不一定是虚函数的地址
- UESTC 1425(线段树)
- linux多线程【5】哲学家用餐-另一种思路
- 杜君立:紧箍咒与纸枷锁
- WinCE平台USB摄像头驱动开发流程
- Oracle中的 UPDATE FROM 解决方法
- 如何寻找创业合伙人、搭建创业团队?