KeyStroke
来源:互联网 发布:软件外包公司介绍 编辑:程序博客网 时间:2024/05/16 09:56
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class KeyStrokeSample {
private static final String ACTION_KEY = "theAction";
public static void main(String args[]) {
JFrame frame = new JFrame("KeyStroke Sample");
JButton buttonA = new JButton("<html><center>FOCUSED<br>control alt 7");
JButton buttonB = new JButton("<html><center>FOCUS/RELEASE<br>VK_ENTER");
JButton buttonC = new JButton(
"<html><center>ANCESTOR<br>VK_F4+SHIFT_MASK");
JButton buttonD = new JButton("<html><center>WINDOW<br>' '");
// Define ActionListener
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
}
};
KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = buttonA.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = buttonA.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
inputMap = buttonB.getInputMap();
inputMap.put(enter, ACTION_KEY);
buttonB.setActionMap(actionMap);
KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.SHIFT_MASK);
inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(shiftF4, ACTION_KEY);
buttonC.setActionMap(actionMap);
KeyStroke space = KeyStroke.getKeyStroke(' ');
inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(space, ACTION_KEY);
buttonD.setActionMap(actionMap);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(2, 2));
contentPane.add(buttonA);
contentPane.add(buttonB);
contentPane.add(buttonC);
contentPane.add(buttonD);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
- KeyStroke
- Keystroke Monitoring
- KeyStroke类的使用
- Keystroke mappings in WebMatrix
- Win32 Series - Keystroke Messages
- Max Keylogger - #1 Keystroke Logger & Computer Monitoring Software
- GEF中组合按键设置汇总以及org.eclipse.gef.KeyStroke的方法的使用
- Dbviusalizer报错Java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Compar
- 电脑出现"NTLDR is Missing Press any key to restart "如何解决.
- java程序员必读书籍
- 利用 Django admin 完成更多任务
- 民国英雄打酱油集锦——看你知道几个典故
- 虚拟机 Workstation,GSX Server 和ESX之间的区别
- KeyStroke
- c#事件
- 去除链接上的方块css
- UDL的使用
- 视频推荐: Justice with Michael Sandel
- 代码方式添加Myeclipse8.0插件
- Sybase ASE 12.5.2 Develope Edition
- jvm调优总结
- Magento 网站备份