为什么不能在form和textbox里面切换?(很简单的程序 附源码)
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TextBoxMid extends MIDlet implements CommandListener{
private Display display = null;
private TextBox textbox = null;
private Form form = null;
private Command SetFormDis = new Command( "Turn On Form ",Command.EXIT,1);
private Command ReturnDis = new Command( "Return ",Command.HELP,1);
public TextBoxMid() {
textbox = new TextBox( "UIDemo ", "输入密码: ",1024,
TextField.ANY);
Form form = new Form( "this is a new form ");
form.addCommand(ReturnDis);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
if(display == null){
display = Display.getDisplay(this);
textbox.addCommand(SetFormDis);
textbox.setCommandListener(this);
display.setCurrent(textbox);
}
else{
display.setCurrent(new Form( "display null "));
}
}
public void commandAction(Command cmd, Displayable displayable){
if(cmd == SetFormDis){
//Why the form can not be displayed?!
display.setCurrent(form);
}else if(cmd == ReturnDis){
display.setCurrent(textbox);
}
}
}
目地:在按下Turn On Form 后显示form,并且form中同时会显示Return按钮。此时要是按下Return则会再次显示textbox和其中的按钮。
问题:在按下Turn On Form后什么反应也没有?
------解决方案--------------------Form form = new Form( "this is a new form ");
这句有问题啊~应该是这样的吧
form = new Form( "this is a new form ");
------解决方案--------------------楼主,你的全局变量form没有构造拉,你在TextBoxMid()方法中构造的只是一个临时变量Form form,只是名字和全局变量form相同,在完成TextBoxMid()方法后,临时变量form就没在内存中了。而你的方法commandAction中调用的却是全局变量form,他没有构造过,当然无法显示。
楼主的java基础还要补一下。
saltedfish