時限タイマーを作ってみよう4

 

1度書いた(作った)プログラムは、できるだけ流用して
同じ事を2度3度書かないようにしたいものです。

毎度、管理人イガジーです。

あるクラス(プログラム)を元に、変更・追加するのに、java では
extends (継承)という手段を使います。
時限タイマーを作る過程で、メッセージを表示するクラス MsgBox を
作りましたが、時間を表示するクラス(TimeBoxと命名しましょう)は
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();
	}
}

MsgBoxに無い Label tm の処理だけを追加しているのが分かりますよね。
super(message); というのが、元となる(親クラスとも呼びます)MsgBoxの呼び出しです。
MsgBoxを元にしているので、[OK]ボタンを設置したり、メッセージを
表示したりするための処理は書く必要がありません。
これが、継承(extends)のうれしいところです。

ここまでの全体を以下に示します。
時間指定の(初期値は 3 になってる部分)を 0 や空白にしたら
「1以上の数字を入力してください」を表示します(MsgBox)。
1以上の数字だったら、時刻を表示します(TimeBox)。

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;


public class VTimer3 {
	Frame f;
	MsgBox alart;
	TimeBox tmsg;
	TextField msg,delay;
	Button startbtn;
	Choice unit;

	VTimer3(){
		f=new Frame("Set Timer 3");
		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;
				dt=Integer.parseInt(delay.getText().trim());

				if (dt<1) alart.visible();
				else tmsg.visibletime();
			}
		});
		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();
		}
	}

	public static void main(String[] args) {
		new VTimer3();
	}

}

継承(extends)って便利だな、と感じて頂ければ幸いです。

この記事へのコメントはこちら

メールアドレスは公開されませんのでご安心ください。
また、* が付いている欄は必須項目となりますので、必ずご記入をお願いします。

内容に問題なければ、下記の「コメント送信」ボタンを押してください。

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)