当前位置: 首页 > 图文教程 > 网络编程 > JSP > J2ME学习札记(三)

JSP
Servlet及JSP中的多线程同步问题
使用Ant和Tomcat创建Web应用
如何直接在浏览器内运行SQL命令
Servlet、Jsp中的多国语言显示
html与jsp开发分离技术
通过Jsp发送动态图像
Servlets和JSP Pages最佳实践
学习在JSP中使用JavaBeans
JSP显示内容缓存技巧
应用JDOM处理数据库到XML转换的JSP实现
JSP中tomcat的SQL Server2000数据库连接池的配置
用JSTL实现JSP应用程序快速开发
浅谈4种类型的JDBC驱动程序
怎样设置 JSP 的虚拟目录
Java 中对文件的读写操作之比较
javamail在jsp中调用
jsp中任意文字转Unicode的通用模块
JSP与SQL SERVER的留言本
jspSmartUpload上传下载全攻略
Tomcat5.x中的虚拟主机配置方法

JSP 中的 J2ME学习札记(三)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 50 ::
收藏到网摘: n/a


ImageItem对象是一个项目类型的对象,他的作用是在容器中显示图片。那么如何使用ImageItem对象呢?请按照下面三个步骤进行:
1.构造一个Image对象,相关代码如下所示:
Image img=Image.createImage("/fancy/test/JavaPowered-8.png");
createImage()方法是Image类的静态方法,它的作用是根据图形文件创建一个Image对象。
J2ME程序中所用到的图片文件必须存放在apps\fancy\res文件夹下面。
2.构造ImageItem对象,相关代码如下所示:
imgItem=new ImageItem("Default Layout",img,ImageItem.LAYOUT_DEFAULT,
"Image Cannot be shown");


ImageItem类的构造函数有三个参数,第一个参数的作用是显示一个标签,第二个参数指明图片的对齐方式,第三个参数的作用是显示图片的tip。
3.利用容器类对象的append()方法将ImageItem对象添加进去。如:
props.append(imgItem);
下面我们来看一个比较完整的例子。
package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ShowImageItem extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private Image img;
private ImageItem imgItem;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);

public ShowImageItem()


{
display = Display.getDisplay(this);
}

