Java Program To Create A Digital Watch Using Swing | Java Project

by | May 28, 2022 | Coding, Java

Home » Coding » Java Program To Create A Digital Watch Using Swing | Java Project

Introduction of the Project

In this Java tutorial, we will learn about how to create a digital watch using Java. To make the project more interesting, we will be implementing some GUI along with the fundamental calculator logic to give users a good interface to work with.

 

Objectives

The program can essentially be divided into objectives that we want to achieve. The first one is to create a user interface involving some graphics and the second objective is to implement the digital clock such that it shows the correct time always.

Requirements

The major requirement in coding this program is Swing because it is used to create the Graphical User Interface (GUI) of the program. The Swing framework of Java has several components that give developers the freedom and flexibility to make eye-catching interfaces for windows-based applications. The best part about using Swing to create components like tables, scroll panes, buttons, and the like is that the framework is platform-independent.

Source Code

// Java program to create a simple calculator
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.util.*;
public class DigitalWatch implements Runnable{
JFrame f;
Thread t=null;
int hours=0, minutes=0, seconds=0;
String timeString = "";
JButton b;


DigitalWatch(){
f=new JFrame();


t = new Thread(this);
t.start();


b=new JButton();
b.setBounds(100,100,100,50);


f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
}


public void run() {
try {
while (true) {


Calendar cal = Calendar.getInstance();
hours = cal.get( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = cal.get( Calendar.MINUTE );
seconds = cal.get( Calendar.SECOND );


SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
Date date = cal.getTime();
timeString = formatter.format( date );


printTime();


t.sleep( 1000 ); // interval given in milliseconds
}
}
catch (Exception e) { }
}


public void printTime(){
b.setText(timeString);
}


public static void main(String[] args) {
new DigitalWatch();



}
}

Explanation of the Code

The GUI of this digital watch program is the least interactive as it will just have one button that will display the current time. Therefore, creating the button and setting it inside the JFrame is all that is needed. To find the current time, the following steps are performed:

1. To get the current time, the Calendar class of Java is used. It is an abstract class containing methods that will return the time, date, year, and month.

2. Once an object of the Calendar class is created, the current time is retrieved using the getDate() method. This clock is designed to be in the 12-hour format.

3. Finally, what is left is formatting the time int hrs:mm:ss. For this, an object of the SimpleDataFormat class is instantiated. The date obtained in the step above is then formatted according to the layout of hrs:mm:ss.

4. The printTime() function simply sets the text of the button to the current time every time it is called. It is called every second, besides which the thread sleeps.

5. The whole body of the code is placed in an Exception Handler in the unforeseen event of a failure.

Output

Java Program to Create a Digital Watch using Swing | Java Project

Conclusion

This tutorial was a simple digital watch interface using Java and Swing framework. Hope you found this tutorial useful in creating a digital watch in Java.

 

You May Also Like To Create…

0 Comments

Submit a Comment

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