基于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); //改变背景颜色}}}



原创粉丝点击