Tuesday, February 6, 2018

Show watch in Label JAVA

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Watch implements Runnable {

JFrame fr;
JPanel pa;
String time;
JLabel label;

public void UI() {

fr = new JFrame("Ding!");
label = new JLabel("Time");
pa = new JPanel();
pa.add(label);
fr.getContentPane().add(pa);
fr.setSize(200, 200);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

}
public void getTime() {

Date date = new Date();
SimpleDateFormat f = new SimpleDateFormat("hh:mm:ss");
time = f.format(date.getTime());

}

@Override
public void run() {

try {
while (true) {
getTime();
setTime();
Thread.sleep(1000);
}
} catch (Exception e) {}
}

public void setTime() {

label.setText(time);
}
public static void main(String[] args) {
new Watch().UI();
}
}

No comments:

Post a Comment