您的位置:首页 > 其它

手机蓝牙通信设计(二) 练习移动应用程序

2007-01-26 14:38 267 查看
练习移动应用程序,这一次我们还不去学习蓝牙通信方面的知识,主要是通过设计手机Application 来掌握程序的一般流程:

首先应该新建一个工程,当然是移动程序,然后输入项目的名称:如:MyTest ,接着输入类名称HelloMidlet并且确定保存的路径,一般选择在C:/WTK104/apps/ 下,这是方便j2me_wireless_toolkit-1_0_4-bin-win.exe 进行编译,运行,一般模拟器选择默认就可以了:

新建文件包 hello:新建java文件:HelloMidlet .java

并在文件中输入以下代码:

/*
* HelloMidlet.java
*
* Created on 2007年1月26日, 下午12:34
*/

package hello;

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

/**
*
* @author zhhz
*/
public class HelloMidlet extends MIDlet implements CommandListener {

/** Creates a new instance of HelloMidlet */
public HelloMidlet() {
}

private Form helloForm;
private StringItem helloStringItem;
private Command exitCommand;
private Form form1;
private Command exitCommand1;
private ChoiceGroup choiceGroup1;
private TextField textField1;
private TextField textField2;
private Command okCommand1;
private Command cancelCommand1;
private Command cancelCommand2;
private Command okCommand2;
private Command okCommand3;

/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here
getDisplay().setCurrent(get_helloForm());
// Insert post-init code here
}

/** Called by the system to indicate that a command has been invoked on a particular displayable.
* @param command the Command that ws invoked
* @param displayable the Displayable on which the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// Insert global pre-action code here
if (displayable == helloForm) {
if (command == exitCommand) {
// Insert pre-action code here
getDisplay().setCurrent(get_form1());
// Insert post-action code here
}
} else if (displayable == form1) {
if (command == exitCommand1) {
// Insert pre-action code here
exitMIDlet();
// Insert post-action code here
} else if (command == okCommand3) {
// = "/u8BF7/u8F93/u5165/u59D3/u540D/uFF1A"; // Insert pre-action code here
// Do nothing
// Insert post-action code here
}
}
// Insert global post-action code here
}

/**
* This method should return an instance of the display.
*/
public Display getDisplay() {
return Display.getDisplay(this);
}

/**
* This method should exit the midlet.
*/
public void exitMIDlet() {
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}

/** This method returns instance for helloForm component and should be called instead of accessing helloForm field directly.
* @return Instance for helloForm component
*/
public Form get_helloForm() {
if (helloForm == null) {
// Insert pre-init code here
helloForm = new Form(null, new Item[] {
get_helloStringItem(),
get_choiceGroup1()
});
helloForm.addCommand(get_exitCommand());
helloForm.setCommandListener(this);
// Insert post-init code here
}
return helloForm;
}

/** This method returns instance for helloStringItem component and should be called instead of accessing helloStringItem field directly.
* @return Instance for helloStringItem component
*/
public StringItem get_helloStringItem() {
if (helloStringItem == null) {
// Insert pre-init code here
helloStringItem = new StringItem("Hello", "Hello, World!");
// Insert post-init code here
}
return helloStringItem;
}

/** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.
* @return Instance for exitCommand component
*/
public Command get_exitCommand() {
if (exitCommand == null) {
// Insert pre-init code here
exitCommand = new Command("NEXT", Command.EXIT, 1);
// Insert post-init code here
}
return exitCommand;
}
/** This method returns instance for form1 component and should be called instead of accessing form1 field directly.
* @return Instance for form1 component
*/
public Form get_form1() {
if (form1 == null) {
// Insert pre-init code here
form1 = new Form(null, new Item[] {
get_textField1(),
get_textField2()
});
form1.addCommand(get_exitCommand1());
form1.addCommand(get_okCommand3());
form1.setCommandListener(this);
// Insert post-init code here
}
return form1;
}

/** This method returns instance for exitCommand1 component and should be called instead of accessing exitCommand1 field directly.
* @return Instance for exitCommand1 component
*/
public Command get_exitCommand1() {
if (exitCommand1 == null) {
// Insert pre-init code here
exitCommand1 = new Command("exit", Command.EXIT, 1);
// Insert post-init code here
}
return exitCommand1;
}
/** This method returns instance for choiceGroup1 component and should be called instead of accessing choiceGroup1 field directly.
* @return Instance for choiceGroup1 component
*/
public ChoiceGroup get_choiceGroup1() {
if (choiceGroup1 == null) {
// Insert pre-init code here
choiceGroup1 = new ChoiceGroup("asdfasdfkasd;k f/ndsfasd/nf/nas/ndf/na/n/n", Choice.MULTIPLE, new String[0], new Image[0]);
choiceGroup1.setSelectedFlags(new boolean[0]);
// Insert post-init code here
}
return choiceGroup1;
}

/** This method returns instance for textField1 component and should be called instead of accessing textField1 field directly.
* @return Instance for textField1 component
*/
public TextField get_textField1() {
if (textField1 == null) {
// Insert pre-init code here
textField1 = new TextField("/u8BF7/u8F93/u5165/u59D3/u540D/uFF1A", "", 120, TextField.ANY);
// Insert post-init code here
}
return textField1;
}

/** This method returns instance for textField2 component and should be called instead of accessing textField2 field directly.
* @return Instance for textField2 component
*/
public TextField get_textField2() {
if (textField2 == null) {
// Insert pre-init code here
textField2 = new TextField("/u8BF7/u8F93/u5165/u5BC6/u7801/uFF1A", null, 120, TextField.ANY);
// Insert post-init code here
}
return textField2;
}

/** This method returns instance for okCommand1 component and should be called instead of accessing okCommand1 field directly.
* @return Instance for okCommand1 component
*/
public Command get_okCommand1() {
if (okCommand1 == null) {
// Insert pre-init code here
okCommand1 = new Command("cancle", Command.OK, 1);
// Insert post-init code here
}
return okCommand1;
}

/** This method returns instance for cancelCommand1 component and should be called instead of accessing cancelCommand1 field directly.
* @return Instance for cancelCommand1 component
*/
public Command get_cancelCommand1() {
if (cancelCommand1 == null) {
// Insert pre-init code here
cancelCommand1 = new Command("Cancel", Command.CANCEL, 1);
// Insert post-init code here
}
return cancelCommand1;
}

/** This method returns instance for cancelCommand2 component and should be called instead of accessing cancelCommand2 field directly.
* @return Instance for cancelCommand2 component
*/
public Command get_cancelCommand2() {
if (cancelCommand2 == null) {
// Insert pre-init code here
cancelCommand2 = new Command("Cancel", Command.CANCEL, 1);
// Insert post-init code here
}
return cancelCommand2;
}

/** This method returns instance for okCommand2 component and should be called instead of accessing okCommand2 field directly.
* @return Instance for okCommand2 component
*/
public Command get_okCommand2() {
if (okCommand2 == null) {
// Insert pre-init code here
okCommand2 = new Command("Ok", Command.OK, 1);
// Insert post-init code here
}
return okCommand2;
}

/** This method returns instance for okCommand3 component and should be called instead of accessing okCommand3 field directly.
* @return Instance for okCommand3 component
*/
public Command get_okCommand3() {
if (okCommand3 == null) {
// Insert pre-init code here
okCommand3 = new Command("Ok", Command.OK, 1);
// Insert post-init code here
}
return okCommand3;
}

public void startApp() {
initialize();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}
在设计过程中完全可以向。NET中设计一样进行可视化设计。希望能对新手有一定的帮助 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: