免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4173 | 回复: 1
打印 上一主题 下一主题

Java 里怎么捕捉按键事件?   [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-01-15 08:10 |只看该作者 |倒序浏览
版友 flygp 提供:
1: import java.awt.Graphics;
2: import java.awt.Event;
3: import java.awt.Font;
4:
5: public class Keys extends java.applet.Applet {
6:
7: char currkey;
8: int currx;
9: int curry;
10:
11: public void init() {
12: currx = (this.size().width / 2) -8; // default
13: curry = (this.size().height / 2) -16;
14:
15: setBackground(Color.white);
16: setFont(new Font(“Helvetica”,Font.BOLD,36));
17: }
18:
19: public boolean keyDown(Event evt, int key) {
20: switch (key) {
21: case Event.DOWN:
22: curry += 5;
23: break;
24: case Event.UP:
25: curry -= 5;
26: break;
27: case Event.LEFT:
28: currx -= 5;
29: break;
30: case Event.RIGHT:
31: currx += 5;
32: break;
33: default:
34: currkey = (char)key;
35: }
36:
37: repaint();
38: return true;
39: }
40:
41: public void paint(Graphics g) {
42: if (currkey != 0) {
43: g.drawString(String.valueOf(currkey), currx,curry);
44: }
45: }
46: }

另:
java Tutorial: Run KeyEventDemo
http://java.sun.com/docs/books/tutorial/uiswing/events/KeyEventDemo.html
source code
http://java.sun.com/docs/books/tutorial/uiswing/events/example-swing/KeyEventDemo.java

:)

论坛徽章:
0
2 [报告]
发表于 2003-01-15 08:55 |只看该作者

Java 里怎么捕捉按键事件?  

下面提供一些思路,偶自己整理的:

Key Pressed ------>;

Generate KeyStroke Object ------>;

Search in the InputMap for this KeyStroke Object ------>;

Succeed in searching this KeyStrokey Object,
then return a corresponding object (usually a String) ------>;

Search in the ActionMap for this corressponding object(String) ------>;

Succeed in searching the Action ------>;

Execute the method:actionPerformed of the Action by jvm ------>;

The End.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP