程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 實用的問題:Java 裡捕捉按鍵事件

實用的問題:Java 裡捕捉按鍵事件

編輯:關於JAVA
實用的問題:Java 裡捕捉按鍵事件 

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

:)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved