基于AWT、Swing的GUI编程 - 多个命令共享同一个监听器类

用户点击窗口中的yellow按钮、用户按下Ctrl + Y 键,都会改变背景颜色至黄色。多个事件源调用的都是同一个类的actionPerformed()方法.

Ctrl + Y 黄

Ctrl + B 绿

Ctrl + L 黑




相关类:

public class KeyStroke
extends AWTKeyStroke
A KeyStroke represents a key action on the keyboard, or equivalent input device.


public abstract class AbstractAction
extends Object
implements Action, Cloneable, Serializable
This 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); //改变背景颜色
		}
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值