读书人

J2ME中static的功能,该如何解决

发布时间: 2012-02-08 19:52:21 作者: rapoo

J2ME中static的功能
我把一个按钮可以用static声明,然后在两个面板上都加上这个按钮,不会造成冲突,然儿却不能把一个StringItem对象同时加两次,不管是在同一个面板或是同时加在不同的两个面板.
下面付了原码:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class doubleS extends MIDlet implements CommandListener
{
Display display;

Form form1;
Form form2;

StringItem str1,strDate,strMem;
static StringItem str2=new StringItem( "The Company was incorporated on May 1997 and at present ,we enjoy monopoly in the international market of hardware and software solutions ", " ");//用样式static声明的,但不可以被form1两次使用append(str2)

static Command cmdExit=new Command( "exit ",Command.EXIT,1);//static 方式声明的,可以被两个面板(form1&&form2)调用
Command cmdBack=new Command( "back ",Command.BACK,2);
Command cmdOk=new Command( "ok ",Command.OK,3);

Ticker ticket1,ticket2;

public doubleS()
{
Calendar calendar=Calendar.getInstance();
String date=Integer.toString(calendar.get(Calendar.MONTH)+1)+ "/ "+Integer.toString(calendar.get(Calendar.DAY_OF_MONTH))+ "/ "+Integer.toString(calendar.get(Calendar.YEAR));
strDate=new StringItem( " ", "the date: "+date+ ". ");

Runtime runMemory=Runtime.getRuntime();

String total_mem=Long.toString(runMemory.totalMemory());
String free_mem=Long.toString(runMemory.freeMemory());
strMem=new StringItem( "0506010231 ", "the total memory : "+total_mem+ "the free memory : "+free_mem+ ". ");


}

public void startApp()
{

str1=new StringItem( "Welcome to Prosto System ", " ");



ticket1=new Ticker( "null ");
ticket2=new Ticker( "Use your mobile phone to contact us for service at your doorstep ");

form1=new Form( "Prosto System ");
form2=new Form( "About Us ");

form1.append(str1);
form1.append(str2);
form1.setTicker(ticket1);
form1.addCommand(cmdExit);
form1.addCommand(cmdOk);
form1.setCommandListener(this);

form2.append(str2);
form2.append(strMem);
form2.append(strDate);
form2.setTicker(ticket2);
form2.addCommand(cmdBack);
form2.addCommand(cmdExit);
form2.setCommandListener(this);

display=Display.getDisplay(this);
display.setCurrent(form1);


}
public void pauseApp(){}
public void destroyApp(boolean c)
{
notifyDestroyed();
}

public void commandAction(Command cmd,Displayable displayable)
{
String label=cmd.getLabel();
if(label.equals( "exit "))
{
destroyApp(true);
}
else if(label.equals( "back "))
{
display.setCurrent(form1);
}


else
{
display.setCurrent(form2);
}

}
}

[解决办法]
如果我没记错的话应该是这样的,,这也算是J2ME的基础了,楼主也得多谦虚点呢..

读书人网 >J2ME开发

热点推荐