以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——显示按键信息  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=556)

--  作者:admin
--  发布时间:2008/10/8 22:51:43
--  程序代码——显示按键信息

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
        private Display display;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {

                display.setCurrent(new DrawPanel());
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }
}

class DrawPanel extends Canvas {
        String str = "请按键!";

        public void paint(Graphics g) {
                g.setColor(255, 255, 255);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(0, 0, 0);
                Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
                                Font.SIZE_LARGE);
                g.setFont(font);
                int fontWidth = font.stringWidth(str);
                int fontHeight = font.getHeight();
                g.drawString(str, getWidth() / 2 - fontWidth / 2, getHeight() / 2
                                - fontHeight / 2, Graphics.TOP | Graphics.LEFT);
        }

        protected void keyPressed(int keyCode) {
                if (keyCode > Canvas.KEY_POUND && keyCode < Canvas.KEY_NUM9)
                        str = "按动的键为:" + (char) keyCode;
                else {
                        switch (getGameAction(keyCode)) {
                        case Canvas.UP:
                                str = "按动的键为:向上";
                                break;
                        case Canvas.DOWN:
                                str = "按动的键为:向下";
                                break;
                        case Canvas.LEFT:
                                str = "按动的键为:向左";
                                break;
                        case Canvas.RIGHT:
                                str = "按动的键为:向右";
                                break;
                        case Canvas.FIRE:
                                str = "按动的键为:开火";
                                break;
                        case Canvas.GAME_A:
                                str = "按动的键为:GAME_A";
                                break;
                        case Canvas.GAME_B:
                                str = "按动的键为:GAME_B";
                                break;
                        case Canvas.GAME_C:
                                str = "按动的键为:GAME_C";
                                break;
                        case Canvas.GAME_D:
                                str = "按动的键为:GAME_D";
                                break;
                        default:
                                str = "其他的键";
                        }
                }
                repaint();
        }

        protected void keyReleased(int keyCode) {
        }
}

[此贴子已经被作者于2010-12-12 18:34:58编辑过]