Skip to main content

Stopwatch 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);
}
}

Comments

Popular posts from this blog

Cashew Industry Management System

This Cashew Industry Management System is developed for managing the industry’s daily production details, stock maintenance, salary details and employee details, managed by office workers and administrator. This software system allows administrator to control the entire system including database maintenance for adding a new employee, employee wage payment, supplier details. As well as this software keeps track of work done by each employee in each section & inventory maintenance. LANGUAGES TO BE USED: Front end:    VB.NET Back end:     SQL server SOFTWARE REQUIREMENTS: Windows XP or above. Visual Studio 2010. SQL server 2008. STRUCTURE OF THE PROGRAM: The project contains the following modules: Admin module : This module includes the login module and sub modules. Supplier modules:  To manage suppliers, such as add new supplier or remove an existing one. Employee module:  To manage employees, such as...

Analog Clock using VB.NET

Here we created Analog Clock using VB.NET coding. This is the mini student project and you can learn some coding techniques in this project. this analog clock works in Visual studio 2008 version or Visual Studio 2012 version. No database required to created this analog clock. This mini project created in single form , 12 labels , 3 shapes, and 2 timers used to develop this project code. Analog Clock using VB.NET Programming Language:  VB.NET (This mini student project is for diploma, BCA and ITI students. This download link has source code of this project and it has exe file of this project. Kindly install winrar to extract this project code.) Share this:

PHP Guestbook Script

PHP Guestbook Script is developed using PHP and MySQL database.  Guestbook script allows you to put a  guestbook  option on your website which is helpful to visitors of the website. Using this script website visitor can send comments and feedback on your site. All the records of the guest book stores in MySQL database. The admin is the master user and he has the right to edit and delete the records. Features of PHP Guestbook Script: This script allows you to put a guestbook script on your website. This file is is free software and you can redistribute or modify according to your requirements. It is fully Customizable look, Easy Navigation and usefull for blog site. This is the professional PHP and MySQL based object oriented programming(OOP) fully customizable guestbook. Using this visitors can leave message on your website. The script allows the visitors to enter name, E mail, Title and comment. Admin page to view all recor...