J2ME學習札記 22-26
發表時間:2024-05-31 來源:明輝站整理相關軟件相關文章人氣:
[摘要](22)-----ChoiceGroup對象 ChoiceGroup也是一個項目類型的對象,它代表一個選擇列表,它的作用和List對象類似,不 過后者是一個容器,而前者是一個項目。 我們需要特別注意ChoiceGroup類的構造函數,它有四個參數,第一個參數是標簽,第二個參 數是此選擇列表的類型,...
(22)-----ChoiceGroup對象
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程序的運行效果如下圖所示:(缺)
(23)-----Gauge對象
Gauge對象是一個項目類型的對象,它的作用是顯示一個進度條。請看下面的源代碼。Gaug
e類的構造函數的后面兩個參數分別是進度條的最大值和初始值。
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;
}
}
ShowGauge.java程序的運行效果如下圖所示:(缺)
(24)-----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("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程序的運行效果如下圖所示:
25)----獲取文本框的值
在前面的例子中,我們已經演示了如何構造J2ME程序的用戶界面。現在有一個問題,那就是
如何與用戶界面交互呢?亦即如何獲取用戶通過用戶界面輸入的值呢?請看下面的例子。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GetTextBoxvalues extends MIDlet implements CommandListener
{
private Display display;
private TextBox txtBox;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command getCommand = new Command("GETvalues", Command.OK, 1);
public GetTextBoxvalues()
{
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 valuesScreen()
{
form props=new form("get text box values");
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)
{
valuesScreen();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
txtBox = null;
}
}
在上面的例子中(GetTextBoxvalues.java),當我們往文本框中輸入文本,并按下退出按鈕,接
著選擇GETvalues命令的時候,將會調用valuesScreen()方法。valuesScreen()方法的源代碼如下
:
public void valuesScreen()
{
form props=new form("get text box values");
props.append(txtBox.getString());
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
valuesScreen()方法的邏輯是:首先創建一個容器對象form,然后調用TextBox對象的getStr
ing()方法,獲取文本框中的輸入值,追加到容器對象中,最后將此form對象作為屏幕的當前顯
示對象。GetTextBoxvalues.java的運行效果如下面兩圖所示:
(26)-----Date對象
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程序的運行效果如下圖所示:
------------------------------------------------------------------------------------
網易廣州社區Java版