MyDigitalClock.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class MyDigitalClock extends JFrame implements Runnable
{
Date d=null;
JLabel clock=new JLabel();
Thread t=null;
String hh,mm,ss;
Font f=new Font("Maiandra GD", Font.BOLD, 20);
MyDigitalClock()
{
setLayout(null);
t=new Thread(this);
t.start();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
add(clock);
clock.setFont(f);
clock.setBackground(Color.yellow);
clock.setForeground(Color.PINK);
setVisible(true);
setSize(270,200);
setTitle("Digital Clock");
clock.setBounds(40,40,200,100);
setResizable(false);
}
public void run()
{
try
{
for(;;)
{
d=new Date();
int h=d.getHours();
int m=d.getMinutes();
int s=d.getSeconds();
if(h>12)
{
h=h-12;
//clock.setText(Integer.toString(h)+" : "+Integer.toString(m)+" : "+Integer.toString(s)+" PM");
if(h<10)
{
hh="0"+""+Integer.toString(h);
}
else
{
hh=Integer.toString(h);
}
if(m<10)
{
mm="0"+""+Integer.toString(m);
}
else
{
mm=Integer.toString(m);
}
if(s<10)
{
ss="0"+""+Integer.toString(s);
}
else
{
ss=Integer.toString(s);
}
clock.setText(hh+" : "+mm+" : "+ss+" PM");
}
else
{
if(h<10)
{
hh="0"+""+Integer.toString(h);
}
else
{
hh=Integer.toString(h);
}
if(m<10)
{
mm="0"+""+Integer.toString(m);
}
else
{
mm=Integer.toString(m);
}
if(s<10)
{
ss="0"+""+Integer.toString(s);
}
else
{
ss=Integer.toString(s);
}
clock.setText(hh+" : "+mm+" : "+ss+" AM");
}
Thread.sleep(1000);
}
}
catch(InterruptedException ie)
{
System.out.println(ie.getMessage());
}
}
public static void main(String args[])
{
new MyDigitalClock();
}
}
No comments:
Post a Comment