J2ME入门-(6)使用K
java GUI组件的开发
第七章:使用K
java GUI组件的开发
介绍
本章中,我们来学习如何使用 K
Java API 进行 GUI 开发。首先看一下 K
Java GUI 开发的介绍,然后使用 K
Java API 开发我们的第一个 J2ME 应用程序。HelloWorld ,将示范一个使用
CLD
C 的简易 J2ME 应用程序、K
Java 简表以及 Palm 操作系统的 KVM。
在下一章我们继续进行 K
Java GUI 开发,构建另一个应用程序并重点学习事件处理模块。
Spotlet 介绍
K
Java API 提供了一套开发 Palm 操作系统设备应用程序的
c.
com"
class="wordstyle">类。K
Java 提供了一个 Spotlet
c.
com"
class="wordstyle">类,
com.sun.k
java.Spotlet,它和 J2SE
Canvas
c.
com"
class="wordstyle">类在添加用于事件处理的回调
c.
com"
class="wordstyle">方法上
c.
com"
class="wordstyle">类似。因此,应用程序可以扩展 Spotlet
c.
com"
class="wordstyle">类,不使用合适的事件处理
c.
com"
class="wordstyle">方法也可提供需要的功能。
应用程序可以创建并使用多个 spotlets 来显示不同的窗口。就像使用 J2SE
Canvas(一个负责画出自身以及放置在其上的 GUI 控件的 spotlet)一样。
在我们的两个 K
Java 示例中,都将使用 Spotlet
c.
com"
class="wordstyle">类。这两个示例中一个是很快就看到的 HelloWorld 应用程序,另一个是 S
cribble 应用程序,后者将在使用 K
Java 事件处理的开发这一章中构建。
K
Java 应用程序 HelloWorld
这个应用程序将在屏幕中央显示 "Hello World!" 和一个 Exit 按钮,按下后即终止该应用程序。HelloWorld.
java 开始时使用下面的几行代码导入将在后面的 HelloWorld
c.
com"
class="wordstyle">类中使用的
c.
com"
class="wordstyle">类:
import
com.sun.k
java.Button;
import
com.sun.k
java.Graphi
cs;
import
com.sun.k
java.Spotlet;
下面的代码行将 HelloWorld
c.
com"
class="wordstyle">类定义为扩展 Spotlet:
publi
c class HelloWorld extends Spotlet
请记住 Spotlet
c.
com"
class="wordstyle">类提供用于处理事件的回调功能。在这个简单的示例中,我们只对一个事件感兴趣,即用户何时按下 Exit 按钮。下一个代码行存储对 Exit 按钮的引用:
private stati
c Button exitButton;
如同在 J2SE 中一样,main()
c.
com"
class="wordstyle">方法定义程序的主要入口点。对于 J2ME 应用程序,main 也定义了入口点。在本例中,main() 创建了一个新的 HelloWorld
c.
com"
class="wordstyle">类的实例,它运行我们的应用程序。
publi
c stati
c void main(String[] args)
{
(new HelloWorld()).register(NO_EVENT_OPTIONS);
}
下一个代码块定义了构造程序。在构造程序中,我们首先创建一个 Button 并为其加上 "Exit" 标签。按钮起初是不可见的。当我们得到对图形
对象的引用后,此按钮成了一个可画的屏幕,先清屏然后在屏幕中央画出文本 "Hello World!"。最后,我们在屏幕上添加 Exit 按钮。
publi
c HelloWorld()
{
//
Create (initially invisible) the "Exit" button
exitButton = new Button("Exit",70,145);
// Get a referen
ce to the graphi
cs obje
ct;
// i.e. the drawable s
creen
Graphi
cs g = Graphi
cs.getGraphi
cs();
g.
clearS
creen();
// Draw the text, "Hello World!" somewhere near the
center
g.drawString("Hello World!", 55, 45, g.PLAIN);
// Draw the "Exit" button
exitButton.paint();
}
最后,我们定义 penDown 事件处理程序,用来简单地检查 Exit 按钮是否被按下。如果已按下,就退出应用程序。
publi
c void penDown(int x, int y)
{
// If the "Exit" button was pressed, end this appli
cation
if (exitButton.pressed(x,y))
System.exit(0);
}
HelloWorld -- 完整的代码清单
以下便是 Palm 设备的 HelloWorld 应用程序的完整代码示例:
import
com.sun.k
java.Button;
import
com.sun.k
java.Graphi
cs;
import
com.sun.k
java.Spotlet;
/**
* Simple demonstration, "Hello World" program. Note that Spotlet is
* the
class that provides
callba
cks for event handling.
*/
publi
c class HelloWorld extends Spotlet
{
/** Stores a referen
ce to the "Exit" button. */
private stati
c Button exitButton;
/**
* Main entry point for this program.
*/
publi
c stati
c void main(String[] args)
{
(new HelloWorld()).register(NO_EVENT_OPTIONS);
}
/**
*
Constru
ctor: draws the s
creen.
*/
publi
c HelloWorld()
{
//
Create (initially invisible) the "Exit" button
exitButton = new Button("Exit",70,145);
// Get a referen
ce to the graphi
cs obje
ct;
// i.e. the drawable s
creen