import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ListDemo extends MIDlet implements CommandListener
{
private Command exitCommand;
private Command confirmCommand;
private Display display = null;
private List mainList;
private List ExclusiveList;
private List ImplicitList;
private List MultipleList;
private static String[] stringArray;
private static Image[] imageArray1;
private static Image[] imageArray2;
public ListDemo() //屬性、元件、初值設定(初始化)
{
display = Display.getDisplay(this);
exitCommand = new Command("離開", Command.EXIT, 1);
confirmCommand = new Command("確認", Command.OK, 1);
try
{
imageArray1 = new Image[]{ Image.createImage("/App.png"),
Image.createImage("/Auction.png"),
Image.createImage("/ColorChooser.png")
};
}
catch(java.io.IOException err)
{
System.out.println("error");
}
try
{
imageArray2 = new Image[]{ Image.createImage("/App.png"),
Image.createImage("/Auction.png"),
Image.createImage("/ColorChooser.png"),
Image.createImage("/ManyBalls.png")
};
}
catch(java.io.IOException err)
{
System.out.println("error");
}
}
public void startApp() // 程式進入點
{
stringArray = new String[]{"Exclusive", "Implicit", "Multiple"};
mainList = new List("主選單", List.EXCLUSIVE, stringArray, imageArray1);
mainList.addCommand( exitCommand );
mainList.addCommand( confirmCommand );
mainList.setCommandListener( this );
display.setCurrent( mainList );
}
public void pauseApp(){ }
public void destroyApp(boolean unconditional){ }
public void commandAction(Command c, Displayable s) // 各事件處理函數
{
if (display.getCurrent() == mainList)
{
if(c == confirmCommand)
{
switch( mainList.getSelectedIndex() )
{
case 0:
{
stringArray = new String[]{"高中", "大學", "研究所"};
ExclusiveList = new List("學歷", List.EXCLUSIVE, stringArray, imageArray1);
ExclusiveList.addCommand(exitCommand);
ExclusiveList.addCommand(confirmCommand);
ExclusiveList.setCommandListener(this);
display.setCurrent( ExclusiveList );
} break;
case 1:
{
stringArray= new String[]{"初級", "中級", "進階", "專家"};
ImplicitList = new List("遊戲等級", List.IMPLICIT, stringArray, imageArray2);
ImplicitList.addCommand(exitCommand);
ImplicitList.setCommandListener(this);
display.setCurrent( ImplicitList );
} break;
case 2:
{
stringArray= new String[]{"打球", "看電影", "上網", "逛街"};
MultipleList = new List("興趣", List.MULTIPLE, stringArray, imageArray2);
MultipleList.addCommand(exitCommand);
MultipleList.addCommand(confirmCommand);
MultipleList.setCommandListener(this);
display.setCurrent( MultipleList );
} break;
}
}
if(c==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
else
{
if(c == confirmCommand)
{
if (display.getCurrent() == ExclusiveList)
{
String showtext = "你的學歷是:";
StringItem showSel;
Form f = new Form("最終結果");
showtext = showtext +" " + ExclusiveList.getString(ExclusiveList.getSelectedIndex());
showSel = new StringItem("", showtext);
f.append( showSel );
f.addCommand(exitCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
if (display.getCurrent() == MultipleList)
{
String showtext = "你的興趣是:";
StringItem showSel;
Form f = new Form("最終結果");
for(int n= 0; n
{
if( MultipleList.isSelected( n) )
{
showtext = showtext+"n*"+MultipleList.getString(n);
}
}
showSel = new StringItem("", showtext);
f.append( showSel );
f.addCommand(exitCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
}
if( c==List.SELECT_COMMAND)
{
String showtext = "你選擇的遊戲等級是:";
StringItem showSel;
Form f = new Form("最終結果");
showtext = showtext +" " + ImplicitList.getString(ImplicitList.getSelectedIndex());
showSel = new StringItem("", showtext );
f.append( showSel );
f.addCommand(exitCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
if(c==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
}
}
文章定位: