Java Programming-Digital clock

Source: Internet
Author: User

Clockdemo.java

/*
* To-Change the license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
Package newpackage;
Import java.awt.*;
Import java.util.*;
Import javax.swing.*;

Digital clock
public class Clockdemo extends JFrame implements runnable{
Thread clock;

Public Clockdemo () {
Super ("Digital Clock");//Call Parent class constructor
SetFont (New Font ("Times New Roman", font.bold,60));//Set the display font for the clock
Start (); Start process
SetSize (280,100); Set Window size
SetVisible (TRUE); Window Visual
Setdefaultcloseoperation (Jframe.exit_on_close); Exit the program when the window is closed
}

public void Start () {//START process
if (clock==null) {//If the process is a null value
Clock=new Thread (this); Instantiating a process
Clock.start (); Start process
}
}

public void Run () {//Run process
while (Clock!=null) {
Repaint (); Call the Paint method to redraw the interface
try{
Thread.Sleep (1000); Thread paused for one second (1000 milliseconds)
}
catch (Interruptedexception ex) {
Ex.printstacktrace (); Output error message
}
}
}

public void Stop () {//Stop process
Clock=null;
}

public void Paint (Graphics g) {//Reload component's Paint method
Graphics2D g2= (graphics2d) G; Get Graphics2D Object
Calendar now=new GregorianCalendar (); Instantiating a Calendar object
String timeinfo= ""; Output information
int Hour=now.get (calendar.hour_of_day); Get the number of hours
int Minute=now.get (calendar.minute); Get points
int Second=now.get (Calendar.second); Get the number of seconds

if (hour<=9)
timeinfo+= "0" +hour+ ":"; Formatted output
Else
timeinfo+=hour+ ":";
if (minute<=9)
timeinfo+= "0" +minute+ ":";
Else
Timeinfo+=minute+ ":";
if (second<=9)
timeinfo+= "0" +second;
Else
Timeinfo+=second;

G.setcolor (Color.White); Set the current color to white
Dimension dim=getsize (); Get window Size
G.fillrect (0,0,dim.width,dim.height); Fill background color to white
G.setcolor (Color.orange); Set the current color to orange
g.DrawString (timeinfo,20,80); Show Time string
}

public static void Main (string[] args) {
New Clockdemo ();
}
}

Java Programming-Digital clock

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.