日期:2014-05-20 浏览次数:20719 次
public class GetKey extends Canvas{ private int fh; private int lastKey = 0; private Display display; private Displayable backScreen; private TextField setKey; public GetKey(TextField theSetKey){ fh = Font.getDefaultFont().getHeight() + 4; setKey = theSetKey; lastKey = Integer.parseInt(setKey.getString()); } public void show(Display theDisplay){ display = theDisplay; backScreen = display.getCurrent(); display.setCurrent(this); } protected void paint(Graphics g) { g.setColor(0); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0xffffff); g.drawString("Press the key:", 2, fh*0, Graphics.TOP|Graphics.LEFT); g.drawString("press < to cancel,", 2, fh*1, Graphics.TOP|Graphics.LEFT); g.drawString("press > to save.", 2, fh*2, Graphics.TOP|Graphics.LEFT); g.setColor(0xff0000); g.drawString(String.valueOf(lastKey), getWidth()/2, getHeight()/2, Graphics.HCENTER|Graphics.TOP); } // 响应按键 protected void keyPressed(int keyCode) { if (keyCode <= 0) { int gameAction = getGameAction(keyCode); if (gameAction != 0) { keyCode = gameAction; } } switch(keyCode){ case Canvas.RIGHT: setKey.setString(String.valueOf(lastKey)); case Canvas.LEFT: display.setCurrent(backScreen); return; default: lastKey = keyCode; } repaint(); serviceRepaints(); } }
public void commandAction(Command cmd, Item item) { if(item == uiMenuKey){ GetKey keyCanvas = new GetKey((TextField)item); keyCanvas.show(controler.getDisplay()); } }