import javax.swing.*; import java.awt.*; import java.awt.event.*; /** This class tests the MyClock component */ class TestMyClock extends JFrame { MyClock mc; // the clock we are testing public TestMyClock () { Container c = getContentPane(); c.setLayout(null); // Add the clock to the contentPane mc = new MyClock(); mc.setLocation(20, 20); c.add(mc); // Add a button to start the clock ticking JButton start = new JButton ("Start"); start.setSize(70, 30); //start.setPreferredSize(new Dimension(70, 30)); //start.setMaximumSize(new Dimension(70, 30)); //start.setMinimumSize(new Dimension(70, 30)); start.setLocation(20, 300); start.addActionListener(new TickerHandler(true)); c.add(start); // Add a button to stop the clock JButton stop = new JButton ("Stop"); stop.setSize(70, 30); //stop.setPreferredSize(new Dimension(70, 30)); //stop.setMaximumSize(new Dimension(70, 30)); //stop.setMinimumSize(new Dimension(70, 30)); stop.setLocation(100, 300); stop.addActionListener(new TickerHandler(false)); c.add(stop); setSize(205, 400); setDefaultCloseOperation (EXIT_ON_CLOSE); validate(); show(); } // This class handles the button clicks to start and stop the clock. class TickerHandler implements ActionListener { boolean runit; // should we start or stop // The constructor requires us to say if are a starter or stopper public TickerHandler(boolean runit) { this.runit = runit; } // dispatch the event to the clock public void actionPerformed (ActionEvent e) { mc.setTicking(runit); } } // a short simple main public static void main (String [] args) { new TestMyClock(); } }