J2me lecture04 b

Uploaded from authorPOINTLite
Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

By: davidludut (38 month(s) ago)

怎么下载?

Presentation Transcript

J2ME设计及应用: 

J2ME设计及应用 Fall, 2007

Contents: 

Contents Command类 Item类

Command类: 

Command类 创建Command类实例: 通过使用Command类的构造函数创建Command类的实例; Command类的构造函数需要3个参数: 命令标签 — 命令的标识符 命令类型 命令优先级 以一个指定的数值来设定优先级,显示应用程序创建的命令对象的重要程度,数值越小优先级越高; 设备的应用程序管理器可以忽略优先级,也可以使用这个优先级来处理两个命令之间的冲突。

Command类: 

Command类 Example: Command cancel = new Command(“Cancel”, Command.CANCEL, 1);

命令类型: 

命令类型

CommandListener: 

CommandListener J2ME的应用程序,每个创建的Command实例,都必须创建实现CommandListener接口的实例; 每当用户通过commandAction()方法的方式与命令进行交互的时候,就会通知CommandListener; 实现CommandListener的类必须实现commandAction()方法,这个方法有两个参数: Command实例的引用 Displayable类的实例的引用

commandAction( )方法: 

commandAction( )方法 Example: public void commandAction(Command command, Displayable displayable) { if( command == cancel) { destroyApp(false); notifyDestroyed(); } }

commandAction( )方法: 

commandAction( )方法 当命令的事件发生的时候,应用程序需要将用户输入的命令与其创建的Command类的实例进行比较; 程序员要实现每一个命令事件发生时所要发生的全部处理过程; 调用destroyApp()方法可以无条件地终止应用程序; 在应用程序终止前,需要调用notifyDestroyed()方法通知设备的应用程序正在被终止。

Example: 

Example

Example: 

Example 基于Command类的GUI例程: Listing 5-5.java

Form类: 

Form类 Form类是在屏幕上同时显示其他displayable类的容器; 在Form类中能放置Item类的派生类; 将Item类的实例与表单建立关联的方法: 在Form类的实例中放置实例,是通过调用insert()方法或append()方法; insert()方法是通过传人的参数在表单的特定位置上放置实例; append()方法是,在表单上,在前一个放置的对象之后放置实例。 创建Item实例的数组。

Form类 — Example: 

Form类 — Example private Form form; private StringItem message; form = new Form("My Form"); message = new StringItem("Welcome, ", "glad you could come."); form.append(message); //StringItem类的实例是静态的,用户不能修改; //StringItem类的实例使文本出现在表单上。

Form类 — Example: 

Form类 — Example private Form form; private StringItem message1, message2; private int index1; form = new Form("My Form"); message1 = new StringItem("Welcome, ", "glad you could come."); message2 = new StringItem("Hello, ", "Mary."); index1 = form.append(message1); form.insert(index1,message2); index1: 索引数,从0开始计数,可以把索引数作为实例的引用传给相应的方法; 把变量index1作为第一个参数传给insert()方法,用来标识在表单上将第二个StringItem实例放置在第一个StringItem实例之前;

Slide14: 

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class CreatingFormWithItems extends MIDlet implements CommandListener { private Display display; private Form form; private Command exit; public CreatingFormWithItems () { display = Display.getDisplay(this); exit = new Command("Exit", Command.SCREEN, 1); StringItem messages[] = new StringItem[2]; message[0] = new StringItem("Welcome, ", "glad you could come."); message[1] = new StringItem("Hello, ", "Mary."); form = new Form("Display Form with Items", messages); form.addCommand(exit); form.setCommandListener(this); } public void startApp() { display.setCurrent(form); } …… }

Item类: 

Item类 Item类使一组能被Form类包含的派生类的基类,包括: ChoiceGroup — 在表单上创建复选框或单选按钮; DateField — 获取MIDlet的用户的日期输入; Gauge — 创建一个指示器; ImageItem — 使一副图像出现在表单上; StringItem — 使文本出现在表单上; TextField — 获取MIDlet的用户的任意形式的文本输入。

Item类: 

Item类 当用户在Item类的派生类中输入数据使,就会改变这个实例的状态; 通过Item类的派生类的实例所关联的ItemStateListener来捕获这个变化; ItemStateListener监听MIDlet的运行过程中的事件,并且捕获屏幕上的表单所包含的任何Item类实例的变化; 当发生一个Item类事件时,调用了ItemStateChanged()方法来处理这个事件; 在itemStateChanged()方法中,比较这个引用和表单上已知的item对象,然后进行处理。

Item类 — Example: 

