首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

怎么使用一个继承MIDlet的抽象类

2012-03-09 
如何使用一个继承MIDlet的抽象类大家好!现在发现一个抽象类继承MIDlet,但运行后,自动推出了,然后报错:java

如何使用一个继承MIDlet的抽象类
大家好!
  现在发现一个抽象类继承MIDlet,但运行后,自动推出了,然后报错:java.lang.InstantiationException
  想问下,MIDlet不是个抽象类吗?为什么还要写一个抽象类来继承它呢?
   
abstract public class SDAApplication extends MIDlet {

  private static SDAApplication instance;
  protected final SDAApplication Application = this;
  private AppCanvas appCanvas = null;
  private Graphics graphics = null;
  private Display disp = Display.getDisplay(this);
  private SDAForm foucsedForm = null;
  private SDAForm mainForm = null;
  public SDAInputPanel inputPanel = new SDAInputPanel();
  private Vector dispList = new Vector();
  private SDAMessageBox msgBox;
  public static final int SKIN_STANDARD = 0;
  public static final int SKIN_POPULAR = 1;
  private SDASkins Skins = null;
  private int skin = SKIN_STANDARD;
  private boolean doubleBuffered = true;
  private boolean ctrl3d = false;
  private Timer runOneceTimer = new Timer();
  public SDAApplication() {
  instance = this;
  this.appCanvas = new AppCanvas();
  this.appCanvas.setFullScreenMode(true);
  this.disp.setCurrent(appCanvas);
  }

  public final void setSkin(int skin) {
  this.skin = skin;
  if (skin == SKIN_POPULAR) {
  Skins = null;
  Skins = SDASkins.CreateSkins();
  }
  }

  public final void setDoubleBuffered(boolean doubleBuffered) {
  this.doubleBuffered = doubleBuffered;
  }

  public void setCtrl3d(boolean ctrl3d) {
  this.ctrl3d = ctrl3d;
  }

  public void setMainForm(SDAForm mainForm) {
  this.mainForm = mainForm;
  }

  public final boolean isDoubleBuffered() {
  return this.doubleBuffered;
  }

  public final boolean hasPointer() {
  return this.appCanvas.hasPointerEvents();
  }

  protected final Graphics GetGraphics() {
  return this.graphics;
  }

  protected final Canvas GetCanvas() {
  return this.appCanvas;
  }

  protected SDAForm getFoucsedForm() {
  return foucsedForm;
  }
  public int getSkin() {
  return skin;
  }

  public boolean isCtrl3d() {
  return ctrl3d;
  }

  public static SDAApplication GetInstance() {
  return instance;
  }

  public void MessageBox(String Caption, String Text, int flags) {
  MessageBox(Caption, Text, flags, null);
  }

  public void MessageBox(String Caption, String Text, int flags,
  MsgBoxCloseEvent closeEvent) {
  msgBox.closeEvents.addElement(closeEvent);
  msgBox.caption = Caption;
  msgBox.txtLabel.setText(Text);
  msgBox.button1.setVisible(false);
  msgBox.button2.setVisible(false);
  int btnFlags = flags % 10;
  switch (btnFlags) {
  case SDAConsts.MB_OK: {
  msgBox.button1.setVisible(true);
  msgBox.button1.setText("确定");
  break;
   
  }
  case SDAConsts.MB_YESNO: {
  msgBox.button1.setVisible(true);
  msgBox.button2.setVisible(true);
  msgBox.button1.setText("是");


  msgBox.button2.setText("否");
  break;
  }
  default: {
  msgBox.button1.setVisible(true);
  msgBox.button1.setText("确定");
  break;
  }
  }
  //System.out.println("MessageBoxEnd");
  if (msgBox.button2.visible) {
  msgBox.button2.setFoucsed();
  } else if (msgBox.button1.visible) {
  msgBox.button1.setFoucsed();
  }
  msgBox.InternalShow();
  }

