📄 testmidlet.java
字号:
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import demo.TestItem;
public class TestMidlet extends MIDlet implements CommandListener{
private Form form; //对话框对象
private ChoiceGroup choice_title; //存储各个题目的标题
private StringItem strItem; //存储结果信息
private TestItem items[]; //存储所有的题目
private int curItem = -1; //当前选择的题目
private Command OKCommand; //测试指令
private Command ExitCommand; //退出指令
private Command BackCommand; //返回指令
//type控制对话框的显示内容,
//0显示所有题目的标题,1显示被选题目的问句及选项,2显示测试结果
private int type = 0;
public TestMidlet() {
super();
//创建对话框及各个组件
form = new Form("心理测试");
choice_title = new ChoiceGroup("选择题目",
ChoiceGroup.EXCLUSIVE );
strItem = new StringItem("","");
form.append(choice_title);
//读取文本信息
LoadText();
//创建高级指令,并将高级指令添加到对话框
OKCommand = new Command( "测试", Command.OK, 0);
BackCommand = new Command( "返回", Command.OK, 0);
ExitCommand = new Command( "退出", Command.EXIT, 0);
form.addCommand(OKCommand);
form.addCommand(ExitCommand);
//设置高级指令监听器
form.setCommandListener(this);
}
private void LoadText(){
items = new TestItem[100]; //最多允许有100个题目
try{
byte b[] = new byte[10240]; //分配10K大小的缓存
InputStream is = getClass().getResourceAsStream("/demo/test.txt");
int total = is.read(b); //一次性将数据全部读出
int start = 0;
int index = 0;
while( start < total && index < items.length )
{//当 start < total 时,表明还有尚未读取的题目
items[index] = new TestItem(); //为新题目分配存储空间
start = items[index].Load( b, start, total ); //读取一个题目的全部信息
choice_title.append(items[index].Title, null); //保存题目的标题
index ++;
}
is.close();
}
catch (IOException e){ //输出错误信息
e.printStackTrace();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
//将对话框设置为当前屏幕的显示对象
Display.getDisplay(this).setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if( c == OKCommand ){
switch( type ){
case 0: //当前对话框上显示所有标题
curItem = choice_title.getSelectedIndex();
if( curItem >= 0 ){ //如果选择某个题目,则更换显示内容
form.deleteAll();
form.append( items[curItem].choicegroup );
type = 1;
}
break;
case 1: //当前对话框上显示问句和选项
int n = items[curItem].choicegroup.getSelectedIndex();
if( n >= 0 ){ //如果选择某个选项,则更换显示内容
form.deleteAll();
strItem.setText(items[curItem].results[n]);
form.append( strItem );
type = 2;
}
form.removeCommand(OKCommand);
form.addCommand(BackCommand);
break;
}
}
if( c == BackCommand ){
form.deleteAll(); //则更换显示内容
form.append( choice_title );
curItem = choice_title.getSelectedIndex();
type = 0;
form.removeCommand(BackCommand);
form.addCommand(OKCommand);
}
if( c == ExitCommand ){
this.notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -