Stopwatch code in JAVA

Stop watch code in JAVA

import java.awt.*;

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

public class StopWatch extends JPanel
{
private Timer myTimer1;
public static final int ONE_SEC = 1000; //time step in milliseconds
public static final int TENTH_SEC = 100;

private Font myClockFont;

private JButton startBtn, stopBtn, resetBtn;
private JLabel timeLbl;
private JPanel topPanel, bottomPanel;

private int clockTick; //number of clock ticks; tick can be 1.0 s or 0.1 s
private double clockTime; //time in seconds
private String clockTimeString;

public StopWatch()
{
clockTick = 0; //initial clock setting in clock ticks
clockTime = ((double)clockTick)/10.0;

clockTimeString = new Double(clockTime).toString();
myClockFont = new Font(“Serif”, Font.PLAIN, 50);

timeLbl = new JLabel();
timeLbl.setFont(myClockFont);
timeLbl.setText(clockTimeString);

startBtn = new JButton(“Start”);
stopBtn = new JButton(“Stop”);
resetBtn = new JButton(“Reset”);

myTimer1 = new Timer(TENTH_SEC, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
clockTick++;
clockTime = ((double)clockTick)/10.0;
clockTimeString = new Double(clockTime).toString();
timeLbl.setText(clockTimeString);
//System.out.println(clockTime);
}
});

startBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
myTimer1.start();
}
});

stopBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
myTimer1.stop();
}
});

resetBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
clockTick = 0;
clockTime = ((double)clockTick)/10.0;
clockTimeString = new Double(clockTime).toString();
timeLbl.setText(clockTimeString);
}
});

}//end of StopWatch constructor

public void launchStopWatch()
{
topPanel = new JPanel();
topPanel.setBackground(Color.red);
bottomPanel = new JPanel();
bottomPanel.setBackground(Color.orange);
topPanel.add(timeLbl);
bottomPanel.add(startBtn);
bottomPanel.add(stopBtn);
bottomPanel.add(resetBtn);

this.setLayout(new BorderLayout());

add(topPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);

setSize(300,200);
setBackground(Color.orange);

}//end of launchClock

public static void main(String[] args)
{
MyTestFrame myTestFrame1 = new MyTestFrame();
}

}//end of public class

//Testing Code

class MyTestFrame extends JFrame
{
StopWatch StopWatch1;

public MyTestFrame()
{
super(“My Stop Watch”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container myPane = getContentPane();

StopWatch1 = new StopWatch();
StopWatch1.launchStopWatch();
myPane.add(StopWatch1);
pack();
setVisible(true);
}

}

 

DOWNLOAD NOW

 

FreeStudentProjects

A collection of source codes that I wrote in VB 6.0, ASP.NET, PHP, C#.NET, VB.NET and JAVA in a course of my career as web developer and software engineer that I would like to share to my fellow programmers. Some of the codes here is not my original work that I found over the Internet and Books while I'm learning how to program. I hope you find my work useful in your learning in programming. Please share my work to other people also who interested to learn the basics. Thank you very much and Happy Productive Programming Everyone. for more info please WhatsApp us on +91-9481545735 or Email id: freestudentprojectsindia@gmail.com

1 Comment

  • Martin Tomar

    July 9, 2013 - 10:33 PM

    it is ok men.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.