当前位置:早雪网网络学院编程文档Java → J2ME学习札记1

J2ME学习札记1

减小字体 增大字体 作者:不详  来源:supcode.com收集整理  发布时间:2005-7-22 18:42:19
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] [3] [4] 

[数据载入中...] [返回上一页] [打 印]