第一个简单的J2ME程序

上机实验二简单 MIDP程序的创建与调试 3.18 MIDP程序的创建与调试 3.18

[实验目的]

完成简单 MIDP程序的创建与调试过程,熟悉在 Netbeans环境下进行 JavaME
MIDP程序开发的基本方法与步骤。
[实验内容]

1、创建一个名为 Hello的 MIDP项目,设备配置使用 CLDC1.1,设备配置文
件使用 MIDP 2.0,设备模拟器直接使用默认的彩屏手机模拟器。
2、在 Hello项目中创建一个名为 HelloMidlet的 MIDlet程序 (源程序见


附录部分)。
3、生成该程序,在电脑中对其进行测试运行。
4、通过数据线或其它方法将该程序拷贝到自己的手机中,在手机中试运行


该程序。

[实验步骤]
1、创建程序
2、测试运行程序
3、将程序发布到手机中运行

/*

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**

* @author yangyan
*/
public class HelloMIDlet extends MIDlet implements CommandListener {

private boolean midletPaused = false;
private TextBox text;


//

private Command exitCommand;
private Form form;
private StringItem stringItem;


//



/**

* The HelloMIDlet constructor.
*/
public HelloMIDlet() {
text=new TextBox("Hello", "Hello World", 30, TextField.ANY);
exitCommand = new Command("Exit", Command.EXIT, 1);
text.addCommand(exitCommand);
text.setCommandListener(new CommandListener() {


public void commandAction(Command cmd, Displayable disp) {
destroyApp(true);
notifyDestroyed();


}
});
}


//
//



//

/**

* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the
startMIDlet method.
*/

private void initialize() {

// write pre-initialize user code here

// write post-initialize user code here
}
//

//


/**

* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {


// write pre-action user code here

switchDisplayable(null, getForm());

// write post-action user code here

}

//


//

/**


* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {

// write pre-action user code here

// write post-action user code here

}

//


//

/**

* Switches a current displayable in a display. The display instance is taken
from getDisplay method. This method is used by all actions in the design for
switching displayable.
* @param alert the Alert which is temporarily set to the display; if null, then
nextDisplayable is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {

// write pre-switch user code here

Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}


// write post-switch user code here

}

//


//

/**

* Called by a system to indicated that a command has been invoked on a particular
displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {

// write pre-action user code here

if (displayable == form) {
if (command == exitCommand) {


// write pre-action user code here

exitMIDlet();


// write post-action user code here

}
}


// write post-action user code here

}

//


//

/**

* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {

// write pre-init user code here

exitCommand = new Command("Exit", Command.EXIT, 0);

// write post-init user code here

}
return exitCommand;
}

//


//

/**

* Returns an initiliazed instance of form component.
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {

// write pre-init user code here

form = new Form("Welcome", new Item[] { getStringItem() });
form.addCommand(getExitCommand());
form.setCommandListener(this);


// write post-init user code here

}
return form;
}

//


//

/**

* Returns an initiliazed instance of stringItem component.
* @return the initialized component instance
*/

public StringItem getStringItem() {
if (stringItem == null) {


// write pre-init user code here

stringItem = new StringItem("Hello", "Hello, World!");

// write post-init user code here

}
return stringItem;
}


//


/**

* Returns a display instance.
* @return the display instanc

e.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}


/**

* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();


}

/**

* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the
MIDlet.
*/

public void startApp() {
if (midletPaused) {
resumeMIDlet ();


} else {
initialize ();
startMIDlet ();


}
midletPaused = false;
}


/**

* Called when MIDlet is paused.

*/

public void pauseApp() {
midletPaused = true;
}


/**

* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all
resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}

}




相关主题
相关文档
最新文档