Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So I need to write an applet that simulates a stopwatch. It should have a Start button and a Stop button. When the Start button

So I need to 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. I also have an html file that I need to use to adjust the size of the GUI timer. This is what I have so far, but Im having difficulty compliling the code, any help is appreciated.

StopWatch Java:

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

public class StopWatch extends JApplet {

private final int TIME_DELAY = 1000; private int numSceonds = 0; private JTextField seconds; private JButton startButton; private JButton stopButton; private JPanel secondsPanel; private JPanel buttonPanel; private Timer timer;

public void init() {

buildButtonPanel(); buildSecondsPanel(); add(buttonPanel, BorderLayout.SOUTH); add(secondsPanel, BorderLayout.CENTER);

timer = new Timer(TIME_DELAY, new TimerListener()); }

private void buildButtonPanel() {

buttonPanel = new JPanel();

startButton = new JButton("Start"); stopButton = new JButton("Stop");

startButton.addActionListener(new StartButtonListener()); stopButton.addActionListener(new StopButtonListener());

buttonPanel.add(startButton); buttonPanel.add(stopButton); }

private void buildSecondsPanel() {

secondsPanel = new JPanel();

seconds = new JTextField(10); seconds.setEditable(false); seconds.setFont(new Font("SansSerif", Font.BOLD, 18)); seconds.setText("0");

secondsPanel.add(seconds);

}

private class StartButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) { timer.start(); }

}

private class StopButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { timer.stop(); }

}

private class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { numSeconds++; seconds.setText(String.valueOf(numSceonds)); }

}

}

StopWatch html:

**//

Stop Watch

Use the buttons to start and stop the timer

**//

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

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions