{"id":103328,"date":"2023-01-18T18:08:43","date_gmt":"2023-01-18T18:08:43","guid":{"rendered":"https:\/\/gkscientist.com\/?p=103328"},"modified":"2023-01-18T18:13:09","modified_gmt":"2023-01-18T18:13:09","slug":"java-stopwatch","status":"publish","type":"post","link":"https:\/\/gkscientist.com\/java-stopwatch\/","title":{"rendered":"Java StopWatch"},"content":{"rendered":"\n

Java StopWatch:<\/h2>\n\n\n\n
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<\/a><\/em><\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

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.<\/p>\n\n\n\n

Sample Code for Java StopWatch:<\/em><\/strong><\/p>\n\n\n\n

\"java<\/figure>\n\n\n\n
\"java<\/figure>\n\n\n\n
\"java<\/figure>\n\n\n\n
\"java<\/figure>\n\n\n\n

Output:<\/em><\/strong><\/p>\n\n\n\n

\"java<\/figure>\n\n\n\n
import java.awt.event.*;\nimport java.awt.*;\nimport javax.swing.*;\n\npublic class Stopwatch implements ActionListener{\n\n\tJFrame frame = new JFrame();\n\tJButton start_btn = new JButton(\"START\");\n\tJButton reset_btn = new JButton(\"RESET\");\n\tJLabel timeLabel = new JLabel();\n\tint elapsedTime = 0;\n\tint seconds = 0;\n\tint minutes = 0;\n\tint hours = 0;\n\tboolean started = false;\n\tString seconds_str = String.format(\"%02d\", seconds);\n\tString minutes_str = String.format(\"%02d\", minutes);\n\tString hours_str = String.format(\"%02d\", hours);\n\t\n\tTimer timer = new Timer(1000, new ActionListener() {\n\t\t\n\t\tpublic void actionPerformed(ActionEvent e) {\n\t\t\t\n\t\t\telapsedTime = elapsedTime + 1000;\n\t\t\thours = (elapsedTime\/3600000);\n\t\t\tminutes = (elapsedTime\/60000) % 60;\n\t\t\tseconds = (elapsedTime\/1000) % 60;\n\t\t\tseconds_str = String.format(\"%02d\", seconds);\n\t\t\tminutes_str = String.format(\"%02d\", minutes);\n\t\t\thours_str = String.format(\"%02d\", hours);\n\t\t\ttimeLabel.setText(hours_str + \":\" + minutes_str + \":\" + seconds_str);\n\t\t\t\n\t\t}\n\t\t\n\t});\n\t\n\t\n\tStopwatch(){\n\t\t\n\t\ttimeLabel.setText(hours_str + \":\" + minutes_str + \":\" + seconds_str);\n\t\ttimeLabel.setBounds(100,100,200,100);\n\t\ttimeLabel.setFont(new Font(\"Roboto\",Font.PLAIN,35));\n\t\ttimeLabel.setBorder(BorderFactory.createBevelBorder(1));\n\t\ttimeLabel.setOpaque(true);\n\t\ttimeLabel.setHorizontalAlignment(JTextField.CENTER);\n\t\t\n\t\tstart_btn.setBounds(100,200,100,50);\n\t\tstart_btn.setFont(new Font(\"Roboto\",Font.PLAIN,20));\n\t\tstart_btn.setFocusable(false);\n\t\tstart_btn.addActionListener(this);\n\t\t\n\t\treset_btn.setBounds(200,200,100,50);\n\t\treset_btn.setFont(new Font(\"Roboto\",Font.PLAIN,20));\n\t\treset_btn.setFocusable(false);\n\t\treset_btn.addActionListener(this);\n\t\t\n\t\tframe.add(start_btn);\n\t\tframe.add(reset_btn);\n\t\tframe.add(timeLabel);\n\t\tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n\t\tframe.setSize(450,400);\n\t\tframe.setLayout(null);\n\t\tframe.setVisible(true);\n\t\t\n\t\t}\n\t\t\n\t@Override\n\tpublic void actionPerformed(ActionEvent e) {\n\t\t\/\/ TODO Auto-generated method stub\n\t\t\n\t\tif (e.getSource()==start_btn) {\n\t\t\t\n\t\t\tstart();\n\t\t\tif(started==false) {\n\t\t\t\tstarted=true;\n\t\t\t\tstart_btn.setText(\"STOP\");\n\t\t\t\tstart();\n\t\t\t} else {\n\t\t\t\t\n\t\t\t\tstarted=false;\n\t\t\t\tstart_btn.setText(\"START\");\n\t\t\t\tstop();\n\t\t\t\t\n\t\t\t}\n\t\t\t\n\t\t}\n\t\t\n\t\tif(e.getSource()==reset_btn) {\n\t\t\t\n\t\t\tstarted=false;\n\t\t\tstart_btn.setText(\"START\");\n\t\t\treset();\n\t\t}\n\t\t\n\t}\n\t\n\tvoid start() {\n\t\t\n\t\ttimer.start();\n\t}\n\t\n    void stop() {\n\t\ttimer.stop();\n\t\t\n\t}\n    \n    void reset() {\n    \t\n    \ttimer.stop();\n    \telapsedTime = 0;\n    \tseconds = 0;\n    \tminutes = 0;\n    \thours = 0;\n    \tseconds_str = String.format(\"%02d\", seconds);\n    \tminutes_str = String.format(\"%02d\", minutes);\n    \thours_str = String.format(\"%02d\", hours);\n    \ttimeLabel.setText(hours_str + \":\" + minutes_str + \":\" + seconds_str);\n    \t\n    }\n\t\n\t\n\tpublic static void main(String[] args) {\n\t\t\/\/ TODO Auto-generated method stub\n\n\tStopwatch stopwatch = new Stopwatch();\n\t\n\t\n\t}\n\n}<\/code><\/pre>\n\n\n\n
\n\n\n\n
Tutorial MySQL<\/a>
Tutorial Python<\/a>
Object-Oriented Programming (OOP)<\/a>
Structured Programming- Advantages and Disadvantages<\/a>
Continue Statement in C Language<\/a>
Spooling and Virtual Devices<\/a>
Information Security in E-Commerce<\/a>
Client-Server Architecture<\/a>
Java (programming language)<\/a>\u2013 Wikipedia<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"

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: Output: Tutorial MySQLTutorial PythonObject-Oriented Programming (OOP)Structured <\/p>\n","protected":false},"author":1,"featured_media":103333,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6985],"tags":[8115,8112,8111,8114,8116],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/gkscientist.com\/wp-content\/uploads\/2023\/01\/Java-StopWatch.jpg?fit=500%2C250&ssl=1","_links":{"self":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/posts\/103328"}],"collection":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/comments?post=103328"}],"version-history":[{"count":0,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/posts\/103328\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/media\/103333"}],"wp:attachment":[{"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/media?parent=103328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/categories?post=103328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gkscientist.com\/wp-json\/wp\/v2\/tags?post=103328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}