Hello again !
Today I thought I would share one of my "best" works. It took along while and was quite hard to understand and how to use it; But I have fixed it. So here is my Digital watch.(Sorry I can't explain everything quite good, am right now 50% understanding what I have done to get the watch work lol...)
*Side note* It won't show updating time, only the time you requested for.(I have still along way to go with boring java.)
So lets begin...
This code was made logically and took along time to get right construction. Now due to my ability not to be able to explain how this code works; but still I will try and do my best!Code:public class showtime { private float t, m, s;//We setup instructions private boolean showsec = true;//allow to show our box of time public void set (float h, float min, float sec) { if (h>=0 && h<24 && min>=0 && min<60 && sec>=0 && sec <60) {//Counter t=h;//defenitions m=min;//defenitions s=sec;//defenitions } else System.out.println ("Wrong hour");//Error message } public void setshowsec(boolean show) {//function showsec = show; } public float readh() {//function out of our defenition return t; } public float readMin() {//function out of our defenition return m; } public float readSec() {//function out of our defenition return s; } public String toString () {//Print out time in right order, String time = t + ":" + m; if (showsec) time = t + ":" + m + ":" + s; return time; } public void tick() { s = s + 1; if (s==60){ s = 0; m = m + 1; } if (m==60){ m = 0; t = t + 1; } if (t==24){ t = 0; } } }
Save it on your folder where you keep your java files or so...
Next step lets fix it so it can sync to our local machines time and get it out. This thing we made was kind of a counter, that will check the time and print out it for us(correct me if am wrong!)
Well this code is our main function to get out the time, now we can use it freely.Code:import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; public class whatsthetime { public static void main (String[] arg) { } } class thetime extends JFrame implements ActionListener {//Our time catcher private showtime tp = new showtime ();//reading our timer we made private JLabel l; public thetime () {//new function javax.swing.Timer h = new javax.swing.Timer (1000, this); h.start (); Calendar c = Calendar.getInstance ();//function of our watch tp.set (c.get (Calendar.HOUR_OF_DAY),//what to show c.get (Calendar.MINUTE),//what to show c.get (Calendar.SECOND));//what to show l = new JLabel (tp.toString(), JLabel.CENTER);//Gui menu add (l); l.setOpaque(true);//so it can be seen l.setBackground (Color.black);//background color l.setForeground (Color.white);//foreground color l.setFont (new Font("SanSerif", Font.BOLD, 24));//text size setSize (200,75);//box size setVisible (true);//if it should be visible setDefaultCloseOperation (EXIT_ON_CLOSE);//exit on our demand } public void actionPerformed (ActionEvent e) {//The catcher tp.tick(); l.setText (tp.toString());//the outputer } }
Last part, to print out the time "we have catched"...
So here is the whole how to printout the time in java. Myself this was kinda fun and challening. I might say this is my last thing for a while with java. So nowdays I will more of my lovely SICP.Code:import java.util.*; import javax.swing.*; public class time { public static void main (String[] arg) { showtime tp = new showtime ();//Will look for our main function Calendar c = Calendar.getInstance ();//Look up tp.set (c.get (Calendar.HOUR_OF_DAY),// c.get (Calendar.MINUTE),// c.get (Calendar.SECOND));// JOptionPane.showMessageDialog (null, "The time is " + tp.toString()); }//Printout our time }
P.S, Sorry if I couldn't explain it for you guys as good I have to
Anyways enjoy!
Lol, Lovely and thanks![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks