display.setCurrent(props); }
public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }
public void destroyApp(boolean unconditional) { }
public void pauseApp() { display.setCurrent(null); props = null; } } 使用普通的编辑器编辑好上述文件以后,保存为Poem.java,保存路径为src\fancy\test。然 后在Ktoolbar中编译,一切无误之后,单击Setting按钮,出现一个配置窗口,选择MIDlets面板 ,单击Add按钮,依次输入Poem、fancy.png、fancy.test.Poem三项。单击OK按钮,再单击OK按 钮,关闭配置窗口,回到Ktoolbar的主界面,再次编译。一切无误之后,单击Run按钮运行程序。 Poem的运行效果如下图所示。 注意:每新编写一个程序,都要按照这个步骤进行配置,再编译运行,我以后就不再重复描述 这个步骤了。
请看下面的代码(Prop.java): package fancy.test;
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*;
public class Prop extends MIDlet implements CommandListener { private Display display; private Form props;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public Prop() { display = Display.getDisplay(this); }
public void startApp() { props = new Form("System Properties"); props.append("Hello World!\n"); long time=System.currentTimeMillis(); props.append("current time:"+time+"\n"); props.append("microedition.configuration:"+ System.getProperty("microedition.configuration")+"\n"); props.append("microedition.profiles:"+ System.getProperty("microedition.profiles")+"\n"); props.append("microedition.platform:"+ System.getProperty("microedition.platform")+"\n"); props.append("microedition.locale:"+ System.getProperty("microedition.locale")+"\n"); props.append("microedition.encoding:"+ System.getProperty("microedition.encoding")+"\n"); props.append("java.version:"+System.getProperty("java.version")+"\n");//null props.append("java.vendor:"+System.getProperty("java.vendor")+"\n");//null props.append("java.vm.name:"+System.getProperty("java.vm.name")+"\n");//null props.append("java.vm.version:"+System.getProperty("java.vm.version")+"\n");// null props.append("os.name:"+System.getProperty("os.name")+"\n");//null props.append("os.arch:"+System.getProperty("os.arch")+"\n");//null props.append("os.version:"+System.getProperty("os.version")+"\n");//null props.append("user.name:"+System.getProperty("user.name")+"\n");//null props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); }
public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }
public void destroyApp(boolean unconditional) { }
public void pauseApp() { display.setCurrent(null); props = null; } } 这个程序的作用是输出系统中各个环境属性的值。诀窍是使用System类的getProperty()方 法。请注意,J2ME核心包的System类已经不支持getProperties()方法了,而且很多环境属性都 不再支持了,比如java.version、java.vendor等等。
查看内存利用情况 请看程序(Memory.java): package fancy.test;
import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class Memory extends MIDlet implements CommandListener { private Display display; private Form props; private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public Memory() { display = Display.getDisplay(this); }
public void startApp() { props = new Form("Runtime Information"); long total=Runtime.getRuntime().totalMemory(); long free=Runtime.getRuntime().freeMemory(); props.append("total memory:"+total+"\n"); props.append("free memory:"+free+"\n");
props.addCommand(exitCommand); props.setCommandListener(this); display.setCurrent(props); }
public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }
public void destroyApp(boolean unconditional) { }
public void pauseApp() { display.setCurrent(null); props = null; }
} 这个程序的诀窍是利用Runtime类的totalMemory()方法以及freeMemory()方法。J2ME中的 Runtime类不再具有执行外部程序的功能了,这是很显然的。
List对象 发信站: 北大未名站 (2001年10月20日20:32:00 星期六) , 站内信件
List属于javax.microedition.lcdui包,它和Form一样,同样属于容器类型的对象。属于容 器类型的对象还有TextBox和Alert。我们在下面还会介绍这两个类的用法。此处首先介绍Li st的用法。请看下面的程序(FormList.java): package fancy.test;
import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class FormList extends MIDlet implements CommandListener { private Display display; private List list; private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public FormList() { display = Display.getDisplay(this); }
public void startApp() { list= new List("Choose URL", Choice.EXCLUSIVE); list.append("www.pku.edu.cn",null); list.append("www.yahoo.com",null); list.append("fancyrainbow@263.net",null);
list.addCommand(exitCommand); list.setCommandListener(this); display.setCurrent(list); }
public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } }
public void destroyApp(boolean unconditional) { }
public void pauseApp() { display.setCurrent(null); list = null; }
} 请大家留意startApp()方法的内部: list= new List("Choose URL", Choice.EXCLUSIVE); list.append("www.pku.edu.cn",null); list.append("www.yahoo.com",null); list.append("fancyrainbow@263.net",null);
list.addCommand(exitCommand); list.setCommandListener(this); display.setCurrent(list); 其逻辑流程如下:首先调用构造函数实例化一个List对象(list),List对象实际上代表一个 选择列表。List类的构造函数的第一个参数是选择列表的名字,第二个参数是选择列表的形式 , Choice.EXCLUSIVE表示这个选择列表只能够单选。如果是Choice.MULTIPLE,则表示这个选 择列表可以多选。List类的append()方法有两个参数,第一个参数是选择项的描述,第二个参 数是一个Image对象,代表每个选择项前面的小图标。第二个参数可以是null值,但是第一个参 数是必须的。我们同样可以使用addCommand()方法往List中注册命令,也可以使用setComman dListener()方法指定命令监听者,这和Form是一样的。在startApp()方法的最后,使用Displ ay对象的setCurrent()方法将List对象设定为当前的屏幕显示对象。 FormList.java程序的运行效果如下图所示:
上一页 [1] [2]
|