明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

J2ME學(xué)習(xí)札記 22-26

[摘要](22)-----ChoiceGroup對(duì)象 ChoiceGroup也是一個(gè)項(xiàng)目類型的對(duì)象,它代表一個(gè)選擇列表,它的作用和List對(duì)象類似,不 過后者是一個(gè)容器,而前者是一個(gè)項(xiàng)目。 我們需要特別注意ChoiceGroup類的構(gòu)造函數(shù),它有四個(gè)參數(shù),第一個(gè)參數(shù)是標(biāo)簽,第二個(gè)參 數(shù)是此選擇列表的類型,...
(22)-----ChoiceGroup對(duì)象
ChoiceGroup也是一個(gè)項(xiàng)目類型的對(duì)象,它代表一個(gè)選擇列表,它的作用和List對(duì)象類似,不
過后者是一個(gè)容器,而前者是一個(gè)項(xiàng)目。
我們需要特別注意ChoiceGroup類的構(gòu)造函數(shù),它有四個(gè)參數(shù),第一個(gè)參數(shù)是標(biāo)簽,第二個(gè)參
數(shù)是此選擇列表的類型,例如多選還是單選。第三個(gè)參數(shù)是一個(gè)字符串?dāng)?shù)組,代表每個(gè)選項(xiàng)的
標(biāo)簽,第四個(gè)選項(xiàng)是一個(gè)Image類型的數(shù)組,代表每個(gè)選項(xiàng)前面的小圖標(biāo)。下面是一個(gè)比較完整
的例子。
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程序的運(yùn)行效果如下圖所示:(缺)

(23)-----Gauge對(duì)象
Gauge對(duì)象是一個(gè)項(xiàng)目類型的對(duì)象,它的作用是顯示一個(gè)進(jìn)度條。請(qǐng)看下面的源代碼。Gaug
e類的構(gòu)造函數(shù)的后面兩個(gè)參數(shù)分別是進(jìn)度條的最大值和初始值。
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程序的運(yùn)行效果如下圖所示:(缺)

(24)-----Ticker對(duì)象
Ticker對(duì)象是一個(gè)項(xiàng)目類型的對(duì)象,它的作用相當(dāng)于一個(gè)滾動(dòng)消息欄,在屏幕的上方顯示滾
動(dòng)的信息。 Ticker類的構(gòu)造函數(shù)僅有一個(gè)參數(shù),那就是需要滾動(dòng)顯示的消息。
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程序的運(yùn)行效果如下圖所示:

25)----獲取文本框的值

在前面的例子中,我們已經(jīng)演示了如何構(gòu)造J2ME程序的用戶界面。現(xiàn)在有一個(gè)問題,那就是
如何與用戶界面交互呢?亦即如何獲取用戶通過用戶界面輸入的值呢?請(qǐng)看下面的例子。
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),當(dāng)我們往文本框中輸入文本,并按下退出按鈕,接
著選擇GETvalues命令的時(shí)候,將會(huì)調(diào)用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()方法的邏輯是:首先創(chuàng)建一個(gè)容器對(duì)象form,然后調(diào)用TextBox對(duì)象的getStr
ing()方法,獲取文本框中的輸入值,追加到容器對(duì)象中,最后將此form對(duì)象作為屏幕的當(dāng)前顯
示對(duì)象。GetTextBoxvalues.java的運(yùn)行效果如下面兩圖所示:

(26)-----Date對(duì)象

Date對(duì)象是屬于java.util包的,它的作用是返回當(dāng)前的時(shí)間。請(qǐng)看下面的代碼:
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程序的運(yùn)行效果如下圖所示:


------------------------------------------------------------------------------------
網(wǎng)易廣州社區(qū)Java版




相關(guān)文章