Question

Nork individually or in pairs to write a simple timer dialog in Java. The dialog shall consist of three GUI components: a t

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//JAVA COSDE TO COPY//


// importing required packages
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
  
public class Timer extends Applet implements Runnable, ActionListener {
  
// Panel to keep all the buttons and labels
Panel p;
Label display;
  
// Button
Button start, stop, reset;
  
// Time
int hour, minute, second, millisecond;
  
// string to be displayed on the label
String disp;
  
// State of stopwatch on/off
boolean on;
  
// initialization
public void init()
{
// initially off
on = false;
  
p = new Panel();
// Setting layout of the panel
p.setLayout(new GridLayout(4, 1, 6, 10));
  
// initial time 00 : 00 : 00 : 000
hour = minute = second = millisecond = 0;
  
// Label
display = new Label();
disp = "00 : 00 : 00 : 000";
display.setText(disp);
p.add(display);
  
// Start button
start = new Button("Start");
start.addActionListener((ActionListener) this);
p.add(start);
  
// Reset button
reset = new Button("Reset");
reset.addActionListener((ActionListener) this);
p.add(reset);
  
// Stop button
stop = new Button("Stop");
stop.addActionListener((ActionListener) this);
p.add(stop);
  
add(p);
  
// starting thread
new Thread(this, "StopWatch").start();
}
  
// Reset Function
// reset to default value
public void reset()
{
try {
Thread.sleep(1);
}
catch (Exception e) {
System.out.println(e);
}
hour = minute = second = millisecond = 0;
}
  
// update function
// update the timer
public void update()
{
millisecond++;
if (millisecond == 1000) {
millisecond = 0;
second++;
if (second == 60) {
second = 0;
minute++;
if (minute == 60) {
minute = 0;
hour++;
}
}
}
}
  
// changing label
public void changeLabel()
{
  
// Properly formatting the display of the timer
if (hour < 10)
disp = "0" + hour + " : ";
else
disp = hour + " : ";
  
if (minute < 10)
disp += "0" + minute + " : ";
else
disp += minute + " : ";
  
if (second < 10)
disp += "0" + second + " : ";
else
disp += second + " : ";
  
if (millisecond < 10)
disp += "00" + millisecond;
else if (millisecond < 100)
disp += "0" + millisecond;
else
disp += millisecond;
  
display.setText(disp);
}
  
// thread.run function
public void run()
{
  
// while the stopwatch is on
while (on) {
try {
// pause 1 millisecond
Thread.sleep(1);
// update the time
update();
// changeLabel
changeLabel();
}
catch (InterruptedException e) {
System.out.println(e);
}
}
}
  
// actionPerformed
// To listen to the actions on the buttons
public void actionPerformed(ActionEvent e)
{
  
// start a thread when start button is clicked
if (e.getSource() == start) {
// stopwatch is on
on = true;
new Thread(this, "StopWatch").start();
}
  
// reset
if (e.getSource() == reset) {
// stopwatch off
on = false;
reset();
changeLabel();
}
  
if (e.getSource() == stop) {
// stopwatch off
on = false;
}
}
}

//PROGRAM OUTPUT//

X Applet Viewer: test/Timer.class Applet 00:00:00:000 L Start Reset Stop Applet started

Applet Viewer: test/Timer.class Applet 00:00:01: 185 Start Reset Stop Applet started.

//EDITOR OUTPUT//

x х Test - NetBeans IDE 8.2 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Help <default config>

//COMMENT DOWN FOR ANY QUERIES...
//HIT A THUMBS UP IF YOU DO LIKE IT!!!

Add a comment
Know the answer?
Add Answer to:
Nork individually or in pairs to write a simple "timer" dialog in Java. The dialog shall...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT