-
Digital Clock in Java
I want to write a program that basically displays a digital clock. I dont want this to be an applet either, I have found tons of those on the web but its not what I am looking for. I just want some simple source code for a program that looks at my computers system clock and presents a digital display of the current time. Please post source code or any help you can offer.
-
In my opinion starting something then submitting it on the forum with get you a lot of help ;
but droping what YOU WANT would get you nowhere near the solution to you problem;
for the simple reason that ppl are volonteering here
best regards
-
Well I dont expect anyone to run out and write the program just for me. I was hoping for someone who has written a program like this already and could just offer the source. I will try to write it myself later if I cant get any help. Just trying to save myself sometime. I dont need to write the program to learn, I just need to use it.
-
Well i really didn't mean it to hurt you , i was trying to tell you what i think could get you quick help
I'm deeply sorry i
best regard
-
Pervert17,
Thanks for the apology. I know that you were just trying to help me and I snapped at you. I was wrong and I hope you didnt take my comments to heart. Now I am the one that should apologize. There is no excuse for my actions. I wish you the best in all your endeavors. May your heart be filled with happiness and your kitchen floors always be clean.
All the best
-
Well
now you got me red, i
i do appreciat your ability to re-think about issues ;
this atittude would make you the winner,
i now dicided to work on your case
just tell me weither you want it on applet or in Frame
so i could get to work
best regard
-
Here is a nice fat clock for you. The Clockpanel does all the work
IMPORTANT A: This bugger runs a thread and wont stop when you close the window, the stopClock method must be called or it will keeeeeeeep on. (check Krams answer on 'closing a window').
IMPORTANT B: Don't use this method for animations.
import javax.swing.*;
import java.awt.*;
class ClockPanel extends JPanel implements Runnable {
Font fnt=new Font("monospace",Font.PLAIN,25);
private boolean isTicking=false;
private String getTimeString() {
return new java.util.Date(System.currentTimeMillis()).toString();
}
public void startClock() {
Thread t=new Thread (this);
setTicking(true);
t.start();
}
private synchronized void setTicking(boolean isTicking) {
this.isTicking=isTicking;
}
private synchronized boolean getTicking() {
return isTicking;
}
public void stopClock() {
setTicking(false);
}
public void run() {
while (getTicking()) {
try {
Thread.currentThread().sleep(1000);
this.repaint();
} catch (InterruptedException ie) { // does NOT happend when you clode the frame !!!!!
return;
}
}
}
public void paint(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(0,0,this.getSize().width,this.getSize().height);
g.setFont(fnt);
g.setColor(Color.BLUE);
g.drawString(getTimeString(),20,30);
}
}
public class DigiClock extends JFrame {
ClockPanel theClock = new ClockPanel();
public DigiClock() {
this.setTitle("A nice clock");
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(theClock,BorderLayout.CENTER);
theClock.startClock();
}
public static void main(String[] args) {
DigiClock dc=new DigiClock();
dc.setBounds(20,50,400,70);
dc.setVisible(true);
}
}
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks