import java.util.Calendar; import java.util.Date; import com.nttdocomo.ui.*; import com.nttdocomo.util.Timer; import com.nttdocomo.util.TimerListener; public class Watch extends IApplication implements SoftKeyListener,ComponentListener,TimerListener { /* --- Field --- */ private Panel panel; private Label now; private Button button; private TextBox snapshot; private Timer timer; /* --- Constructor --- */ public Watch() { } /* --- 開始 --- */ public void start() { panel=new Panel(); panel.add(now=new Label("",Label.CENTER)); panel.add(button=new Button("計測")); panel.add(snapshot=new TextBox("",10,1,TextBox.DISPLAY_ANY)); panel.setSoftLabel(Panel.SOFT_KEY_1,"終了"); panel.setSoftKeyListener(this); panel.setComponentListener(this); Display.setCurrent(panel); timer=new Timer(); timer.setListener(this); timer.setRepeat(true); timer.setTime(500); timer.start(); } /* --- 定期処理 --- */ public void timerExpired(Timer source) { now.setText(getTime()); } private String getTime() { Calendar calendar; String time; calendar=Calendar.getInstance(); calendar.setTime(new Date()); time=calendar.get(Calendar.HOUR_OF_DAY)+":"; time=time+(calendar.get(Calendar.MINUTE)<10 ? "0" : ""); time=time+calendar.get(Calendar.MINUTE)+":"; time=time+(calendar.get(Calendar.SECOND)<10 ? "0" : ""); time=time+calendar.get(Calendar.SECOND); return time; } /* --- イベント --- */ public void componentAction(Component source,int type,int param) { if (source==button && type==ComponentListener.BUTTON_PRESSED) { snapshot.setText(getTime()); } } public void softKeyPressed(int softKey) { if (softKey==Frame.SOFT_KEY_1) { timer.stop(); timer.dispose(); terminate(); } } public void softKeyReleased(int softKey) {} }