使用Eclipse 为Nokia手机开发移动应用程序(1)
作者: 来源:it167 点击: 日期:2007-01-28 |
|
|
WelcomeScreen类
WelcomeScreen类扩展了Form类来展示包含一些高级MIDP UI组件的屏幕。正如您已经看到的,WelcomeScreen的软键命令和事件处理程序被添加在TutorialMidlet类中。可以通过JavaBeans风格的get和set方法,从这个类的外部访问nameField UI组件中的数据。以这种形式使用的图像来自外部的PNG图像文件welcome.png。该文件必须位于midlet的运行时类路径中,以便对它进行访问。
package tutorial;
import javax.microedition.lcdui.*;
public class WelcomeScreen extends Form {
private TextField nameField;
public WelcomeScreen () {
super ("Welcome");
Image img;
// Construct the image from the media file
try {
img = Image.createImage("/welcome.png");
} catch (Exception e) {
e.printStackTrace ();
img = null;
}
ImageItem imageItem =
new ImageItem ("", img,
ImageItem.LAYOUT_CENTER, "Welcome");
nameField =
new TextField ("Please enter your name", "",
10, TextField.ANY);
append (imageItem);
append (nameField);
}
public void setName (String n) {
nameField.setString (n);
}
public String getName () {
return nameField.getString ();
}
}
|
HelloScreen类
HelloScreen类扩展了Canvas类来展示必须通过应用程序自身得到渲染的屏幕。paint()方法重新绘制了整个屏幕,每次屏幕需要更新时,都由系统调用该方法。再次声明,HelloScreen的软键命令和事件处理程序被添加在TutorialMidlet类中。屏幕上渲染的名称字符串是通过setName()方法在显示该屏幕之前设置的。图像文件hello.png也必须位于midlet运行时类路径中。
package tutorial;
import javax.microedition.lcdui.*;
public class HelloScreen extends Canvas {
private int width, height;
private String name;
private Image img;
public HelloScreen () {
width = getWidth ();
height = getHeight ();
name = "unknown";
// Construct the image from the media file
try {
img = Image.createImage("/hello.png");
} catch (Exception e) {
e.printStackTrace ();
img = null;
}
}
public void setName (String n) {
name = n;
}
// Paint the screen based on the name
protected void paint (Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
g.setColor(0x000000);
g.drawImage (img, width / 2, height / 4,
Graphics.VCENTER | Graphics.HCENTER);
g.setFont(Font.getFont(
Font.FACE_PROPORTIONAL,
Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString (name, width / 2, height * 3/4,
Graphics.BASELINE | Graphics.HCENTER);
}
}
|
不再有错误
键入所有的源代码,如果Eclipse没有显示任何错误或不一致,您就已经为运行midlet做好了准备!

图17:没有错误,运行midlet
|