关于Eclipse中的异常的处理方法
最近学习用Eclipse开发J2ME程序,我是按照书上的内容写入程序来运行调试。我总觉得书上有很多没说清楚,老是出现一些异常。比如我如果编辑的是使用高级用户界面的程序(使...
最近学习用Eclipse开发J2ME程序,我是按照书上的内容写入程序来运行调试。我总觉得书上有很多没说清楚,老是出现一些异常。比如我如果编辑的是使用高级用户界面的程序(使用MIDLET类),没语法错误的程序一般都能正确运行并调用模拟器(WTK)。但如果用的是使用低级用户界面的程序(使用CANVAS类),就算没语法错误,也编译不了,比如下面这个程序:package chapt1;
import javax.microedition.lcdui.*;
public class KTB extends Canvas {
private String promot;
public void paint(Graphics g){
g.setColor(11,22,123);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255,0,0);
g.drawString(promot, 10, 10, Graphics.TOP|Graphics.LEFT);
}
protected void KeyPressed(int keycode)
{if(keycode==Canvas.KEY_NUM0)promot="it is 0";
if(keycode==Canvas.KEY_NUM1)promot="it is 1";
repaint();
}
protected void KeyReleased(int keycode)
{ if(keycode==48)promot="it is 0";
if(keycode==49)promot="it is 1";
repaint();
}
}
老是会出现“Exception in thread "main" java.lang......"
的错误。但我自己试了下如果把这种程序跟高级程序界面结合起来(把低级用户界面的部分作为一个方法定义),就可以正常运行。比如:package chapt1;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
public class dirt extends MIDlet {
private Display display;
public dirt() {
super();
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
display=Display.getDisplay(this);
BackCom bac=new BackCom();
display.setCurrent(bac);
}
// TODO Auto-generated method stub
}
class BackCom extends Canvas implements CommandListener{
private Command red=new Command("red",Command.ITEM,1);
private Command green=new Command("green",Command.ITEM,1);
private Command blue=new Command("blue",Command.ITEM,1);
int background=0;
public BackCom(){
this.addCommand(red);
this.addCommand(green);
this.addCommand(blue);
this.setCommandListener(this);
}
protected void paint(Graphics g){
g.setColor(background);
g.fillRect(0,0, getWidth(), getHeight());
}
public void commandAction(Command c,Displayable d) {
if(c.equals(red))background=0xff0000;
else if(c.equals(green))background=0x00ff00;
else if(c.equals(blue))background=0x0000ff;
}
}
我不是很能说明问题的,很多说明不是很清楚。希望大家能帮我下。 展开
import javax.microedition.lcdui.*;
public class KTB extends Canvas {
private String promot;
public void paint(Graphics g){
g.setColor(11,22,123);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255,0,0);
g.drawString(promot, 10, 10, Graphics.TOP|Graphics.LEFT);
}
protected void KeyPressed(int keycode)
{if(keycode==Canvas.KEY_NUM0)promot="it is 0";
if(keycode==Canvas.KEY_NUM1)promot="it is 1";
repaint();
}
protected void KeyReleased(int keycode)
{ if(keycode==48)promot="it is 0";
if(keycode==49)promot="it is 1";
repaint();
}
}
老是会出现“Exception in thread "main" java.lang......"
的错误。但我自己试了下如果把这种程序跟高级程序界面结合起来(把低级用户界面的部分作为一个方法定义),就可以正常运行。比如:package chapt1;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
public class dirt extends MIDlet {
private Display display;
public dirt() {
super();
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
display=Display.getDisplay(this);
BackCom bac=new BackCom();
display.setCurrent(bac);
}
// TODO Auto-generated method stub
}
class BackCom extends Canvas implements CommandListener{
private Command red=new Command("red",Command.ITEM,1);
private Command green=new Command("green",Command.ITEM,1);
private Command blue=new Command("blue",Command.ITEM,1);
int background=0;
public BackCom(){
this.addCommand(red);
this.addCommand(green);
this.addCommand(blue);
this.setCommandListener(this);
}
protected void paint(Graphics g){
g.setColor(background);
g.fillRect(0,0, getWidth(), getHeight());
}
public void commandAction(Command c,Displayable d) {
if(c.equals(red))background=0xff0000;
else if(c.equals(green))background=0x00ff00;
else if(c.equals(blue))background=0x0000ff;
}
}
我不是很能说明问题的,很多说明不是很清楚。希望大家能帮我下。 展开
2008-03-27
展开全部
你没有了解J2ME程序的体系结构,他是在手机上外接的小程序,所以必须由 MIDlet入口,这个类是手机上的程序创建实例的,可能我没说太清楚,推荐一本带视频的书<J2me手机游戏开发与项目实战详解>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询