Java Program to implement System Clock

Write a Java Program to implement System Clock:-

public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import java.util.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements ItemListener, ActionListener
{
Choice c1;
Button b1;
Checkbox cb1, cb2, cb3, cb4;
CheckboxGroup cbg1, cbg2;
Label lblTime;
TimeThread tt;
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(400,300);
addControls();
setVisible(true);
tt = new TimeThread(this);
}
private void addControls()
{
setLayout(new FlowLayout());
lblTime = new Label("System Time Here");
add(lblTime);
c1 = new Choice();
c1.add("Ajmer");
c1.add("Jaipur");
c1.add("Alwar");
c1.add("Nasirabad");
c1.add("Bikaner");
c1.add("Kishangarh");
c1.add("Beawar");
c1.add("Bundi");
c1.add("Kota");
c1.add("Nagur");
c1.add("Jodhpur");
c1.add("Pali");
c1.addItemListener(this);
add(c1);
b1 = new Button("Click Me");
b1.addActionListener(this);
add(b1);
cbg1 = new CheckboxGroup();
cbg2 = new CheckboxGroup();
cb1 = new Checkbox("DOS",true,cbg1);
cb2 = new Checkbox("Windows",cbg1,true);
cb3 = new Checkbox("Linux",cbg2,false);
cb4 = new Checkbox("Unix",cbg2,false);
add(cb1);
add(cb2);
add(cb3);
add(cb4);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
cb4.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
if (ie.getSource() == c1)
{
if (c1.getSelectedIndex() != -1)
{
System.out.println(c1.getSelectedItem());
System.out.println(c1.getSelectedIndex());
}
}
else if (ie.getSource() == cb1)
{
c1.setEnabled(cb1.getState());
}
else if (ie.getSource() == cb3 || ie.getSource() == cb4)
{
System.out.println("Item Selected : " +
cbg2.getSelectedCheckbox().getLabel());
}
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == b1)
{
int i;
for (i=0;i<c1.getItemCount();i++)
System.out.println(c1.getItem(i));
}
}
String getTime()
{
Calendar cal = new GregorianCalendar();
int hour;
int minute;
int second;
hour = cal.get(Calendar.HOUR);
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
String timeStr = hour + ":" + minute + ":" + second;
return timeStr;
}
}
class TimeThread extends Thread
{
MyFrame mf;
TimeThread(MyFrame mf)
{
this.mf = mf;
start();
}
public void run()
{
while(true)
{
mf.lblTime.setText(mf.getTime());
try
{
Thread.sleep(970);
}
catch (InterruptedException e1)
{
}
}
}
}
import java.awt.event.*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

Related Posts