clearscreen.java

来自「《精通JAVA手机游戏与应用程序设计》随书光盘」· Java 代码 · 共 81 行

JAVA
81
字号


import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;


public class ClearScreen extends MIDlet implements CommandListener {

	private Command exitCommand;
	private Command showCommand;
	private Command clearCommand;
	private ClearScreenCanvas sg;

	public ClearScreen() {
		exitCommand = new Command("退出", Command.EXIT, 1);
		showCommand = new Command("显示", Command.SCREEN, 1);
		clearCommand = new Command("清除", Command.SCREEN, 1);
		sg = new ClearScreenCanvas();
		sg.addCommand(exitCommand);		
		sg.addCommand(clearCommand);
		sg.setCommandListener(this);
		
		
	}
	protected void startApp(){
		Display.getDisplay(this).setCurrent(sg);
	}

	protected void pauseApp() {
	}

	protected void destroyApp(boolean arg0){		
	}
	public void commandAction(Command c, Displayable d) {
		if (c == exitCommand) {			
			destroyApp(false);			
			notifyDestroyed();
		}
		if (c == showCommand) {			
			sg.show();	
			sg.removeCommand(showCommand);
			sg.addCommand(clearCommand);
		}
		if (c == clearCommand) {		
			sg.clear();
			sg.removeCommand(clearCommand);
			sg.addCommand(showCommand);
			
		}
	}	
}

class ClearScreenCanvas extends Canvas{
	private boolean cleared = false;//cleared 表示被清除了
	protected ClearScreenCanvas(){
		cleared = false;
	}
	protected void paint(Graphics g) {		
		if(!cleared){
			g.setColor(0,0,0);
			g.drawString("Hello World", this.getWidth()/2, this.getHeight()/2,
				Graphics.BOTTOM|Graphics.HCENTER);
		}
		else{
			g.setColor(255,255,255);
			g.fillRect(0,0,getWidth(),getHeight());
			cleared = true;
		}		
	}
	public void clear(){		
		cleared = true;			
		repaint();
	}
	public void show(){
		
		cleared = false;			
		
		repaint();
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?