/////// TimerControls.java /////// import java.awt.*; import java.awt.event.*; import java.util.StringTokenizer; /** the controls of a timer that buzzes when the set time expires sets the timer (input TextField) starts and stops the timer (start/stop button) */ public class TimerControls extends Panel { public TimerControls(Timer t, Color bg) { tm = t; add(st = new TextField("00 : 00", 7)); ss = new Button("Start"); Font f = new Font("TimesRoman", Font.BOLD, 22); ss.setFont(f); add(ss); st.setBackground(bg); ss.setBackground(bg); st.addActionListener(new SetHandler()); ss.addActionListener(new SsHandler()); } protected TimerControls() { } protected boolean parseString(String s, int[] ms) { StringTokenizer tk = new StringTokenizer(s, " :"); int i = tk.countTokens(); if ( i == 2 ) { ms[0]=Integer.parseInt(tk.nextToken()); ms[1]=Integer.parseInt(tk.nextToken()); return ( ms[0] >=0 && ms[1] >= 0 && ms[0]+ms[1] > 0); } return false; } protected int[] setting = new int[2]; protected Button ss; protected TextField st; protected Timer tm; private final class SetHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if ( parseString( st.getText(), setting) ) { tm.set(setting[0], setting[1]); ss.setLabel("Start"); } } } private final class SsHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if ( tm.beginEnd() ) ss.setLabel("Stop"); else ss.setLabel("Start"); } } }