Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Timer
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
alex69s




PostPosted: Thu Jun 02, 2005 10:53 pm   Post subject: Timer

Hey all, I'm new to these forums, although I've read some of the tutorials. Great website! Anways, we just started GUIs in my Grade 11 compsci class, and I'm making little programs that will hopefully get me more familliar with the code. I'm trying to make a timer program, in which the user enters the time they want to count down, and then the program counts down. I know the delay method in turing, so I looked and found the Thread.delay() method for java. Here is my code, and it just seems to take a second, then displays 0 : 0. How do I make it that each time it goes through the loop it changes the JLabel to the values I have assigned it? Here is my program, hopefully its not a silly mistake on my part:

code:
// The "TeaTimer" class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TeaTimer extends JApplet implements MouseListener
{
    // Place instance variables here
    static int secondsi, minutesi;
    static JTextField mins = new JTextField (6);
    static JTextField secs = new JTextField (6);
    static JLabel min = new JLabel ();
    static JLabel colon = new JLabel (":");
    static JLabel sec = new JLabel ();
    static JFrame frame = new JFrame ("Timer!");
    public void init ()
    {

        JPanel panel = new JPanel ();
        JLabel prompt = new JLabel ("           Please enter the minutes and seconds:");
        JLabel minutes = new JLabel ("Minutes:");
        JLabel seconds = new JLabel ("Seconds:");

        JButton go = new JButton ("                                       Go!                                       ");
        go.addMouseListener (this);
        panel.add (prompt);
        panel.add (minutes);
        panel.add (mins);
        panel.add (seconds);
        panel.add (secs);
        panel.add (go);
        panel.add (min);
        panel.add (colon);
        panel.add (sec);
        frame.setContentPane (panel);
        frame.setSize (300, 200);
        frame.show ();
    } // init method



    public static void output (int loop)
    {
        for (int x = 0 ; x < loop ; x++)
        {


            if (secondsi == 1)
            {
                secondsi--;
                secondsi += 60;
                minutesi--;
                min.setText (Integer.toString (minutesi));
            }
            else
            {
                secondsi--;

            }
            if (secondsi == 1 && minutesi == 0)
                secondsi = 0;
            sec.setText (Integer.toString (secondsi));
            frame.show ();



            try
            {
                Thread.sleep (1); //1000
            }
            catch (Exception e)
            {
                ;
            }



        }


    }


    public void mouseClicked (MouseEvent e)
    {
        minutesi = Integer.parseInt (mins.getText ());
        secondsi = Integer.parseInt (secs.getText ());
        int loop = (minutesi * 60) + secondsi - 1;
        output (loop);

    }


    public void mouseEntered (MouseEvent e)
    {
    }


    public void mouseExited (MouseEvent e)
    {
    }


    public void mousePressed (MouseEvent e)
    {
    }


    public void mouseReleased (MouseEvent e)
    {
    }
} // TeaTimer class







Thanks for help in advance
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: