GK SCIENTIST

General Knowledge One Stop Source
Menu
  • Home
  • Social Science
    • History
    • Political Science
    • Geography
  • Science
    • Physics
    • Chemistry
    • Biology
  • Chemistry Notes
  • Mathematics
  • Computer
  • Tutorial MySQL
  • Tutorial Python
  • Java Tutorial
  • English Grammar
  • English Essay
  • Indian Anthropology
  • Philosophy
  • Solved Paper
  • UPSC
  • Current Content
    • Current Affairs
    • RSTV News
    • Yojana and Kurukshetra Gist
  • Donate
  • Contact Us

If you are interested in advertising to our audience, submit the advertising enquiry form.

Advertising Enquiry
Computer

Java StopWatch

Gk Scientist January 18, 2023 No Comments
Tweet WhatsApp Telegram

Java StopWatch:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

In Java, we can create a StopWatch which contains three push buttons and one label. The push buttons are called Start, Stop and Reset and are used to start, stop and reset the StopWatch. The label displays the elapsed time. Although quite simple, this project shows the ease with which GUI interfaces can be created using Swing.

Sample Code for Java StopWatch:

java stopwatch code1 - Java StopWatch
java stopwatch code2 - Java StopWatch
java stopwatch code3 - Java StopWatch
java stopwatch code4 - Java StopWatch

Output:

java stopwatch output - Java StopWatch
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Stopwatch implements ActionListener{

	JFrame frame = new JFrame();
	JButton start_btn = new JButton("START");
	JButton reset_btn = new JButton("RESET");
	JLabel timeLabel = new JLabel();
	int elapsedTime = 0;
	int seconds = 0;
	int minutes = 0;
	int hours = 0;
	boolean started = false;
	String seconds_str = String.format("%02d", seconds);
	String minutes_str = String.format("%02d", minutes);
	String hours_str = String.format("%02d", hours);
	
	Timer timer = new Timer(1000, new ActionListener() {
		
		public void actionPerformed(ActionEvent e) {
			
			elapsedTime = elapsedTime + 1000;
			hours = (elapsedTime/3600000);
			minutes = (elapsedTime/60000) % 60;
			seconds = (elapsedTime/1000) % 60;
			seconds_str = String.format("%02d", seconds);
			minutes_str = String.format("%02d", minutes);
			hours_str = String.format("%02d", hours);
			timeLabel.setText(hours_str + ":" + minutes_str + ":" + seconds_str);
			
		}
		
	});
	
	
	Stopwatch(){
		
		timeLabel.setText(hours_str + ":" + minutes_str + ":" + seconds_str);
		timeLabel.setBounds(100,100,200,100);
		timeLabel.setFont(new Font("Roboto",Font.PLAIN,35));
		timeLabel.setBorder(BorderFactory.createBevelBorder(1));
		timeLabel.setOpaque(true);
		timeLabel.setHorizontalAlignment(JTextField.CENTER);
		
		start_btn.setBounds(100,200,100,50);
		start_btn.setFont(new Font("Roboto",Font.PLAIN,20));
		start_btn.setFocusable(false);
		start_btn.addActionListener(this);
		
		reset_btn.setBounds(200,200,100,50);
		reset_btn.setFont(new Font("Roboto",Font.PLAIN,20));
		reset_btn.setFocusable(false);
		reset_btn.addActionListener(this);
		
		frame.add(start_btn);
		frame.add(reset_btn);
		frame.add(timeLabel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(450,400);
		frame.setLayout(null);
		frame.setVisible(true);
		
		}
		
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if (e.getSource()==start_btn) {
			
			start();
			if(started==false) {
				started=true;
				start_btn.setText("STOP");
				start();
			} else {
				
				started=false;
				start_btn.setText("START");
				stop();
				
			}
			
		}
		
		if(e.getSource()==reset_btn) {
			
			started=false;
			start_btn.setText("START");
			reset();
		}
		
	}
	
	void start() {
		
		timer.start();
	}
	
    void stop() {
		timer.stop();
		
	}
    
    void reset() {
    	
    	timer.stop();
    	elapsedTime = 0;
    	seconds = 0;
    	minutes = 0;
    	hours = 0;
    	seconds_str = String.format("%02d", seconds);
    	minutes_str = String.format("%02d", minutes);
    	hours_str = String.format("%02d", hours);
    	timeLabel.setText(hours_str + ":" + minutes_str + ":" + seconds_str);
    	
    }
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	Stopwatch stopwatch = new Stopwatch();
	
	
	}

}

Tutorial MySQL
Tutorial Python
Object-Oriented Programming (OOP)
Structured Programming- Advantages and Disadvantages
Continue Statement in C Language
Spooling and Virtual Devices
Information Security in E-Commerce
Client-Server Architecture
Java (programming language)– Wikipedia
Prev Article
Next Article

Related Articles

Create Java MenuBar and Menus
Create Java MenuBar: We can use the Eclipse IDE for …
Gk Scientist January 17, 2023 Computer

Create Java MenuBar and Menus

List Iteration in Python
List Iteration in Python: We can use the PyCharm code …
Gk Scientist November 27, 2022 Computer

List Iteration in Python

How to Create List in Python
Create List in Python: We can use the PyCharm code …
Gk Scientist November 27, 2022 Computer

How to Create List in Python

List Comprehension in Python
List Comprehension in Python: We can use the PyCharm code …
Gk Scientist November 27, 2022 Computer

List Comprehension in Python

Applications of Computer
Applications of Computer: In the 1950s, computers were giant, special-purpose …
Gk Scientist August 7, 2022 Computer

Applications of Computer

Python MySQL Between Operator
Python MySQL Between Operator: (1) MySQL Between Operator is a …
Gk Scientist December 16, 2022 Computer

Python MySQL Between Operator

Java GUI Calculator Project
Java GUI Calculator Project: We can use the PyCharm code …
Gk Scientist January 20, 2023 Computer

Java GUI Calculator Project

Python Bitwise Operators
Python Bitwise Operators: We can use the PyCharm code editor …
Gk Scientist November 22, 2022 Computer

Python Bitwise Operators

Leave a Reply Cancel Reply

Search




  • Popular
  • Recent




GK SCIENTIST

General Knowledge One Stop Source

Information

  • About Us
  • Terms and Condition, Disclaimer
  • Contact Us

Android Apps

  • IAS App For English Medium Students
  • IAS Basics App For English Medium Students
  • IAS Hindi App For Hindi Medium Students
DMCA.com Protection Status

Popular Tags

Biology (33) Biology Questions (88) Chemistry (57) Computer (213) Current Affairs (4) Current Content (0) Economy (10) English Essay (172) English Grammar (75) English Literature (10) Geography (83) History (259) Indian Anthropology (11) Indian Polity (14) JKAS Mains Question Papers (17) Mathematics (68) Moral Science (7) NCERT & Other Boards Books (25) Philosophy (114) Physics (89) Political Science (132) RS TV News (33) Science (553) Social Anthropology (7) Social Science (17) Solved Paper (47) UPSC (7) UPSC Mains Question Papers (26)

Downloads

  • NCERT Books
  • Old NCERT Books
  • NIOS Books For IAS, SSC, and State PSC Exam
  • Tamil Nadu Board Books: Important For UPSC, SSC, and State PSC Exam
  • Modern Indian and World History Notes For IAS Exam
  • UPSC Topper 2013 Gaurav Agrawal Notes For IAS Preparation
  • UPSC IAS Prelims General Studies – Previous Year Papers
  • UPSC Mains Question Papers

Copyright © 2023 GK SCIENTIST
Theme by MyThemeShop.com& Hosted On Cloudways

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
 

Loading Comments...