2012-03-14 119 views
1

我是.Net开发人员,目前正在迁移到java。我在这里错过了什么?我运行程序时没有显示?J2ME在运行应用程序时不显示

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(); 
    } 
} 

回答

3

不要设置private Form form = null;

试试这个代码

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(); 
    } 
} 
2

缺少什么我在这里?

嘛,据我可以告诉你的代码偏出调用Display.setCurrent(Displayable),这将要求“......一个不同的Displayable对象可以在显示器上可见......”(报价API文档)。

当我运行该程序时没有显示?

这是预期的行为,考虑到上述情况。如果您在startApp方法中调用display.setCurrent(form),您很可能会看到该表单。

附注。我还会考虑将日历DateField的初始化从构造函数移动到startApp。根据我的回忆,这种方式会更可靠。

  • 还的startApp的这一部分看起来很可疑:
    Form form = new Form("Calender"); /* why is 'Form' here? */
    据我可以告诉消除Form将使更多的意义
+0

不过它没有显示。我试过了。 – Lion 2012-03-14 13:10:12

+0

@Lion不知道你尝试了什么,但没有'setCurrent'什么也不会显示,这肯定是 – gnat 2012-03-14 13:25:16

+0

没有'setCurrent()'没有显示什么是正确的。它只显示(使用'setCurrent()')'Date In:'。我无法弄清楚。 – Lion 2012-03-14 13:28:53

2

只需要使用的startApp一个代码以下行()方法

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