Ticker对象 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("С¥һҹ ;Ìý´ºÓê"); 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对象的getString()方法,获取文本框中的输入值,追加到容器对象中,最后将此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包,它可以提供更为详尽的时间信息。具体的例子如下所示
|