Item类 — Example private Form form; private ChoiceGroup choiceGroup; …. choiceGroup = new ChoiceGroup("Pick One",Choice.EXCLUSIVE); form.append(choiceGroup); form.setItemStateListener(this); …. public void itemStateChanged(Item item) { if (item == choiceGroup) { // do some processing } }

ChoiceGroup类: 

ChoiceGroup类 在表单中创建复选框和单选框; 一个ChoiceGroup类的实例可以是单选(exclusive)和多选(multiple)这两个类型中的一种; 一个单选的实例是作为一组单选按钮出现的; 多选实例则包含了一个或一组复选框; 通过ChoiceGroup类的构造函数的选择类型参数来决定ChoiceGroup类的实例的类型。

ChoiceGroup类和List对象的选择类型: 

ChoiceGroup类和List对象的选择类型 Choice Type 说明 EXCLUSIVE 每次只能选择一个(单选按钮) MULTIPLE 每次可选0或多个 IMPLICIT 每次只选择一个,这个选择自动生成一 个命令事件,不使用图标。

ChoiceGroup类: 

ChoiceGroup类 当用户选择了一个单选按钮或复选框时,设备的应用管理器就会探测到这个事件,并且调用MIDlet的itemStateChanged()方法; itemStateChanged()方法判断选择的表项是否为ChioceGroup的实例; 如果是,则必须调用getSelectedFlag()方法或getSelectedIndex()方法来获得用户选择的选项; getSelectedFlag()方法返回一个数组,这个数组包含了每个ChoiceGroup类的实例的成员的选择状态标记的取值; true — 表示用户选定了数组元素索引所对应的单选按钮或复选框; false —表示用户选定了数组元素索引所对应的单选按钮或复选框; getSelectedIndex()方法返回的是用户选择的选项的索引数,可以将这个索引数作为参数传给getString()方法,获取被选择的单选按钮或复选框所代表的文本。

创建和访问复选框: 

创建和访问复选框

Slide22: 

public class CheckBoxes extends MIDlet implements CommandListener { private Display display; private Form form; private Command exit; private Command process; private ChoiceGroup movies; private int movieIndex; public CheckBoxes() { display = Display.getDisplay(this); movies = new ChoiceGroup("Select Movies You Like to See", Choice.MULTIPLE); movies.append("Action", null); movies.append("Romance", null); movies.append("Comedy", null); movies.append("Horror", null); exit = new Command("Exit", Command.EXIT, 1); process = new Command("Process", Command.SCREEN,2); form = new Form("Movies"); movieIndex = form.append(movies); form.addCommand(exit); form.addCommand(process); form.setCommandListener(this); }

Slide23: 

public void commandAction(Command command, Displayable displayable) { if (command == process){ boolean picks[] = new boolean[movies.size()]; StringItem message[] = new StringItem[movies.size()]; movies.getSelectedFlags(picks); for (int x = 0; x < picks.length; x++) { if (picks[x]){ message[x] = new StringItem ("",movies.getString(x)+"\n"); form.append(message[x]); } } form.delete(movieIndex); form.removeCommand(process); } else if (command == exit) { destroyApp(false); notifyDestroyed(); } }

创建和访问单选按钮: 

创建和访问单选按钮

Slide25: 

private Display display; private Form form; private Command exit; private Command process; private ChoiceGroup gender; private int currentIndex; private int genderIndex; public RadioButtons() { display = Display.getDisplay(this); gender = new ChoiceGroup("Enter Gender", Choice.EXCLUSIVE); gender.append("Female", null); currentIndex = gender.append("Male ", null); gender.setSelectedIndex(currentIndex, true); exit = new Command("Exit", Command.EXIT, 1); process = new Command("Process", Command.SCREEN,2); form = new Form("Gender"); genderIndex = form.append(gender); form.addCommand(exit); form.addCommand(process); form.setCommandListener(this); }

Slide26: 

public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(false); notifyDestroyed(); } else if (command == process) { currentIndex = gender.getSelectedIndex(); StringItem message = new StringItem("Gender: ", gender.getString(currentIndex)); form.append(message); form.delete(genderIndex); form.removeCommand(process); } }

DateField类: 

DateField类 使用DateField类可以在MIDlet中显示、编辑或输入日期和时间; 3个参数: 一个特殊的标签 模式 时区 — 可选的 DateField datefield = new DateField("Today", DateField.DATE); DateField datefield = new DateField("Time", DateField.TIME, timeZone);

DateField的模式: 

DateField的模式 模式 说明 DATE 显示、编辑和输入日期 TIME 显示、编辑和输入时间 DATE_TIME 显示、编辑和输入日期与时间

DateField类: 

DateField类 通过一个DateField类的实例,使用DateField类的方法在日期域中输入日期和时间,获得已经输入到日期域的日期和时间值; 调用setDate()方法,在日期域中设置日期或时间; setDate()需要一个参数,为在日期域中显示的包含日期或时间值的Date类的实例。

Example: 显示系统的时间: 

Example: 显示系统的时间

Slide31: 

private Display display; private Form form; private Date today; private Command exit; private DateField datefield; public DateToday() //constructor { display = Display.getDisplay(this); form = new Form("Today's Date"); today = new Date(System.currentTimeMillis()); datefield = new DateField("", DateField.DATE_TIME); datefield.setDate(today); exit = new Command("Exit", Command.EXIT, 1); form.append(datefield); form.addCommand(exit); form.setCommandListener(this); }