J2ME学习笔记(基础7)

在前面的例子中,我们已经演示了如何构造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;
}
}
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 +"\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+"\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程序的运行效果如下图所示: <!--v:3.2-->

你可能感兴趣的:(C++,c,C#)