public void startApp()
{
props = new Form("Hello World");
//props.append("Hello World!\n");
try
{
img=Image.createImage("/fancy/test/JavaPowered-8.png");
imgItem=new ImageItem("Default Layout",
img,ImageItem.LAYOUT_DEFAULT,"Image Cannot be
shown");
props.append(imgItem);
props.append(new ImageItem("Left Layout",
img,ImageItem.LAYOUT_LEFT,"Image Cannot be
shown"));
props.append(new ImageItem("Center Layout",
img,ImageItem.LAYOUT_CENTER,"Image Cannot be
shown"));
}


catch(Exception fe)
{
//to do nothing
}

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;
}

}
ShowImageItem.java程序的运行效果如下图所示:

ChoiceGroup也是一个项目类型的对象,它代表一个选择列表,它的作用和List对象类似,不过后者是一个容器,而前者是一个项目。
我们需要特别注意ChoiceGroup类的构造函数,它有四个参数,第一个参数是标签,第二个参数是此选择列表的类型,例如多选还是单选。第三个参数是一个字符串数组,代表每个选项的标签,第四个选项是一个Image类型的数组,代表每个选项前面的小图标。下面是一个比较完整的例子。
package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ShowChoiceGroup extends MIDlet implements CommandListener

{
private Display display;
private Form props;


private Image duke;
private Image[] imageArray;
private ChoiceGroup choice;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);

public ShowChoiceGroup()
{
display = Display.getDisplay(this);
}

public void startApp()
{
props = new Form("Hello World");
//props.append("Hello World!\n");
try
{
Image duke= Image.createImage("/fancy/test/Icon.png");
imageArray = new Image[]{duke,duke,duke};
String[] stringArray = { "Option A", "Option B",
"Option C" };
choice=new ChoiceGroup("choice group",


ChoiceGroup.MULTIPLE,stringArray,imageArray);
props.append(choice);
}
catch(Exception fe)
{
//to do nothing.
}
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;
}
}
ShowChoiceGroup.java程序的运行效果如下图所示:


Gauge对象是一个项目类型的对象,它的作用是显示一个进度条。请看下面的源代码。
Gauge类的构造函数的后面两个参数分别是进度条的最大值和初始值。
package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ShowGauge extends MIDlet implements CommandListener
{
private Display display;
private Form props;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);

public ShowGauge()
{
display = Display.getDisplay(this);
}

public void startApp()
{


props = new Form("Hello World");
//props.append("Hello World!\n");
Gauge gauge=new Gauge("show gauge",true,100,50);
props.append(gauge);
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;
}

}
Ticker对象是一个项目类型的对象,它的作用相当于一个滚动消息栏,在屏幕的上方显示滚动的信息。 Ticker类的构造函数仅有一个参数,那就是需要滚动显示的消息。
package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ShowTicker extends MIDlet implements CommandListener
{
private Display display;
private Form props;


private Command exitCommand = new Command("Exit", Command.EXIT, 1);

public ShowTicker()
{
display = Display.getDisplay(this);
}

public void startApp()
{
props = new Form("Hello World");


props.append("Hello World!\n");
Ticker ticker=new Ticker("D??¥ò?ò1
;ìy′oóê");
props.setTicker(ticker);
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;
}

}
ShowTicker.java程序的运行效果如下图所示:


在前面的例子中,我们已经演示了如何构造J2ME程序的用户界面。现在有一个问题,那就是如何与用户界面交互呢?亦即如何获取用户通过用户界面输入的值呢?请看下面的例子。
package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class GetTextBoxValue extends MIDlet implements CommandListener

{
private Display display;
private TextBox txtBox;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command getCommand = new Command("GETVALUE", Command.OK, 1);

public GetTextBoxValue()
{
display = Display.getDisplay(this);
}



public void startApp()
{
//or :
//String str="hello world";
//txtBox = new TextBox("Text Box",str,str.length(),0);
//the follow code is wrong:
//txtBox = new TextBox("Text Box",str,any number here,0);

txtBox = new TextBox("Text Box",null,200,0);

txtBox.addCommand(exitCommand);
txtBox.addCommand(getCommand);
txtBox.setCommandListener(this);
display.setCurrent(txtBox);
}

public void valueScreen()
{
Form props=new Form("get text box value");
props.append(txtBox.getString());
props.addCommand(exitCommand);
props.setCommandListener(this);


display.setCurrent(props);
}

public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
if(c==getCommand)
{
valueScreen();
}
}

public void destroyApp(boolean unconditional)
{
}

public void pauseApp()
{


display.setCurrent(null);
txtBox = null;
}

}
在上面的例子中(GetTextBoxValue.java),当我们往文本框中输入文本,并按下退出按钮,接着选择GETVALUE命令的时候,将会调用valueScreen()方法。valueScreen()方法的源代码如下
:
public void valueScreen()
{
Form props=new Form("get text box value");
props.append(txtBox.getString());
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
valueScreen()方法的逻辑是:首先创建一个容器对象Form,然后调用TextBox对象的getString()方法,获取文本框中的输入值,追加到容器对象中,最后将此Form对象作为屏幕的当前显示对象。GetTextBoxValue.java的运行效果如下面两图所示:

Date对象是属于java.util包的,它的作用是返回当前的时间。请看下面的代码:

package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


import java.util.*;

public class GetDate extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private Date date;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);

public GetDate()
{
display = Display.getDisplay(this);
}

public void startApp()
{
props = new Form("Hello World");
props.append("Hello World!\n");
date=new Date();
props.append("Now Time:"+date.getTime()+"\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;


}

}
GetDate.java程序的运行效果如下图所示:


TimeZone对象也是属于java.util包的。这个对象的作用是提供关于时区的信息。TimeZone类有一个静态方法----getDefault(),可以获取与当前系统相关的时区对象。getAvailableIDs()方法可以获取系统中所有可用的时区的ID号,getID()方法可以获取系统当前所设置的时
区。具体的例子如下所示:
package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class GetTimeZone extends MIDlet implements CommandListener
{
private Display display;
private Form props;
//private Date date;
private TimeZone zone;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);

public GetTimeZone()
{
display = Display.getDisplay(this);
}



public void startApp()
{
props = new Form("Hello World");
props.append("Hello World!\n");
//date=new Date();
//props.append("Now Time:"+date.getTime()+"\n");
zone=TimeZone.getDefault();
String []zoneid=zone.getAvailableIDs();
for(int i=0;i<zoneid.length;i++)
{
props.append(zoneid[i]+"\n");
}
props.append("Current Time Zone:"+zone.getID()+"\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;
}

}


Calendar对象归属于java.util包,它可以提供更为详尽的时间信息。具体的例子如下所示:
package fancy.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class GetCalendar extends MIDlet implements CommandListener
{
private Display display;
private Form props;
//private Date date;


//private TimeZone zone;
//private Calendar calendar;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);

public GetCalendar()
{
display = Display.getDisplay(this);
}

public void startApp()
{
props = new Form("Hello World");
props.append("Hello World!\n");

Calendar rightNow = Calendar.getInstance();
props.append("YEAR:"+rightNow.get(Calendar.YEAR)+"\n");
props.append("MONTH:"+rightNow.get(Calendar.MONTH)+"\n");
props.append("DAY OF MONTH:"
+rightNow.get(Calendar.DAY_OF_MONTH)+"\n");
props.append("HOUR OF DAY:"
+rightNow.get(Calendar.HOUR_OF_DAY)+"\n");


props.append("MINUTE:"
+rightNow.get(Calendar.MINUTE)+"\n");
props.append("SECOND:"
+rightNow.get(Calendar.SECOND)+"\n");
props.append("MILLISECOND:"
+rightNow.get(Calendar.MILLISECOND)+"\n");

//date=new Date();
//props.append("Now Time:"+date.getTime()+"\n");
//zone=TimeZone.getDefault();
//String []zoneid=zone.getAvailableIDs();
//for(int i=0;i<zoneid.length;i++)
//{
// props.append(zoneid[i]+"\n");
/