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
  • JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. T...

    JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. The timer should show "count: the beginning and increase the number by 1 in every one second, and so on. o" at .3 A Timer - × A Timer Count: 0 Count: 1 (B) After 1 second, the GUI Window (A) Initial GUI Window According to the...

  • I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioBut...

    I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioButton actionListeners? and mycomboBox addItemListener? There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...

  • ******Java Programming Hi guys, I really need you help. I created a code for my java...

    ******Java Programming Hi guys, I really need you help. I created a code for my java course, but it keep giving me error messages. Majority of my code is fine but some keep display error on my console. I was hoping someone could pin points the problem. .There are three classes with the testCenter class being the main class. In the following is the assignment, and the bottom is my code. Please help! Assignment: Concepts: GUI User Design Graphics Deployment...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Java please answer A and B a.         Describe the purpose of action listener in push button...

    Java please answer A and B a.         Describe the purpose of action listener in push button jbtnCompare.(from the code below) b.         Explain why there are not action listeners added to text fields of this program.(from the code below) code import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; class SwingFC implements ActionListener { JTextField jtfFirst; // holds the first file name JTextField jtfSecond; // holds the second file name JButton jbtnComp; // button to compare the files JLabel jlabFirst, jlabSecond; //...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate...

    /** * File: GradeCalculator . java * Description: Instances of this class are used to calculate * a course average and a letter grade. In order to calculate * the average and the letter grade, a GradeCalculator must store * two essential pieces of data: the number of grades and the sum * of the grades. Therefore these are declared as object properties * (instance variables). * Each time calcAverage (grade) is called, a new grade is added to *...

  • Need help writing Java code for this question. CircleFrame class is provided below. what happen after...

    Need help writing Java code for this question. CircleFrame class is provided below. what happen after u add the code and follow the requirement? It would be nice to be able to directly tell the model to go into wait mode in the same way that the model's notify method is called. (i.e. notify is called directly on the model, whereas wait is called indirectly through the suspended attribute.) Try altering the the actionPerformed method of the SliderListener innner class...

  • I have this problem for a final thats in my book thats i'm stuck on ....

    I have this problem for a final thats in my book thats i'm stuck on . Any help would be really appreciated. Design and code a Swing GUI to translate text that is input in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: ⦁ For words that begin with consonants, move the leading consonant to the end of the word and add “ay.” For example, “ball” becomes...

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

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