| CODE |
| <center> <center><b>Clock created in Java and copyright to Scott Clarke</b></center> <br> </br> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // add a zero in front of numbers<10 m=checkTime(m) s=checkTime(s) document.getElementById('txt').innerHTML=h+":"+m+":"+s t=setTimeout('startTime()',500) } function checkTime(i) { if (i<10) {i="0" + i} return i } </script> <b> <center>The time is: <body onload="startTime()"> <div id="txt"></div> </b> <br> </br> </center> // do not remove copyright // clock runs on your system clock so updates are automatic |
| CODE |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.Calendar; public class TextClock { public static void main(String[] args) { JFrame clock = new TextClockWindow(); clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); clock.setVisible(true); }//end main }//endclass TextClock class TextClockWindow extends JFrame { private JTextField timeField; // set by timer listener public TextClockWindow() { // Build the GUI - only one panel timeField = new JTextField(6); timeField.setFont(new Font("sansserif", Font.PLAIN, 48)); Container content = this.getContentPane(); content.setLayout(new FlowLayout()); content.add(timeField); this.setTitle("Text Clock"); this.pack(); // Create a 1-second timer and action listener for it. // Specify package because there are two Timer classes javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { Calendar now = Calendar.getInstance(); int h = now.get(Calendar.HOUR_OF_DAY); int m = now.get(Calendar.MINUTE); int s = now.get(Calendar.SECOND); timeField.setText("" + h + ":" + m + ":" + s); } }); t.start(); // Start the timer } } |