📄 listmidlet.java
字号:
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
// A first MIDlet with simple text and a few commands.
public class ListMIDlet extends MIDlet
implements CommandListener{
TextBox main;
//The exit, info, and buy commands
private Command exitCommand;
private Command exclusiveCommand;
private Command multipleCommand;
private Command implicitCommand;
private Command confirmCommand;
private List aEXCLUSIVEList;
private List aMULTIPLEList;
private List aIMPLICITList;
String[] position ={"程序设计师","系统分析师","项目经理"};
String[] hobby = {"阅读","听音乐","看电影","闲聊","逛街"};
Image[] positionIcon = {createImage("/Sokoban.png"),
createImage("/Tiles.png"),
createImage("/ManyBalls.png")};
Image[] hobbyIcon = {createImage("/StarCruiser.png"),
createImage("/Stock.png"),
createImage("/ManyBalls.png"),
createImage("/Tiles.png"),
createImage("/Sokoban.png")};
//The display for this MIDlet
private Display display;
private String currentScreen = "";
public ListMIDlet() {
display = Display.getDisplay(this);
exitCommand = new Command("离开", Command.SCREEN, 1);
exclusiveCommand = new Command("职业",Command.SCREEN, 1);
multipleCommand = new Command("兴趣",Command.SCREEN, 1);
implicitCommand = new Command("菜单",Command.SCREEN, 1);
confirmCommand = new Command("确认",Command.SCREEN,2);
}
// Start the MIDlet by creating the TextBox and
// associating the exit command and listener.
public void startApp() {
main=new TextBox("主画面","请按软键选择要运行的目录",256,TextField.ANY);
main.addCommand(exitCommand);
main.addCommand(exclusiveCommand);
main.addCommand(multipleCommand);
main.addCommand(implicitCommand);
main.setCommandListener(this);
display.setCurrent(main);
currentScreen = "主画面";
}
// Pause is a no-op because there are no background
// activities or record stores to be closed.
public void pauseApp() { }
// Destroy must cleanup everything not handled
// by the garbage collector.
// In this case there is nothing to cleanup.
public void destroyApp(boolean unconditional) { }
private Image createImage(String name){
Image aImage = null;
try{
aImage = Image.createImage(name);
}
catch(IOException e){
}
return aImage;
}
private void exclusiveList(){
aEXCLUSIVEList = new List("职业",List.EXCLUSIVE,position,positionIcon);
aEXCLUSIVEList.addCommand(exitCommand);
aEXCLUSIVEList.addCommand(confirmCommand);
aEXCLUSIVEList.setCommandListener(this);
display.setCurrent(aEXCLUSIVEList);
currentScreen="职业";
}
private void multipleList(){
aMULTIPLEList = new List("兴趣",List.MULTIPLE,hobby,hobbyIcon);
aMULTIPLEList.addCommand(exitCommand);
aMULTIPLEList.addCommand(confirmCommand);
aMULTIPLEList.setCommandListener(this);
display.setCurrent(aMULTIPLEList);
currentScreen="兴趣";
}
private void implicitList(){
aIMPLICITList = new List("菜单",List.IMPLICIT);
aIMPLICITList.append("字体",createImage("/Sokoban.png"));
aIMPLICITList.append("段落",createImage("/Stock.png"));
aIMPLICITList.append("边框及底纹",createImage("/StarCruiser.png"));
aIMPLICITList.append("项目符号",createImage("/ManyBalls.png"));
aIMPLICITList.append("项目编号",createImage("/Tiles.png"));
aIMPLICITList.addCommand(exitCommand);
aIMPLICITList.setCommandListener(this);
display.setCurrent(aIMPLICITList);
currentScreen="菜单";
}
// Respond to commands. Here we are only implementing
// the exit command. In the exit command, cleanup and
// notify that the MIDlet has been destroyed.
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
if(currentScreen=="主画面"){
destroyApp(false);
notifyDestroyed();
}
else {
display.setCurrent(main);
currentScreen="主画面";
}
}
if (c == exclusiveCommand) {
exclusiveList();
}
if (c == multipleCommand) {
multipleList();
}
if(c == implicitCommand) {
implicitList();
}
if(c==confirmCommand){
Alert aAlert;
if(currentScreen=="职业"){
int aIndex = aEXCLUSIVEList.getSelectedIndex();
aAlert = new Alert("结果",position[aIndex],null,AlertType.INFO);
display.setCurrent(aAlert);
}
if(currentScreen=="兴趣"){
String result="你的兴趣是:\n";
for(int i=0;i<aMULTIPLEList.size();i++)
if(aMULTIPLEList.isSelected(i))
result += hobby[i] + ", ";
aAlert = new Alert("结果",result,null,AlertType.INFO);
aAlert.setTimeout(5000);
display.setCurrent(aAlert);
}
}
if(c==List.SELECT_COMMAND){
int aIndex = aIMPLICITList.getSelectedIndex();
String menuItem = aIMPLICITList.getString(aIndex);
Alert aAlert = new Alert("结果",menuItem,null,AlertType.INFO);
aAlert.setTimeout(5000);
display.setCurrent(aAlert);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -