以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——数字时钟  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=575)

--  作者:admin
--  发布时间:2008/10/23 22:16:56
--  程序代码——数字时钟

import java.util.Calendar;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
        private Display display;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {

                display.setCurrent(new DrawPanel());
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }
}

class DrawPanel extends Canvas {
        String time = "";

        Thread thread = new Thread(new ActionThread());

        public DrawPanel() {
                thread.start();
        }

        public void paint(Graphics g) {
                g.setColor(255, 255, 255);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(0, 0, 0);
                Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
                                Font.SIZE_LARGE);
                g.setFont(font);
                int fontWidth = font.stringWidth(time);
                int fontHeight = font.getHeight();
                g.drawString(time, getWidth() / 2 - fontWidth / 2, getHeight() / 2
                                - fontHeight / 2, Graphics.TOP | Graphics.LEFT);
        }

        class ActionThread implements Runnable {
                final int beijingZone = 8;

                public void run() {
                        while (true) {
                                Calendar rightNow = Calendar.getInstance();
                                int hour = rightNow.get(Calendar.HOUR_OF_DAY) + beijingZone;
                                int minute = rightNow.get(Calendar.MINUTE);
                                int second = rightNow.get(Calendar.SECOND);
                                time = "";
                                String sh = String.valueOf(hour);
                                if (sh.length() == 1)
                                        time = time + "0";
                                time = time + sh + ":";

                                sh = String.valueOf(minute);
                                if (sh.length() == 1)
                                        time = time + "0";
                                time = time + sh + ":";

                                sh = String.valueOf(second);
                                if (sh.length() == 1)
                                        time = time + "0";
                                time = time + sh;

                                repaint();
                                try {
                                        Thread.sleep(100);
                                } catch (Exception e) {
                                        System.out.println(e.getMessage());
                                }
                        }
                }
        }
}

[此贴子已经被作者于2010-12-12 18:39:45编辑过]