repainttest.java

来自「java教程第八章事例」· Java 代码 · 共 84 行

JAVA
84
字号
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class RepaintTest extends MIDlet implements CommandListener{

	Display display;
	MyCanvas myCanvas;
	Form form;
	Command exitCmd;
	Command changeCmd;

	public RepaintTest(){
		display = Display.getDisplay(this);
		exitCmd = new Command("退出", Command.EXIT, 1);
		changeCmd = new Command("更换", Command.SCREEN, 1);
		myCanvas = new MyCanvas();
		form = new Form("另一个Displayable");
		form.append("Hide Canvas!");
		form.addCommand(exitCmd);
		form.addCommand(changeCmd);
		form.setCommandListener(this);
	}


	public void startApp(){
		display.setCurrent(form);
	}

	public void pauseApp(){

	}

	public void destroyApp(boolean unconditional){

	}

	public void commandAction(Command c, Displayable d){

		if(c == exitCmd){
			destroyApp(true);
			notifyDestroyed();
		}
		else if(c == changeCmd && d == form){
			display.setCurrent(myCanvas);
		}
	}


	class MyCanvas extends Canvas implements CommandListener{

		Command repaintCmd;

		public MyCanvas(){
			repaintCmd = new Command("重画", Command.SCREEN, 1);
			addCommand(changeCmd);
			addCommand(repaintCmd);
			setCommandListener(this);
		}

		public void paint(Graphics g){
			g.setColor(0xFFFFFF);
			g.fillRect(0, 0, getWidth(), getHeight());
			System.out.println("paint() is called!");
		}

		public void showNotify(){
			System.out.println("showNotify() is called!");
		}

		public void hideNotify(){
			System.out.println("hideNotify() is called!");
		}

		public void commandAction(Command c, Displayable d){

			if(c == changeCmd && d == this){
				display.setCurrent(form);
			}
			else if(c == repaintCmd){
				repaint();
			}
		}
	}
}

⌨️ 快捷键说明

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