程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 我的J2ME編程練習(5)——ImageItem

我的J2ME編程練習(5)——ImageItem

編輯:關於JAVA

/* * imageItemlet.Java * * Created on 2005年4月17日, 下午8:56 */

import javax.microedition.midlet.*;import Javax.microedition.lcdui.*;

/** * * @author Administrator * @version */public class imageItemlet extends MIDlet implements CommandListener,ItemCommandListener{ private Form aForm; private Image sohuImage; private Image neImage; private ImageItem sohuImageItem; private ImageItem neImageItem; private Display aDisplay; private Command exitCommand; private Command connectCommand; private Spacer aSpacer; private Alert anAlert; public imageItemlet() { aForm=new Form("ImageItemTest"); exitCommand=new Command("EXIT",Command.EXIT,1); connectCommand=new Command("Connect",Command.ITEM,2); aSpacer=new Spacer(10,2); anAlert=new Alert("Connecting...","Connecting to www.163.com ...", null,AlertType.INFO); try{ sohuImage=Image.createImage("/sohu.png"); sohuImageItem=new ImageItem(null,sohuImage,ImageItem.LAYOUT_RIGHT, "This is Sohu!"); ASPacer.setLayout(Item.LAYOUT_EXPAND);

//The profram will connect to www.163.com and display the Alert //when click the ImageItem of 163.png .

neImage=Image.createImage("/163.png"); neImageItem=new ImageItem(null,neImage,ImageItem.LAYOUT_RIGHT, "This is 163",Item.HYPERLINK); neImageItem.setItemCommandListener(this); neImageItem.setDefaultCommand(connectCommand); aForm.append(sohuImageItem); aForm.append(ASPacer); aForm.append(neImageItem); aForm.addCommand(exitCommand); aForm.setCommandListener(this); } catch (Exception e){ e.printStackTrace(); } } public void startApp() { aDisplay=Display.getDisplay(this); aDisplay.setCurrent(aForm); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c,Displayable d){ if(c==exitCommand){ destroyApp(false); notifyDestroyed(); } } public void commandAction(Command c,Item i){ if(c==connectCommand){ aDisplay.setCurrent(anAlert,aForm); } } }

這個程序的比較新的地方在於使用了midp2.0中新增加的ITEM類的外觀模式和SPACER類。其中外觀模式的使用在StringItem中已經介紹過了。

public Spacer(int minWidth, int minHeight),SPACER類在本程序中設置為Item.LAYOUT_EXPAND,即填充剩余的空白部分。這樣,運行後可以看到兩個ImageItem對象分別位於左右兩端,布局上好看一些。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved