设置菜单项JMenuItem()的快捷键

package PreD.utils;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.Icon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;

/**
 * 
 * @Description 
 * @Author zhangzuoqiang
 * @Date 2012-1-6
 */
public class KeyStrokeDemo extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel buttonPnl = null;
	private JButton blueBtn, yellowBtn, redBtn;

	public KeyStrokeDemo() {
		super("快捷键测试程序");
		this.setSize(300, 200);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		buttonPnl = new JPanel();

		blueBtn = new JButton();
		yellowBtn = new JButton();
		redBtn = new JButton();

		Action blueAction = new ColorAction("bule", null, Color.BLUE, blueBtn);
		Action yellowAction = new ColorAction("yellow", null, Color.YELLOW,
				yellowBtn);
		Action redAction = new ColorAction("red", null, Color.RED, redBtn);

		blueBtn.setAction(blueAction);
		yellowBtn.setAction(yellowAction);
		redBtn.setAction(redAction);

		buttonPnl.add(blueBtn);
		buttonPnl.add(yellowBtn);
		buttonPnl.add(redBtn);
		this.getContentPane().add(buttonPnl, BorderLayout.CENTER);

		KeyStroke blueKS = KeyStroke.getKeyStroke("ctrl B");// 定义一个ctrl + b的快捷键
		KeyStroke yellowKS = KeyStroke.getKeyStroke("ctrl Y");// 定义一个ctrl +
																// y的快捷键
		// KeyStroke redKS = KeyStroke.getKeyStroke("ctrl R");//定义一个ctrl + r的快捷键
		// KeyStroke redKS = KeyStroke.getKeyStroke(KeyEvent.VK_R,
		// InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK);
		KeyStroke redKS = KeyStroke.getKeyStroke("ctrl shift R");

		// 以下是另一种实现快捷键定义的方法,与上面注释的代码实现效果是一样的
		// KeyStroke blueKS = KeyStroke.getKeyStroke(KeyEvent.VK_B,
		// InputEvent.CTRL_MASK);// 定义一个ctrl + b的快捷键
		// KeyStroke yellowKS = KeyStroke.getKeyStroke(KeyEvent.VK_Y,
		// InputEvent.CTRL_MASK);
		// KeyStroke redKS = KeyStroke.getKeyStroke(KeyEvent.VK_R,
		// InputEvent.CTRL_MASK);

		/** 定义一个ctrl + shift + y的快捷键 */
		// KeyStroke testKS = KeyStroke.getKeyStroke(KeyEvent.VK_Y,
		// InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK);

		InputMap imap = buttonPnl
				.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		imap.put(blueKS, "panel.blue");
		imap.put(yellowKS, "panel.yellow");
		imap.put(redKS, "panel.red");

		ActionMap amap = buttonPnl.getActionMap();
		amap.put("panel.blue", blueAction);
		amap.put("panel.yellow", yellowAction);
		amap.put("panel.red", redAction);
	}

	public class ColorAction extends AbstractAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public ColorAction(String name, Icon icon, Color c, JButton btn) {
			this.putValue(Action.NAME, name);
			this.putValue(Action.SMALL_ICON, icon);
			this.putValue(Action.SHORT_DESCRIPTION,
					"set panel color to" + name.toLowerCase());
			this.putValue("color", c);
			this.putValue("button", btn);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			Color c = (Color) this.getValue("color");
			buttonPnl.setBackground(c);

			JButton btn = (JButton) this.getValue("button");
			System.out.println("command:" + btn.getActionCommand());
		}
	}

	public static void main(String[] args) {
		new KeyStrokeDemo().setVisible(true);
	}
}
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值