简体   繁体   中英

J2ME No display when application is run

I am a .Net Developer and currently migrating to java. What am I missing here? There is no display when I run the program?

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import java.util.Date;
import java.util.TimeZone;

public class CalenderMIDlet extends MIDlet{
  private Form form = null;
  private DateField calender = null;  
  private static final int DATE = 0;

  public CalenderMIDlet(){
  calender = new DateField("Date In:", DateField.DATE, TimeZone.getTimeZone("GMT"));
  }

  public void startApp(){
  display = Display.getDisplay(this);
  Form form = new Form("Calender");
  form.append(calender);
  }

  public void pauseApp(){}

  public void destroyApp(boolean destroy){
  notifyDestroyed();
  }
} 

Don't set the private Form form = null;

Try this code

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import java.util.Date;
import java.util.TimeZone;

public class CalenderMIDlet extends MIDlet{
    private Form form;
    private Display display;
    private DateField calender;  
    private static final int DATE = 0;

    public CalenderMIDlet(){
        calender = new DateField("Date In:", DateField.DATE, TimeZone.getTimeZone("GMT"));
    }

    public void startApp(){
        display = Display.getDisplay(this);
        Form form = new Form("Calender");
        form.append(calender);
        display.setCurrent(form);
    }

    public void pauseApp(){}

    public void destroyApp(boolean destroy){
        notifyDestroyed();
    }
}

What am I missing here?

Well as far as I can tell your code misses to invoke Display.setCurrent(Displayable) , that would request "...that a different Displayable object be made visible on the display..." (quoting API documentation).

There is no display when I run the program?

This is expected behavior, taking into account above. Most likely if you invoke display.setCurrent(form) in startApp method, you'll see the form.

side note. I would also consider moving initialization of calender DateField from constructor into startApp. Per my recollection this way would be more reliable.

  • also this part of startApp looks very fishy:
    Form form = new Form("Calender"); /* why is 'Form' here? */
    as far as I can tell removing Form would make much more sense

Just use one following line of code in startApp() method

 public void startApp(){
  display = Display.getDisplay(this);
  Form form = new Form("Calender");
  form.append(calender);
display.setCurrent(form);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM