java canvas 用法,我的J2ME编程练习(8)——Canvas3-JSP教程,Java基础

/*

* canvas3let.java

*

* created on 2005年4月20日, 下午3:55

*/

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

/**

*

* @author  administrator

* @version

*/

public class canvas3let extends midlet implements commandlistener{

private display adisplay;

private command okcommand;

private command exitcommand;

private command backcommand;

private textbox atextbox;

private alert showalert;

private image showimage;

private mycanvas amycanvas;

public canvas3let(){

exitcommand=new command(“退出”,command.exit,1);

okcommand=new command(“进入”,command.ok,1);

try{

showimage=image.createimage(“/sohu.png”);

}

catch(exception e){}

showalert=new alert(“loading…”,”载入中……”,showimage,alerttype.info);

atextbox=new textbox(“keypress test”,

“按下进入键后,即可进入显示界面,可以显示所按按键”,

25,textfield.any);

atextbox.addcommand(exitcommand);

atextbox.addcommand(okcommand);

atextbox.setcommandlistener(this);

}

public void startapp() {

adisplay=display.getdisplay(this);

adisplay.setcurrent(atextbox);

}

public void pauseapp() {

}

public void destroyapp(boolean unconditional) {

}

public void commandaction(command c , displayable d){

if(c==exitcommand){

destroyapp(false);

notifydestroyed();

}

else if (c==okcommand){

amycanvas=new mycanvas();

adisplay.setcurrent(amycanvas);

}

}

public class mycanvas extends canvas implements commandlistener{

string keyname=””;

public mycanvas(){

backcommand=new command(“后退”,command.exit,1);

addcommand(backcommand);

setcommandlistener(this);

}

public void paint(graphics g){

//清除屏幕

g.setcolor(0xffffff);

g.fillrect(0,0,getwidth(),getheight());

//设置字体颜色并画出所按键的名称

g.setcolor(0);

g.drawstring(keyname,100,100,graphics.left|graphics.bottom);

}

public void commandaction(command c , displayable d){

if (c==backcommand){

hidenotify();

//adisplay.setcurrent(atextbox);

}

}

public void keypressed(int keycode){

keyname=getkeyname(keycode);

repaint();

}

protected void hidenotify(){

adisplay.setcurrent(showalert,atextbox);

}

}

}

通过这个程序,主要练习了keypress的用法。此外,还有hidenotify()的用法,但shownotify()的用法还不是很清楚。

hidenotify()是在画布被从显示屏移除后,实现立刻调用该方法。

shonotify()是在画布显示在显示屏之前系统将先调用该方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值