[英]J2ME No display when application is run
我是.Net开发人员,目前正在迁移到Java。 我在这里想念什么? 运行程序时没有显示?
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();
}
}
不要设置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();
}
}
我在这里想念什么?
据我所知,您的代码未正确调用Display.setCurrent(Displayable) ,这将要求“ ...使另一个Displayable对象在显示器上可见...” (引用API文档)。
运行程序时没有显示?
考虑到上述,这是预期的行为。 如果您在startApp方法中调用display.setCurrent(form)
,则很有可能会看到该表单。
边注。 我还将考虑将日历DateField的初始化从构造函数移动到startApp中。 根据我的回忆,这种方式会更可靠。
Form form = new Form("Calender"); /* why is 'Form' here? */
Form
更有意义 只需在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.