One of the fascinating invention by man that amaze me is the clock or watch this object help us knows what is the exact time. It helps us in many ways in our day to day lives for instances what time we go to work and school, what time we to go sleep, what time we talk our medicine so that we will healed for our sickness. As a programmer there are many things that I have learned not only to solve programming problems but also mimic the real things that I can find in my surroundings.
In this article I will show you how create your own Digital Clock using Java as my programming language. I am using Swing Framework to create this program. There are many Java libraries that I used to create the graphical user interface of our Digital Clock I am very glad that Java is enriched with many build in library to do the graphical interface much easier that cannot be found in some programming language just like in C or C++.
Here is the library that I used to create our graphical user interface, time and calendar functions in Java import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.util.Calendar and import java.util.* all this library file is a part of Java programming language we must able to study each one of them to know more the different library functions which enhance and add functionality in our program.
I hope you find my sample program Digital Clock in Java useful in your learning in Java programming language. One of the things that I learned in Java know first what kind of problem you want to solve and select the appropriate library and frameworks that suited in your needs why because Java is a very big and complex language you may be lost if you are not aware of it.
Sample Output Of Our Program
The TextPad text editor that I used in creating our program
Program Listing
// clock.java
// Program code and Design By: Mr. Jake Rodriguez Pomperada,MAED-IT
// Tool : Java, TextPad Text Editor
// Date : May 31, 2014
// A Simple Digital Clock in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Calendar;
import java.util.*;
public class clock {
public static void main(String[] args) {
JFrame clock = new TextClockWindow();
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}
}
class TextClockWindow extends JFrame {
private JTextField timeField;
public TextClockWindow() {
timeField = new JTextField(10);
timeField.setFont(new Font("sansserif", Font.PLAIN, 48));
Container content = this.getContentPane();
content.setLayout(new FlowLayout());
content.add(timeField);
this.setTitle("DIGITAL CLOCK By: Jake R. Pomperada, MAED-IT");
this.pack();
javax.swing.Timer t = new javax.swing.Timer(1000,
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calendar calendar = new GregorianCalendar();
String am_pm;
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
if( calendar.get( Calendar.AM_PM ) == 0 ){
am_pm = "AM";
}
else{
am_pm = "PM";
} // Code to Determine whether the time is AM or PM
timeField.setText("" + h + ":" + m + ":" + s + " " + am_pm);
timeField.setHorizontalAlignment(JTextField.CENTER);
// Center the text
timeField.getCaret().setVisible(false);
// Hide the Cursor in JTextField
}
});
t.start();
}
} // End of Code
thank you so much sir, for sharing...
ReplyDeleteAm kupa,a beginner in java.please can u help me with this project given to me by my instructor...i really dnt know how to go abt it.
ReplyDelete"A Banking Management System that will display in console".