J2ME学习――RMS从入门到精通之一 (2)
import javax.microedition.lcdui.alert;
import javax.microedition.lcdui.alerttype;
import javax.microedition.lcdui.display;
import javax.microedition.midlet.midlet;
import javax.microedition.midlet.midletstatechangeexception;
import javax.microedition.rms.recordstoreexception;
public class rmsanalyzer extends midlet
{
private display display;
private countercanvas countercanvas;
private alert alert;
protected void startapp() throws midletstatechangeexception
{
display = display.getdisplay(rmsanalyzer.this);
alert = new alert("错误提示");
try
{
string interval = this.getappproperty("inter");
int t = integer.parseint(interval);
countercanvas = new countercanvas(t, 1, this);
}
catch (recordstoreexception e)
{
this.showalerterror(e.getmessage());
}
display.setcurrent(countercanvas);
}
public display getdisplay()
{
return display;
}
protected void pauseapp()
{
}
protected void destroyapp(boolean arg0) throws midletstatechangeexception
{
}
public void showalerterror(string message)
{
alert.setstring(message);
alert.settype(alerttype.error);
alert.settimeout(3000);
display.setcurrent(alert);
}
}
import java.util.timer;
import java.util.timertask;
import javax.microedition.lcdui.canvas;
import javax.microedition.lcdui.command;
(来源www.iocblog.net)
import javax.microedition.lcdui.commandlistener;
import javax.microedition.lcdui.displayable;
import javax.microedition.lcdui.graphics;
import javax.microedition.midlet.midletstatechangeexception;
import javax.microedition.rms.*;
public class countercanvas extends canvas implements commandlistener
{
private rmsmodel model;
private rmsanalyzer rmsanalyzer;
private int intertime;
private int counter;
private boolean go = true;
public static command backcommand = new command("退出", command.exit, 3);
public static final int inc = 1;
public final timer timer = new timer();
public countercanvas(int intertime, int base, rmsanalyzer rmsa)
throws recordstoreexception
{
this.intertime = intertime;
this.counter = base;
this.rmsanalyzer = rmsa;
model = new rmsmodel(base, rmsanalyzer);
this.addcommand(backcommand);
this.setcommandlistener(this);
timertask timertask = new timertask()
{
public void run()
{
try
{
model.writerecord(inc);
counter++;
} catch (recordstorefullexception e)
{
go = false;
model.deleterms();
timer.cancel();
} catch (recordstoreexception e)
{
model.deleterms();
rmsanalyzer.showalerterror(e.getmessage());
timer.cancel();
}
repaint();
}
};
timer.schedule(timertask, 1000, intertime);
}
public void setcounter(int counter)
{
this.counter = counter;
}
public void setintertime(int intertime)
{
this.intertime = intertime;
}
protected void paint(graphics arg0)
{
int screen_width = this.getwidth();
int screen_height = this.getheight();
arg0.drawrect(screen_width / 10, screen_height / 2,
screen_width * 4 / 5, 10);
if(rmsanalyzer.getdisplay().iscolor())
{
arg0.setcolor(128, 128, 255);
}
arg0.fillrect(screen_width / 10, screen_height / 2, counter, 10);
if (!go)
(来源www.iocblog.net)
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。