  protected final void ShowForm(SDAForm form) {
  int index = dispList.indexOf(form);
  //System.out.println("ShowForm index=" + index);
  if (index > -1) {
  dispList.removeElementAt(index);
  }
  dispList.addElement(form);
  foucsedForm = form;

  form.canvas = this.appCanvas;
  form.graphics = this.graphics;
  disp.setCurrent(appCanvas);
  form.Repaint();
  }

  protected final void CloseForm(SDAForm form) {
  int index = dispList.indexOf(form);
  if (index > -1) {
  dispList.removeElementAt(index);
  }
  if (!dispList.isEmpty()) {
  Object current = dispList.lastElement();
  if (current instanceof SDAForm) {
  ((SDAForm) current).Show();
  } else {
  disp.setCurrent((Displayable) current);
  }
  }else
  {
  if(mainForm!=null&&!mainForm.equals(form))
  {
  mainForm.Show();
  }
  else
  {
  Application.Exit();
  }
  }
  }

  public final void ShowDisplayable(Displayable displayable) {
  //System.out.println("ShowDisplayable");
  int index = dispList.indexOf(displayable);
  if (index > -1) {
  dispList.removeElementAt(index);
  }
  dispList.addElement(displayable);
  disp.setCurrent(displayable);
  }

  public final void CloseDisplayable(Displayable displayable) {
  //System.out.println("CloseDisplayable");
  int index = dispList.indexOf(displayable);
  if (index > -1) {
  //System.out.println("Remove displayable index=" + index);
  dispList.removeElementAt(index);
  }
  if (!dispList.isEmpty()) {
  Object current = dispList.lastElement();
  if (current instanceof SDAForm) {
  ((SDAForm) current).Show();
  } else {
  disp.setCurrent((Displayable) current);
  }
  }
  }

  public void startApp() {
  msgBox = new SDAMessageBox();
  runOneceTimer.schedule(new TimerTask() {
  public void run() {
  while (graphics == null) {
  appCanvas.repaint();
  }
  if (foucsedForm != null) {
  //System.out.println("startApp");
  foucsedForm.Show();
  }
  runOneceTimer.cancel();


  }
  }, 0, 10);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public final void Exit() {
  this.quitApp();
  }

  public final void quitApp() {
  this.foucsedForm = null;
  this.appCanvas = null;
  this.dispList.removeAllElements();
  instance.destroyApp(true);
  instance.notifyDestroyed();
  instance = null;
  }

  class AppCanvas extends Canvas {

  public void paint(Graphics g) {
  //System.out.println("paint");
  if (graphics == null) {
  graphics = g;
  return;
  }
  if (!instance.isDoubleBuffered() && foucsedForm != null) {
  foucsedForm.graphics = g;
  foucsedForm.paint();
  }
  }

  protected void showNotify() {
  //System.out.println("showNotify");
  if (foucsedForm != null) {
  foucsedForm.paint();
  }
  }

  protected void pointerPressed(int x,
  int y) {
  if (foucsedForm != null) {
  foucsedForm.pointerPressed(x, y);
  }
  }

  protected void pointerReleased(int x,
  int y) {
  if (foucsedForm != null) {
  foucsedForm.pointerReleased(x, y);
  }
  }

  protected void pointerDragged(int x, int y) {
  if (foucsedForm != null) {
  foucsedForm.pointerDragged(x, y);
  }
  }

  protected void keyPressed(int keyCode) {
  //String keyName = getKeyName(keyCode).toUpperCase();
  if (foucsedForm != null) {
  foucsedForm.keyPressed(keyCode);
  }
  }

  protected void keyReleased(int keyCode) {
  if (foucsedForm != null) {
  foucsedForm.keyReleased(keyCode);
  }
  }
  }

 

[解决办法]
直接写个类的话少了点强制性了,
如果直接写个类有些人可能不知道哪几个方法必须实现的,
写抽象类的方法好处是只要方法是抽象的且是保护的,子类就得强制实现。。

热点排行