時限タイマーを作ってみよう5
プログラムの試作(プロトタイプと言ったりします)が完成しても
エラー処理や改良に3倍以上の労力がかかったりします。
毎度、管理人イガジーです。
TimeBoxの表示ができたら、あとはそれをタイマーで動かせば
キモ(核)の部分は出来上がります。
フィールドにTimer timer=null; を追加して
class TimerProc extends TimerTask {
@Override
public void run() {
timer=null;
tmsg.visibletime();
}
}
という感じのタイマータスクを作ります。
Startボタンが押された時の処理を
startbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int dt=0;
try {
dt=Integer.parseInt(delay.getText().trim());
} catch (NumberFormatException e1) {
dt=0;
}
if (dt<1) alart.visible();
else {
if (timer==null) {
tmsg.lb.setText(msg.getText());
if (unit.getSelectedIndex()==1) dt*=1000; // second
else dt*=60000; // minute
timer=new Timer();
timer.schedule(new TimerProc(), dt);
}
}
}
});
としてやれば、時限タイマーとしての動作をするようになります。
if (timer==null) {
の判定は、タイマー動作中に[Start]ボタンが押された時に
2重起動しないようにするためのものです。
全コードは次のような感じです。
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
public class VTimer4 {
Frame f;
MsgBox alart;
TimeBox tmsg;
TextField msg,delay;
Button startbtn;
Choice unit;
Timer timer=null;
VTimer4(){
f=new Frame("Set Timer 4");
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
tmsg=new TimeBox("時間のテスト");
alart=new MsgBox("1以上の数字を入力してください");
f.setLayout(new GridLayout(2,1));
Panel p0=new Panel();
p0.add(new Label("表示文"));
msg=new TextField("時間です",20);
p0.add(msg);
Panel p1=new Panel();
delay=new TextField(" 3",3);
p1.add(delay);
unit=new Choice();
unit.add("分後");
unit.add("秒後");
p1.add(unit);
startbtn=new Button(" Start ");
startbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int dt=0;
try {
dt=Integer.parseInt(delay.getText().trim());
} catch (NumberFormatException e1) {
dt=0;
}
if (dt<1) alart.visible();
else {
if (timer==null) {
tmsg.lb.setText(msg.getText());
if (unit.getSelectedIndex()==1) dt*=1000; // second
else dt*=60000; // minute
timer=new Timer();
timer.schedule(new TimerProc(), dt);
}
}
}
});
p1.add(startbtn);
p1.add(new Label(" ")); // スペーサ
Button btn_end=new Button("End");
btn_end.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
p1.add(btn_end);
f.add(p0);
f.add(p1);
f.setSize(280,120);
f.setVisible(true);
}
class MsgBox extends Frame {
private static final long serialVersionUID = 1L;
Label lb;
Button btn;
MsgBox md;
public MsgBox(String message) {
super("Information");
md=this;
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent arg0) {
md.setVisible(false);
}
});
lb=new Label(message); // for message
lb.setFont(new Font(Font.DIALOG,Font.PLAIN,18));
lb.setAlignment(Label.CENTER);
this.add(lb,BorderLayout.CENTER);
btn=new Button(" OK ");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
md.setVisible(false);
}
});
Panel p2=new Panel();
p2.add(btn);
this.add(p2,BorderLayout.SOUTH);
this.setSize(340,140);
}
Point scenter(Dimension fsz) {
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
int x=(screensize.width-fsz.width)/2;
int y=(screensize.height-fsz.height)/2;
Point pos=new Point(x,y);
return pos;
}
void visible() {
Point cpos=scenter(md.getSize());
cpos.y-=40;
md.setLocation(cpos);
md.setVisible(true);
}
}// MsgBox
class TimeBox extends MsgBox {
private static final long serialVersionUID = 1L;
Label tm;
public TimeBox(String message) {
super(message);
this.setTitle("The Time");
tm=new Label();
tm.setFont(new Font(Font.MONOSPACED,Font.BOLD,24));
tm.setForeground(Color.BLUE);
tm.setAlignment(Label.CENTER);
this.add(tm,BorderLayout.NORTH);
}
void visibletime(){
Calendar cc=new GregorianCalendar();
int hh=cc.get(Calendar.HOUR_OF_DAY);
int mm=cc.get(Calendar.MINUTE);
tm.setText(String.format("%02d:%02d", hh,mm));
visible();
}
}
class TimerProc extends TimerTask {
@Override
public void run() {
timer=null;
tmsg.visibletime();
}
}
public static void main(String[] args) {
new VTimer4();
}
}
実際に使ってみると、色々な不満が出てきます。
・[Start]したら、その後でキャンセルできない
・[Start]しても設定画面が残っているので、紛らわしい
・TimeBoxが表示された後、[OK]押さずに [Start]が押せる。その時もTimeBoxは出たままで紛らわしい。
・「1以上の数字を入力してください」が出たままで、数値を変更して[Start]が押せる
など。
不満があっても気にしないのもひとつの手段ですが、ぼちぼち対応してみましょう。
この記事へのコメントはこちら