Results 1 to 4 of 4
  1. #1
    taishan is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default beginner to create a countup timer

    hi guys, i am new to java, and i want to create a timer that counts up that just look like this :

    00 : 00 : 00 which corresponds to hr : min : sec

    the only problem i m having right now is to "add" the leading zeros to the timer, when sec/min/hr is less than 10, because java only display 1 digit if the number is less than 10, if the number is 5, i want it to be "05"

    i've done some code for this timer, please help me if i am doing anything wrong with the code, i want to just use basic java methods, using JFrame and If statements to make the timer, anyway here's my code, if possible please help me fill in the code which adds the leading zeros when the numbers are less than 10, i've been struggling with this for a long time, thanks alot guys:

    public class ClockClass extends JFrame {
    public Timer clock;
    public int secs = 0;
    public int mins = 0;
    public int hrs = 0;
    public JLabel display;

    public ClockClass() {

    setTitle("My Clock");
    setLocation(20, 20);
    setSize(400, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    FlowLayout layout = new FlowLayout();
    setLayout( layout );

    display = new JLabel();
    add(display);

    clock = new Timer(10, this);
    clock.start();

    setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

    if(e.getSource() == clock){
    secs++;

    }

    if(secs == 60){
    mins++;
    secs = 0;

    }

    if(mins == 60){
    hrs++;
    mins = 0;
    secs = 0;

    }

    if(hrs == 24){
    hrs = 0;
    mins = 0;
    secs = 0;
    }
    display.setText(String.valueOf(hrs)+":"+String.val ueOf(mins)+":"+String.valueOf(secs));

    }

    public static void main(String[] args) {

    new ClockClass();

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    DecimalFormat is what you need. You define it via:
    Java Code:
    DecimalFormat dFormat = new DecimalFormat("00");
    which will format numbers to show leading zero if < 10 and use it like so:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import javax.swing.*;
    
    public class ClockClass extends JFrame implements ActionListener
    {
      public Timer clock;
      public int secs = 0;
      public int mins = 0;
      public int hrs = 0;
      public JLabel display;
      private DecimalFormat dFormat = new DecimalFormat("00");
    
      public ClockClass()
      {
        setTitle("My Clock");
        setLocation(20, 20);
        setSize(400, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        FlowLayout layout = new FlowLayout();
        setLayout(layout);
    
        display = new JLabel();
        add(display);
    
        clock = new Timer(10, this);
        clock.start();
    
        setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e)
      {
    
        if (e.getSource() == clock)
        {
          secs++;
        }
    
        if (secs == 60)
        {
          mins++;
          secs = 0;
        }
    
        if (mins == 60)
        {
          hrs++;
          mins = 0;
          secs = 0;
        }
    
        if (hrs == 24)
        {
          hrs = 0;
          mins = 0;
          secs = 0;
        }
        display.setText(
            dFormat.format(hrs) + ":" + 
            dFormat.format(mins) + ":" + 
            dFormat.format(secs));
      }
    
      public static void main(String[] args)
      {
        new ClockClass();
      }
    }

  3. #3
    taishan is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    thank you very much, it really helps me alot. all these time i've been trying to use the "if" statements to add leading zeros to it, lol, i could just add a simple decimalformat that i didnt know about, thanks alot

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 08:14 AM
  2. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 05:33 PM
  3. Replies: 16
    Last Post: 05-08-2008, 09:45 PM
  4. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 09:41 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •