You are on page 1of 1

import java.util.

*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
public class Bai10 extends JFrame implements ActionListener, Runnable {
JButton bt1 = new JButton ("new Clock");
JLabel clock = new JLabel();
Thread t;
public Bai10()
{
Container cont = this.getContentPane();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
clock = new JLabel(sdf.format(cal.getTime()),JLabel.CENTER);
clock.setFont(new Font(clock.getFont().getName(),Font.PLAIN,40));
clock.setForeground(Color.red);
cont.add(bt1,"North");
cont.add(clock);
this.pack();
this.setVisible(true);
bt1.addActionListener(this);
Thread t = new Thread(this);
t.start();
}
public void tick()
{
try
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
clock.setText(sdf.format(cal.getTime()));
Thread.sleep(1000);
}
catch(Exception e) {e.printStackTrace();}
}
public void run()
{
while (true)
{
tick();
}
}
public void actionPerformed(ActionEvent e)
{
Thread t = new Thread(new Bai10());
t.start();
}
public static void main(String[] args) {
new Bai10();
}
}

You might also like