跳至主目录
J2ME:循序渐进 下载教程 zip 文件英文原文
主菜单章节菜单给出此教程的反馈意见上一屏下一屏
第八章:使用 KJava GUI 组件的开发
  


HelloWorld -- 完整的代码清单第 4 页(共 6 页)


以下便是 Palm 设备的 HelloWorld 应用程序的完整代码示例:


      import com.sun.kjava.Button;
      import com.sun.kjava.Graphics;
      import com.sun.kjava.Spotlet;

      /**
       * Simple demonstration, "Hello World" program. Note that Spotlet is
       * the class that provides callbacks for event handling.
       */
      public class HelloWorld extends Spotlet
      {
         /** Stores a reference to the "Exit" button. */
         private static Button exitButton;

         /**
          * Main entry point for this program.
          */
         public static void main(String[] args)
         {
            (new HelloWorld()).register(NO_EVENT_OPTIONS);
         }

         /**
          * Constructor: draws the screen.
          */
         public HelloWorld()
         {
            // Create (initially invisible) the "Exit" button
            exitButton = new Button("Exit",70,145);

            // Get a reference to the graphics object;
            // i.e. the drawable screen
            Graphics g = Graphics.getGraphics();
            g.clearScreen();

            // Draw the text, "Hello World!" somewhere near the center
            g.drawString("Hello World!", 55, 45, g.PLAIN);

            // Draw the "Exit" button
            exitButton.paint();
         }

         /**
          * Handle a pen down event.
          */
         public void penDown(int x, int y)
         {
            // If the "Exit" button was pressed, end this application
            if (exitButton.pressed(x,y))
              System.exit(0);
         }
      }

主菜单章节菜单给出此教程的反馈意见上一屏下一屏