📄 midlet1.java
字号:
package prj14_1;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDlet1 extends MIDlet {
private BallCanvas bc = new BallCanvas();
private Display dis;
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(bc);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
class BallCanvas extends Canvas implements Runnable,CommandListener{
private int left = 50;
private int top = 50;
private int d = 100;
private int DIR = 1; //1:向下,2:向上
private Command cmdPause = new Command("暂停",Command.SCREEN,1);
private Command cmdResume = new Command("继续",Command.SCREEN,1);
private Thread th;
private boolean RUN = true;
public BallCanvas(){
this.addCommand(cmdPause);
this.setCommandListener(this);
th = new Thread(this);
th.start();
}
public void commandAction(Command c,Displayable d){
if(c==cmdPause){//按下暂停按钮
this.removeCommand(cmdPause);
this.addCommand(cmdResume);
RUN = false;
th = null;
}
else if(c==cmdResume){//按下继续按钮
this.removeCommand(cmdResume);
this.addCommand(cmdPause);
RUN = true;
th = new Thread(this);
th.start();
}
}
public void paint(Graphics g){
g.setColor(0,255,0);//绿色
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(255,0,0);
g.fillArc(left,top,d,d,0,360);
}
public void run(){
while(RUN){
if(DIR==1){
top+=3;
d--;
if(top>=this.getHeight()-d){
DIR = 2;
}
}
if(DIR==2){
top-=3;
d++;
if(top<=50){
DIR = 1;
}
}
repaint();//重画
try{
Thread.currentThread().sleep(10);
}catch(Exception ex){}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -