📄 uidemo.java
字号:
package ch09.section01;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
//UIDemo是主类。此实例中的每个控件类都继承了BaseDemo框架类或BaseListDemo框架类
public class UIDemo
extends MIDlet
implements CommandListener {
private static UIDemo instance = null;
private Displayable currentDemo = null;
//存储控件实现类列表的数组
private Class[] demos = {
getClassObject("ch09.section01.LabelDemo"),
getClassObject("ch09.section01.DateFieldDemo"),
getClassObject("ch09.section01.AlertDemo"),
getClassObject("ch09.section01.ChoiceGroupDemo"),
getClassObject("ch09.section01.GaugeDemo"),
getClassObject("ch09.section01.ListDemo"),
getClassObject("ch09.section01.TextBoxDemo"),
getClassObject("ch09.section01.TextFieldDemo"),
getClassObject("ch09.section01.TickerDemo")
};
//存储控件名称的数组
private static final String[] demoNames = {
"标签控件",
"日期域控件",
"提示框控件",
"选择组控件",
"进度条控件",
"列表控件",
"文本框控件",
"文本域控件",
"文本滚动条控件"
};
//改列表控件用于用户选择各个控件
private List selectionList = new List("用户界面控件实例", List.IMPLICIT, demoNames, null);
//此方法用于动态装入类
private static Class getClassObject(String className) {
try {
return (Class.forName(className));
}
catch (Throwable err) {
System.out.println(err.getClass().getName() + ": " + err.getMessage());
err.printStackTrace();
}
return (null);
}
//此方法用于获取一个MIDlet实例
static UIDemo getInstance() {
return (instance);
}
//默认构造器
public UIDemo() {
instance = this;
}
//此方法用于使MIDlet应用程序终止,进入销毁状态。
protected void destroyApp(boolean unconditional) {
demos = null;
selectionList = null;
currentDemo = null;
}
//该方法使MIDlet应用程序停止,进入挂起状态。
protected void pauseApp() {
}
//该方法使MIDlet应用程序进入活动状态。
protected void startApp() {
Display.getDisplay(this).setCurrent(selectionList);
selectionList.setCommandListener(this);
}
//该方法用于退出当前midlet
public static void quit() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
//该方法用于返回初始界面
public static void goBack() {
instance.currentDemo = null;
instance.startApp();
}
//该方法用于获取当前运行的Demo
Displayable getCurrentDemo() {
return (currentDemo);
}
//响应事件
public void commandAction(Command c, Displayable d) {
int pos = selectionList.getSelectedIndex();
try {
currentDemo = (Displayable) demos[pos].newInstance();
Display.getDisplay(this).setCurrent(currentDemo);
}
catch (Exception err) {
Alert a = new Alert("Error");
a.setString("Failed in handling the command at position: " + pos);
a.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(a);
}
}
//创建按钮
static Command[] getDemoCommands() {
Command[] commands = {
new Command("返回", Command.BACK, 1),
new Command("退出", Command.STOP, 2)
};
return (commands);
}
//响应事件
static Runnable[] getDemoActions() {
class BackCallback
implements Runnable {
//响应返回事件
public void run() {
UIDemo.goBack();
}
}
//响应退出事件
class QuitCallback
implements Runnable {
public void run() {
UIDemo.quit();
}
}
//响应菜单返回
class MenuCallback
implements Runnable {
public void run() {
}
}
Runnable[] callbacks = {
new BackCallback(), new QuitCallback()
};
return (callbacks);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -