📄 keymidlet.java
字号:
/*
* KeyMidlet.java
*
* Created on 2008年9月9日, 下午6:18
*/
package com.j2medev.sample.chapter3.keyrepeat;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author yqsun
* @version
*/
public class KeyMidlet extends MIDlet implements CommandListener{
private Display display;
private KeyCanvas mainCanvas;
private Command exitCommand = new Command("退出",Command.EXIT,1);
public void startApp() {
if(display==null)
display = Display.getDisplay(this);
mainCanvas = new KeyCanvas();
mainCanvas.addCommand(exitCommand);
mainCanvas.setCommandListener(this);
//start Thread
new Thread(mainCanvas).start();
display.setCurrent(mainCanvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command cmd, Displayable displayable){
if(cmd==exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
class KeyCanvas extends Canvas implements Runnable{
private boolean leftPressed;
private boolean rightPressed;
private boolean stop = false;
public synchronized void keyPressed(int keyCode){
int action = getGameAction(keyCode);
switch(action){
case LEFT:
//left();
leftPressed = true;
break;
case RIGHT:
//right();
rightPressed = true;
break;
default:
break;
}
repaint();
}
public void run(){
while(!stop){
if(leftPressed){
//left();
}
if(rightPressed){
//right();
}
try{
Thread.sleep(50);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void showNotify(){
System.out.println(this.hasPointerEvents());
if(!isShown())
System.out.println("showNotify()在Canvas显示之前被调用");
}
public void hideNotify(){
System.out.println(this.hasPointerEvents());
if(!isShown())
System.out.println("hideNotify在Canvas被删除之后被调用");
stop = true;
}
protected void paint(Graphics g){
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -