J2ME时间显示

这是一个j2me的时间显示的例子。一个获取时间并且按秒计时的时间显示类,在j2me中创建对象
并且创建一个线程让时间占用这个线程进行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
public Shijianxianshi() {
display=Display.getDisplay(this);
canvas=new testCanvas();
quit=new Command("Quit",Command.EXIT,2);
canvas.addCommand(quit);
canvas.setCommandListener(this);
}
<span style="color: #ff6600;">protected void startApp()</span> throws MIDletStateChangeException {
display.setCurrent(canvas);
}
protected void destroyApp(boolean unconditional)throws MIDletStateChangeException {
// TODO 自动生成方法存根
}
<span style="color: #ff6600;">protected void pauseApp()</span>
{
// TODO 自动生成方法存根
}
<span style="color: #ff6600;">public void commandAction</span>(Command c, Displayable d)
{
try
{
if(c == quit)
{
<span style="color: #ff6600;">destroyApp(true);</span>
notifyDestroyed();
}
}
catch(MIDletStateChangeException me)
{
System.out.println(me+"caught.");
}
}
}
<span style="color: #ff6600;">class testCanvas extends Canvas implements Runnable</span>{
private Image image=null;
private Font font;
TimeZone t;
Calendar c;
int h,  min, sec, year, month, day, week=0;
public testCanvas()
{
t = TimeZone.getTimeZone("GMT+08:00");//获取东8区TimeZone 否则会使用默认的时区
c = Calendar.getInstance(t); //创建一个日历对象
h = c.get(Calendar.HOUR_OF_DAY); //从日历对象获取想要的日期、时间信息
min = c.get(Calendar.MINUTE);
sec = c.get(Calendar.SECOND);
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
week=c.get(Calendar.DAY_OF_WEEK);
}
// int h = c.get(Calendar.HOUR_OF_DAY); //从日历对象获取想要的日期、时间信息
<span style="color: #ff6600;">protected void paint</span>(Graphics g){
<span style="color: #ff6600;">new Thread(this).start();</span>
font=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
g.setFont(font);
String hour;
String minute;
String second;
String m;
String d;
String w = null;
switch(week){
case 1: w=" 一";
case 2: w=" 二";
case 3: w=" 三";
case 4: w=" 四";
case 5: w=" 五";
case 6: w=" 六";
case 7: w=" 日";
}
if(h&lt;10)
{
hour="0"+String.valueOf(h);
}
else
{
hour=String.valueOf(h);
}
if(min&lt;10)
{
minute="0"+String.valueOf(min);
}
else
{
minute=String.valueOf(min);
}
if(sec&lt;10)
{
second="0"+String.valueOf(sec);
}
else
{
second=String.valueOf(sec);
}
String y=String.valueOf(year);
if(month+1&lt;10)
{
m="0"+String.valueOf(month+1);
}
else
{
m=String.valueOf(month+1);
}
if(day&lt;10)
{
d="0"+String.valueOf(day);
}
else
{
d=String.valueOf(day);
}
try {
image=Image.createImage("/2.png");
} catch (IOException ex) {
System.out.println("图片导入失败");
}
g.drawImage(image, 0, 0, Graphics.HCENTER|Graphics.TOP);
g.drawString("我的时钟", getWidth()/2, 20, Graphics.HCENTER|Graphics.TOP);
g.drawString("当前的日期为:",60,60,Graphics.HCENTER|Graphics.TOP);
g.drawString("星期:",30,100,Graphics.HCENTER|Graphics.TOP);
g.drawString("当前的时间为:",60,130,Graphics.HCENTER|Graphics.TOP);
g.drawString(hour,50,150,Graphics.HCENTER|Graphics.TOP);//时间的显示
g.drawString(":",65,150,Graphics.HCENTER|Graphics.TOP);
g.drawString(minute,80,150,Graphics.HCENTER|Graphics.TOP);
g.drawString(":",95,150,Graphics.HCENTER|Graphics.TOP);
g.drawString(second,110,150,Graphics.HCENTER|Graphics.TOP);
g.drawString(w,60,100,Graphics.HCENTER|Graphics.TOP);//星期的显示
g.drawString(y,50,80,Graphics.HCENTER|Graphics.TOP);//年月的显示
g.drawString("/",75,80,Graphics.HCENTER|Graphics.TOP);
g.drawString(m,90,80,Graphics.HCENTER|Graphics.TOP);
g.drawString("/",105,80,Graphics.HCENTER|Graphics.TOP);
g.drawString(d,120,80,Graphics.HCENTER|Graphics.TOP);
if(sec&lt;60)
{
sec++;
}
else
{
sec=0;
if(min&lt;60)
{
min++;
}
else
{
min=0;
if(h&lt;13)
h++;
else
h=1;
}
}
}
<span style="color: #ff6600;">public void run() {</span>
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
// ex.printStackTrace();
}
<span style="color: #ff6600;">repaint();</span>
}
}

备注:以上高亮颜色显示的为重要类与方法,供大家借鉴。我已在自己的java手机上运行过,没有问题的。


1.如果本文帮到了您,不妨点一下右下角的 分享到 按钮。
2.除非注明,本博文章均为原创,转载请以链接形式标明本文地址。
3.本博文章只代表博主当时的观点或结论,与博主公司没有任何关系。