请问运行J2ME程序,运行完模拟器就关闭了是怎么回事? 谢谢
小弟初学J2ME,写了个小程序,运行程序时,模拟器自动关闭是怎么回事?谢谢~~
没有在中文目录下,代码如下:
package com.j2medev.netbeans;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TurboMidlet extends MIDlet implements CommandListener{
private Display display = null;
private Form mainForm = null;
public static final Command exitCommand = new Command("退出",Command.OK, 1);
//startApp()方法是程序的入口,相当于java应用程序中的main()方法
public void startApp() {
if(display == null){
display = Display.getDisplay(this);
}
mainForm = new Form("TurboMIDlet");
Image img = getImage("JavaPowered-8.png");
mainForm.append(img);
String text = "";
try{
text = getText("j2medev.txt");
}catch(IOException ex){
text = "读取文本出错";
}
mainForm.append("\n"+text);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
//将mainForm设置为手机屏幕的当前显示对象
display.setCurrent(mainForm);
}
private Image getImage(String name){
Image img = null;
try{
//读取images目录下的图片
img = Image.createImage("/images/"+name);
}catch(IOException ex){
ex.printStackTrace();
}
return img;
}
private String getText(String name) throws IOException{
//将资源文件,例如图片和文本,与java流关联的最好方法
InputStream is = getClass().getResourceAsStream("/text/"+name);
if(is != null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
//将文本文件的内容写入到baos开辟的内存中
while((ch = is.read()) != -1){
baos.write(ch);
}
//返回文本文件内容的字节数组
byte[] text = baos.toByteArray();
baos.close();
//由于包含中文,所以编码转换为UTF-8
return new String(text,"UTF-8");
}else{
return null;
}
}
public void pauseApp() {
//在本例中不做任何处理
}
public void destroyApp(boolean unconditional) {
//在本例中不做任何处理
}
public void commandAction(Command cmd,Displayable displayable){
if(cmd == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
开发工具NetBeans eclipse也一样,谢谢!!!!