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程序的運行效果如下圖所示:
發佈了45 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章