您的位置:首页 > 产品设计 > UI/UE

解读 LWUIT 之二:关于 LWUIT 开发指南中的 Hello World

2010-02-20 16:46 525 查看
解读 LWUIT 之二:关于 LWUIT 开发指南中的 Hello World

作者写的 Hello World 源代码下载

LWUIT 开发指南下载

虎年第一篇博客。首先祝大家新年快乐,在新的一年里事业虎虎生威!

打开官网下载得到的 LWUIT_1_3.zip 安装文件解压缩后的 docs 目录下的 LWUIT_Developer_Guide.pdf,见第一章里有个 Hello World 例子,而且还给出了 Hello World 运行后的效果图。无奈虽然给出源码,没有说明相关的 .res 文件怎么编辑,以至于运行起来的 Hello World 是一个“白纸黑字”的尤物。虽然 docs 同目录下还有个 Binary Resource File Spec.pdf 介绍说明,却一时半会也弄不明白其中的奥妙。谷歌搜索,关于
.res 文件的编辑说明也是少之又少。得,自己动手写吧。折腾了一上午,终于跑起来一个和官方效果图一样的 Hello World,作者在这里把编辑过程记录下来,只希望可以方便更多的 LWUIT 爱好者。其中的不足之处,恳请 LWUIT 老手们出来批评指正。

一、新建一 J2ME 项目,名“HelloLwuit”,导入 LWUIT 包,拷贝官方给的 Hello World 源代码。

package com.defonds.lwuit;

import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

public class HelloMidlet extends javax.microedition.midlet.MIDlet {
public void startApp() {
// init the LWUIT Display
Display.init(this);
// Setting the application theme is discussed
// later in the theme chapter and the resources chapter
try {
Resources r = Resources.open("/myresources.res");
UIManager.getInstance().setThemeProps(
r.getTheme(r.getThemeResourceNames()[0]));
} catch (java.io.IOException e) {}
Form f = new Form();
f.setTitle("Hello World");
f.setLayout(new BorderLayout());
f.addComponent("Center", new Label("I am a Label"));
f.show();
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}
}


二、拿到 Hello World 中需要的背景图片 orange.jpg 和 bar.jpg。

orange.jpg 如下:



bar.jpg 如下:



三、使用 LWUIT_1_3.zip 解压目录下 util 目录中的 ResourceEdit.exe(windows 以外的平台下可以打开同目录下的 ResourceEditor.jar 文件)新建 myresources.res。详细步骤:

1、双击打开 ResourceEdit.exe 编辑器。



2、将背景图片 orange.jpg 和 bar.jpg 导入。点击 ResourceEdit 左侧 Images 页项,点击“Add Image”按钮,Name 输入“bar”,确定,然后在弹出的对话框中找到你的 bar.jpg 并将其导入。



同样操作将 orange.jpg 也导入进来。



3、自定义文字显示格式。点击 ResourceEdit 左侧 Fonts 页项,点击“+”按钮,Name 输入“myfont”,确定即开始对字体进行定义。作者定义的字体格式如下:



4、自定义主题格式。点击 ResourceEdit 左侧 Themes 页项,开始主题定义。点击 ResourceEdit 右侧左下方“Add”按钮依次添加自定义主题格式。作者自定义后的主题格式如下:



作者自定义各个主题格式选项说明:

bgColor:Hello World 中 Label 的背景色;

fgColor:Hello World 中 Label 的文字显示颜色;

font:Hello World 中 Label 的文字显示格式;

Form.bgImage:Hello World 中 Form 的背景图片;

Title.bgImage:Hello World 中标题的背景图片;

Title.fgColor:Hello World 中标题文字颜色;

Title.font:Hello World 中标题文字显示格式。


定义这些要求有一定的美工背景。没有一点美工底子的作者也只能暂时定义成这个模样了。

5、保存。ResourceEdit 中在 File 菜单栏中选择“Save As”将定义好的 res 文件保存在 HelloLwuit 项目的 src 目录下,名字取为“myresources.res”。

刷新 HelloLwuit 项目,运行 HelloMidlet.java,运行效果图如下图所示。



基本和官方开发指南中给的效果图一样,将就着看吧,呵呵。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: