📄 ui_ex7.java
字号:
// 程序名UI_Ex7.java
// 测试使用命令多屏幕浏览
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class UI_Ex7 extends MIDlet implements CommandListener {
private Display display; // 引用MIDlet的Display 对象
private TextBox scrHelp;
private TextBox scrJump;
private TextBox[] myScreen;
private Command cmdBack;
private Command cmdForw;
private Command cmdJump;
private Command cmdExit;
private Command cmdHelp;
private Command cmdCanc;
private Command cmdConf;
static int MaxHistory = 8;
static int MaxScreen = 4;
int[] history;
int currentPos;
int historyLen;
// MIDlet构造程序
public UI_Ex7() {
display = Display.getDisplay(this);// 获取一个Display实例,它是唯一的
cmdForw = new Command("前进",Command.SCREEN,1);
cmdBack = new Command("后退",Command.BACK,1);
cmdJump = new Command("跳转",Command.SCREEN,2);
cmdHelp = new Command("帮助",Command.HELP,1);
cmdExit = new Command("退出",Command.SCREEN,2);
cmdConf = new Command("确定",Command.OK,1);
cmdCanc = new Command("取消",Command.CANCEL,1);
String helpStr = new String("\"前进\" 跳转到前一个浏览过的屏幕,"
+"\"后退\" 跳转到后一个浏览过的屏幕,"
+"\"跳转\" 指定你要到达的屏幕,"
+"\"退出\" 退出应用程序,");
scrHelp = new TextBox("帮助",helpStr,100,0);
scrHelp.addCommand(cmdCanc);
scrHelp.setCommandListener(this);
scrJump = new TextBox("跳转到","",2,2);
scrJump.addCommand(cmdConf);
scrJump.addCommand(cmdCanc);
scrJump.setCommandListener(this);
myScreen = new TextBox[MaxScreen];
myScreen[0] = createScreen("屏幕一","床前明月光");
myScreen[1] = createScreen("屏幕二","疑是地上霜");
myScreen[2] = createScreen("屏幕三","举头望明月");
myScreen[3] = createScreen("屏幕四","低头思故乡");
history = new int[MaxHistory];
currentPos = -1;
historyLen = 0;
}
// 被应用程序管理器调用来启动MIDlet。
public void startApp() {
gotoScreen(0);// 把textBox设置为当前屏幕或当前Displayable对象
addHistory(0);
}
// 一个必要的方法
public void pauseApp() {
}
// 一个必要的方法
public void destroyApp(boolean unconditional) {
}
private TextBox createScreen(String screenTitle,String screenContent) {
TextBox tempScreen = new TextBox(screenTitle,screenContent,50,0);
tempScreen.addCommand(cmdForw);
tempScreen.addCommand(cmdBack);
tempScreen.addCommand(cmdJump);
tempScreen.addCommand(cmdHelp);
tempScreen.addCommand(cmdExit);
tempScreen.setCommandListener(this);
return tempScreen;
}
private void addHistory(int num) {
if (historyLen < MaxHistory) {
historyLen++;
history[historyLen] = num;
currentPos++;
} else {
for (int i=0;i<MaxHistory;i++) {
history[i] = history[i+1];
}
history[MaxHistory] = num;
currentPos = historyLen-1;
}
}
void gotoScreen(int num) {
display.setCurrent(myScreen[num]);
}
// 设置事件触发时的动作
public void commandAction(Command c, Displayable d) {
if (c == cmdBack) {
if (currentPos > 0) {
currentPos--;
gotoScreen(history[currentPos]);
}
return;
}
if (c == cmdForw) {
if (currentPos < historyLen) {
currentPos++;
gotoScreen(history[currentPos]);
}
return;
}
if (c == cmdJump) {
display.setCurrent(scrJump);
return;
}
if (c == cmdHelp) {
display.setCurrent(scrHelp);
return;
}
if (c == cmdCanc) {
gotoScreen(history[currentPos]);
return;
}
if (c == cmdConf) {
int next = Integer.parseInt(scrJump.getString());
if ((next > 0)&&(next <= MaxScreen)) {
display.setCurrent(myScreen[next-1]);
addHistory(next-1);
}
return;
}
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -