站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题[转贴]J2ME学习记3

[转贴]J2ME学习记3

摘要: [转贴]J2ME学习记3
<tr><td>
http:///tech/article818.html
Ticker对象
发信站: 北大未名站 (2001年10月20日21:14:25 星期六) , 站内信件
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("&ETH;&iexcl;&Acirc;&yen;&Ograve;&raquo;&Ograve;&sup1;
;&Igrave;&yacute;&acute;&ordm;&Oacute;ê");
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程序的运行效果如下图所示:
获取文本框的值
发信站: 北大未名站 (2001年10月21日00:34:19 星期天) , 站内信件
在前面的例子中,我们已经演示了如何构造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对象的getStr
ing()方法,获取文本框中的输入值,追加到容器对象中,最后将此Form对象作为屏幕的当前显
示对象。GetTextBoxValue.java的运行效果如下面两图所示:
Date对象
[]发信站: 北大未名站 (2001年10月21日00:35:20 星期天) , 站内信件
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对象
发信站: 北大未名站 (2001年10月21日00:36:16 星期天) , 站内信件
TimeZone对象也是属于java.util包的。这个对象的作用是提供关于时区的信息。TimeZon
e类有一个静态方法----getDefault(),可以获取与当前系统相关的时区对象。getAvailable
IDs()方法可以获取系统中所有可用的时区的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{;
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;
};
};
GetTimeZone.java程序的运行效果如下图所示:

--
Calendar对象
发信站: 北大未名站 (2001年10月21日00:37:43 星期天) , 站内信件
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//{;
//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;
};
};
GetCalendar.java程序的运行效果如下图所示:

[]--
如何建立HTTP连接
发信站: 北大未名站 (2001年10月21日00:40:04 星期天) , 站内信件
[] 在J2ME程序中,可以利用HttpConnection接口建立HTTP连接,访问远程服务器上的资源。具
体的代码如下所示:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class GetHttpConnection extends MIDlet implements CommandListener
{;
private Display display;
private Form props;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public GetHttpConnection()
{;
display = Display.getDisplay(this);
};
[]public void startApp()
{;
[]props = new Form("Get Http Connection");
//props.append("Hello World!\n");
try
{;
[] HttpConnection conn=(HttpConnection) Connector.open
("http://rainbow:8080/index.html");
//conn.setRequestProperty("user","fancy");
props.append("Date:"+conn.getDate()+"\n");
props.append("Expiration:"+
conn.getExpiration()+"\n");
props.append(conn.getHost()+"\n");
props.append("Last Modified:"+
conn.getLastModified()+"\n");
props.append("Port:"+conn.getPort()+"\n");
props.append("Protocol:"+
conn.getProtocol()+"\n");
props.append("Request Method:"+
[] conn.getRequestMethod()+"\n");
props.append("Response Code:"+
conn.getResponseCode()+"\n");
props.append("Encoding:"+
conn.getEncoding()+"\n");
props.append("Length:"+conn.getLength()+"\n");
props.append("Type:"+conn.getType()+"\n");
props.append("URL:"+conn.getURL()+"\n");
props.append("Response Message:"+
conn.getResponseMessage()+"\n");
};
catch(Exception fe)
{;
props.append("Error:"+fe.getMessage());
};
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;
};
};
Connector类提供了open()方法,可以和各种各样的远程资源建立连接。open()方法的参数
就是远程资源的URL地址。open()方法的返回值是一个Connection接口。为了建立HTTP连接,
我们应该将它强制转换为HttpConnection接口的形式。一旦获取HttpConnection接口的实例
对象,就可以调用HttpConnection接口的各种方法,得到关于HTTP连接的各种信息。
使用J2ME+JavaMail发送电子邮件
发信站: 北大未名站 (2001年10月21日00:41:24 星期天) , 站内信件
我的想法是建立一个Web服务器,使用Tomcat 4.0,支持JSP技术。再配置一个Mail服务器,
使用的软件是Imail 7.0.4。首先编写一个可以发送电子邮件的JSP程序(post.jsp),具体代码
如下所示:
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%!
public class SmtpAuthenticator extends javax.mail.Authenticator
{;
public javax.mail.PasswordAuthentication getPasswordAuthentication()
{;
return new javax.mail.PasswordAuthentication("fancy", "fancy");
};
};
%>
<%
String username="fancyrainbow";
String password="fancy";
String from="fancy@rainbow";
String to="fancy@rainbow";
String cc="fancy@rainbow";
String subject="J2ME Mail Test";
[]//String content="J2ME Mail Test";
String content=request.getParameter("content");
Properties props = System.getProperties();
props.put("mail.smtp.auth","true");
[]props.put("mail.smtp.host","rainbow"); //263
SmtpAuthenticator sa=new SmtpAuthenticator();
Session sess = Session.getInstance(props, sa);
sess.setDebug(true);
Message msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
msg.addRecipients(Message.RecipientType.CC,InternetAddress.parse(cc, false));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(content);
Transport.send(msg);
%>
Send Message OK!
我将post.jsp程序保存在Tomcat 4.0的ROOT目录下面,然后使用Web浏览器测试此程序成功
[]。然后再将上一个J2ME程序(GetHttpConnection.java)改一改,让它与Tomcat 4.0服务器建立
连接,请求post.jsp程序。相关代码如下所示:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class SendMail extends MIDlet implements CommandListener
{;
private Display display;
private Form props;
[]private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public SendMail()
[]{;
display = Display.getDisplay(this);
};
public void startApp()
{;
props = new Form("Get Http Connection");
//props.append("Hello World!\n");
try
{;
HttpConnection conn=(HttpConnection)Connector.open("http://rainbow:8080/post.
jsp?content=Hello World");
//conn.setRequestProperty("user","fancy");
props.append("Date:"+conn.getDate()+"\n");
props.append("Expiration:"+conn.getExpiration()+"\n");
props.append(conn.getHost()+"\n");
[]props.append("Last Modified:"+conn.getLastModified()+"\n");
[]props.append("Port:"+conn.getPort()+"\n");
props.append("Protocol:"+conn.getProtocol()+"\n");
props.append("Request Method:"+conn.getRequestMethod()+"\n");
props.append("Response Code:"+conn.getResponseCode()+"\n");
props.append("Encoding:"+conn.getEncoding()+"\n");
props.append("Length:"+conn.getLength()+"\n");
props.append("Type:"+conn.getType()+"\n");
props.append("URL:"+conn.getURL()+"\n");
props.append("Response Message:"+conn.getResponseMessage()+"\n");
//InputStream is=conn.openInputStream();
//DataInputStream dis=new DataInputStream(is);
//props.append("System Info:"+dis.readUTF()+"\n");
[]props.append("Send Mail OK!"+"\n");
};
catch(Exception fe)
{;
props.append("Error:"+fe.getMessage());
[]};
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;
};
};
启动Tomcat 4.0、Imail 7.0.4、J2MEWTK。在J2MEWTK中运行SendMail.java程序,事后检查
邮箱,发现已经成功的收到了邮件。
[] 有两点需要说明一下:
首先,按照上面的模式,我们可以编写出功能更为强大的程序来,例如使用J2ME+JDBC+JSP访
问远程的数据库系统,访问EJB组件等等。
其次,如果要读取post.jsp程序的输出信息,你可以从HttpConnection接口中获取一个输
入流对象,逐个读取输入流中的数据即可。具体的代码我就不举了,你可以参考J2ME的DOC。

http:///tech/article818.html
</td></tr></table></td> </tr> <tr> <td background="/pic/split.gif" height=1></td> </tr> <tr> <td class="tdMargin1">
↑返回目录
前一篇: J2ME简介
后一篇: [转帖]J2ME学习记2