Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write an applet that simulates a stopwatch. It should have a Start button and a Stop button. When the Start button is clicked the applet

Write an applet that simulates a stopwatch. It should have a Start button and a Stop button. When the Start button is clicked the applet should count the seconds that pass. When the Stop button is clicked, the applet should stop counting seconds.

This is what I have and it works fine, BUT my teacher said we need to upload the Stopwatch.java file AND the Stopwatch.html file...I'm not sure how to make the html file? Can someone help?

import java.awt.event.*; import java.awt.*; import javax.swing.*;

public class Stopwatch extends JFrame implements ActionListener, Runnable { private long startTime; private final static java.text.SimpleDateFormat timerFormat = new java.text.SimpleDateFormat("mm : ss.SSS"); private final JButton startStopButton= new JButton("Start/stop"); private Thread updater; private boolean isRunning= false; private final Runnable displayUpdater= new Runnable() { public void run() { displayElapsedTime(System.currentTimeMillis() - Stopwatch.this.startTime); } }; public void actionPerformed(ActionEvent ae) { if(isRunning) { long elapsed= System.currentTimeMillis() - startTime; isRunning= false; try { updater.join(); // Wait for updater to finish } catch(InterruptedException ie) {} displayElapsedTime(elapsed); // Display the end-result } else { startTime= System.currentTimeMillis(); isRunning= true; updater= new Thread(this); updater.start(); } } private void displayElapsedTime(long elapsedTime) { startStopButton.setText(timerFormat.format(new java.util.Date(elapsedTime))); } public void run() { try { while(isRunning) { SwingUtilities.invokeAndWait(displayUpdater); Thread.sleep(50); } } catch(java.lang.reflect.InvocationTargetException ite) { ite.printStackTrace(System.err); // Should never happen! } catch(InterruptedException ie) {} // Ignore and return! } public Stopwatch() { startStopButton.addActionListener(this); getContentPane().add(startStopButton); setSize(100,50); setVisible(true); } public static void main(String[] arg) { new Stopwatch().addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions

Question

Describe Balor method and give the chemical reaction.

Answered: 1 week ago

Question

How to prepare washing soda from common salt?

Answered: 1 week ago

Question

Explain strong and weak atoms with examples.

Answered: 1 week ago

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago