lass Hello extends MIDlet implements CommandListener //要继承MIDlet 类 private SCanvas canvas//绘图的画布,绘图都在这个上进行。
private Display display//屏幕管理器 public void startApp //MIDlet 类中定义的方法之一,启动 MIDlet if canvas null displayDisplay.getDisplaythis//得到这个 MIDlet 的屏幕管理器 canvas new SCanvas Command exitCommand new CommandquotExitquot Command.EXIT 0 canvas.addCommandexitCommand canvas.setCommandListenerthis display.setCurrentcanvas//将当前 MIDlet 的屏幕设为 canvas // Start up the canvas canvas.start public void pauseApp //MIDlet 类中定义的方法之一暂停 MIDlet,这里什么都不做。
public void destroyAppboolean unconditional //MIDlet 类中定义的方法之一销毁 MIDlet。
这里什么都不做。
public void commandActionCommand c Displayable s if c.getCommandType Command.EXIT destroyApptrue notifyDestroyed class SCanvas extends Canvas public SCanvas super void start repaint public void paintGraphics g // 清屏 g.setColor0 0 0 // black g.fillRect0 0 getWidth getHeight g.setColor255 255 255 // white int y 0 String screenSize quot屏幕大小: quot Integer.toStringgetWidth quotx quot Integer.toStringgetHeight g.drawStringscreenSize 0 y Graphics.TOP Graphics.LEFT // 总内存和自由内存数 Runtime runtime Runtime.getRuntime y Font.getDefaultFont.getHeight String totalMem quot总的内存: quot Long.toStringruntime.totalMemory/ 1024 quotKBquot g.drawStringtotalMem 0 y Graphics.TOP Graphics.LEFT y Font.getDefaultFont.getHeight String freeMem quot可用内存数: quot Long.toStringruntime.freeMemory/ 1024 quotKBquot g.drawStringfreeMem 0 y Graphics.TOP Graphics.LEFT 将上面的程序以”Hello.java”命令保存在我们刚才建的项目中的 src 目录底下这时我们点图4所示界面中的”Build”按钮”Wireless Toolkit”将自动为我们编译源代码编译成功后生成的相应的文件都会放到项目文件夹Hello 下相应的目录中我们就可以点击图4所示的”Run”按钮启动程序,再点击手机上 launch 按钮运行程序了如图5所示: 我的 J2ME 编程练习(4)——StringItem whoamiindeed / stringItemlet.java Created on 2005 年 4 月 14 日 下午 4:26 / import javax.microedition.midlet. import javax.microedition.lcdui. / author Administrator version /public class stringItemlet extends MIDlet implements CommandListener ItemCommandListener private Form aform private Command okCommand private Command exitCommand private Command hllinkCommand private Command bCommand private Display aDisplay private StringItem hlstringItem private StringItem bstringItem private Alert hlAlert private Alert bAlert public stringItemlet okCommandnew CommandquotOKquotCommand.OK1 exitCommandnew CommandquotEXITquotCommand.EXIT1 hllinkCommandnew CommandquotLINKquotCommand.ITEM2 bCommandnew CommandquotBUTTONquotCommand.ITEM2 aformnew FormquotStringItemTestquot //if click hyperlink quotherequotdisplay anAlert hlstringItemnew StringItemnullquotherequotItem.HYPERLINK hlstringItem.setItemCommandListenerthis hlstringItem.setDefaultCommandhllinkCommand bstringItemnew StringItemnullquotAvailablequotItem.BUTTON bstringItem.setItemCommandListenerthis bstringItem.setDefaultCommandbCommandhlAlertnew AlertquotItem.HYPERLINKquotquotYou Can Call Me 800-8101234quot nullAlertType.INFO bAlertnew AlertquotItem.ButtonquotquotThe Button is Availablequot nullAlertType.INFO aform.appendquotAny question please click quot aform.appendhlstringItem aform.appendbstringItem aform.addCommandokCommand aform.addCommandexitCommand aform.setCommandListenerthis public void startApp aDisplayDisplay.getDisplaythis aDisplay.setCurrentaform public void pauseApp public void destroyAppboolean unconditional public void commandActionCommand c Displayable d ifcexitCommand destroyAppfalse notifyDestroyed else public void commandActionCommand cItem i ifchllinkCommand aDisplay.setCurrenthlAlertaform else ifcbCommand aDisplay.setCurrentbAlertaform 这个程序如果说有什么比较新的东西的话,那就在于运用了 StringItem 的外观模式:HYPERLINK 和BUTTON。
由此也使用了 ItemCommandListener 接口,实现了 commandActionCommand c Item i方法。
这个方法的方法体的写法和 commandActionCommand c Displayable d很类似,这一点可以从程序中看出来。
需要说明的是,commandActionCommand c Item i方法也可以使用 Item 变量 i 进行选择,例如: if ioneItem if coneCommand ……// the program else if iotherItem if chttp://www.java-
asp.net/java/200512/otherCommand …… /the program 此外,该程序在编写时,忘记写 aform.setCommandListenerthis语句了,致使 EXIT 按钮按下后无法退 出,这件事提醒我,编程序时要细心! 一个简单的 CustomItem 使用案例import javax.microedition.midlet.import javax.microedition.lcdui.public class SimpleItemMIDlet extends MIDlet implements CommandListener public void startApp Form form new FormquotSimpleItemMIDletquot form.appendnew SimpleItemquotSimpleItemquot Command c new CommandquotExitquot Command.EXIT 0 form.addCommandc form.setCommandListenerthis Display.getDisplaythis.setCurrentform public void pauseApp public void destroyAppboolean unconditional public void commandActionCommand c Displayable s if c.getCommandType Command.EXIT notifyDestroyed class SimpleItem extends CustomItem public SimpleItemString title supertitle // CustomItem abstract methods. public int getMinContentWidth return 100 public int getMinContentHeight return 60 public int getPrefContentWidthint width return getMinContentWidth public int getPrefContentHeightint height return getMinContentHeight public void paintGraphics g int w int h g.drawRect0 0 w - 1 h - 1 g.setColor0x000000ff int offset 0 for int y 4 y lt h y 12 offset offset 12 24 for int x 4 x lt w x 24 g.fillTrianglex offset y x offset - 3 y 6 x offset 3 y 6 进度条使用实例一、进度条类 Gauge 控件就是一个进度条,通过图形的方式表达一个程序运行的进度或其它操作的运行过程,Gauge 的构造函数如下:GaugeString labelboolean interactiveint maxValueint initialValue 其中,参数quotlabelquot指定了控件的标题,参数quotinteractivequot指定了控件的类型,如果设置为 true则表示为交互类型(用于音量控制等),如设置为 false则为不交互类型,只能通过程序控制进度条。
参数quotmaxValuequot指定了进度条的最大值,参数 initialValue指定了进度条的初始值。
import javax.microedition.lcdui.public class GaugeThread extends Gauge implements Runnable private int maxValue 10 private int add 1 private boolean done false public GaugeThreadString label int maxValue int initialValue superlabel false maxValue initialValue this.maxValue maxValue new Threadthis.start public void run while done int newValue getValue add if newValue maxValue add -1 .
上一篇:
【精品】计算机基础与应用实验教学中心 实验项目一览表
下一篇:
山东省节能减排政府规制长效机制的构建策略