J2ME學習筆記(基礎6)

ImageItem對象是一個項目類型的對象,他的作用是在容器中顯示圖片。那麼如何使用ImageItem對象呢?請按照下面三個步驟進行:
1.構造一個Image對象,相關代碼如下所示:
Image img=Image.createImage("/fancy/test/JavaPowered-8.png");
createImage()方法是Image類的靜態方法,它的作用是根據圖形文件創建一個Image對象。
J2ME程序中所用到的圖片文件必須存放在apps/fancy/res文件夾下面。
2.構造ImageItem對象,相關代碼如下所示:
imgItem=new ImageItem("Default Layout",img,ImageItem.LAYOUT_DEFAULT,
"Image Cannot be shown");

ImageItem類的構造函數有三個參數,第一個參數的作用是顯示一個標籤,第二個參數指明圖片的對齊方式,第三個參數的作用是顯示圖片的tip。
3.利用容器類對象的append()方法將ImageItem對象添加進去。如:
props.append(imgItem);
下面我們來看一個比較完整的例子。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ShowImageItem extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private Image img;
private ImageItem imgItem;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public ShowImageItem()

{
display = Display.getDisplay(this);
}
public void startApp()
{
props = new Form("Hello World");
//props.append("Hello World!/n");
try
{
img=Image.createImage("/fancy/test/JavaPowered-8.png");
imgItem=new ImageItem("Default Layout",
img,ImageItem.LAYOUT_DEFAULT,"Image Cannot be
shown");
props.append(imgItem);
props.append(new ImageItem("Left Layout",
img,ImageItem.LAYOUT_LEFT,"Image Cannot be
shown"));
props.append(new ImageItem("Center Layout",
img,ImageItem.LAYOUT_CENTER,"Image Cannot be
shown"));
}

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;
}
}
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;
}
}
Gauge對象是一個項目類型的對象,它的作用是顯示一個進度條。請看下面的源代碼。
Gauge類的構造函數的後面兩個參數分別是進度條的最大值和初始值。
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;
}
}
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;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章