简体   繁体   English

J2ME在运行应用程序时不显示

[英]J2ME No display when application is run

I am a .Net Developer and currently migrating to java. 我是.Net开发人员,目前正在迁移到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; 不要设置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). 据我所知,您的代码未正确调用Display.setCurrent(Displayable) ,这将要求“ ...使另一个Displayable对象在显示器上可见...” (引用API文档)。

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. 如果您在startApp方法中调用display.setCurrent(form) ,则很有可能会看到该表单。

side note. 边注。 I would also consider moving initialization of calender DateField from constructor into startApp. 我还将考虑将日历DateField的初始化从构造函数移动到startApp中。 Per my recollection this way would be more reliable. 根据我的回忆,这种方式会更可靠。

  • also this part of startApp looks very fishy: 同样,startApp的这一部分看起来也很糟糕:
    Form form = new Form("Calender"); /* why is 'Form' here? */
    as far as I can tell removing Form would make much more sense 据我所知,删除Form更有意义

Just use one following line of code in startApp() method 只需在startApp()方法中使用以下一行代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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