Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Astralogic

Thread: Am I using the timer class correctly?

  1. #1
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Am I using the timer class correctly?

    I followed a tutorial on building a timer program. Here's the code:

    Java Code:
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Timer;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    
    
    public class TimerTutorial extends JFrame {
    	
    	JLabel promptLabel, timerLabel;
    	int counter;
    	JTextField tf;
    	JButton button;
    	Timer timer;
    	
    	public TimerTutorial() {
    		setLayout(new GridLayout(2, 2, 5, 5));
    		
    		promptLabel = new JLabel("Enter seconds:", SwingConstants.CENTER);
    		add(promptLabel);
    		
    		tf = new JTextField(5);
    		add(tf);
    		
    		button = new JButton("Start");
    		add(button);
    		
    		timerLabel = new JLabel("Waiting...");
    		add(timerLabel);
    		
    		Event e = new Event();
    		button.addActionListener(e);
    	}
    	
    	public class Event implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			int count = (int)(Double.parseDouble(tf.getText()));
    			timerLabel.setText("Time left: " + counter);
    			
    			TimeClass tc = new TimeClass(counter);
    			timer = new Timer(1000, tc);
    			timer.start();
    		}
    	}
    	
    	public class TimeClass implements ActionListener {
    		int counter;
    
    		public TimeClass(int counter) {
    			this.counter = counter;
    		}
    		
    		public void actionPerformed(ActionEvent tc) {
    			counter--;
    			
    			if (counter >= 1) {
    				timerLabel.setText("Time left: " + counter);
    			} else {
    				timer.stop();
    				timerLabel.setText("Done!");
    				Toolkit.getDefaultToolkit().beep();
    			}
    		}
    	}
    	
    	public static void main(String[] args) {
    
    	}
    
    }
    The problems are with these three statements:


    Java Code:
    timer = new Timer(1000, tc);
    timer.start();
    timer.stop();
    Eclipse says that the Timer() constructor takes no arguments and that the Timer class has no start() or stop() methods. Are there multiple timer class and I accidentally imported the wrong one?

    Thanks
    Astralogic

  2. #2
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: Am I using the timer class correctly?

    Never mind, I WAS importing the wrong Timer class.
    Fubarable likes this.

Similar Threads

  1. Java timer issue not running correctly
    By alexandra12 in forum New To Java
    Replies: 7
    Last Post: 08-16-2012, 10:47 PM
  2. Replies: 2
    Last Post: 05-21-2012, 10:59 AM
  3. Timer Class Issue
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 10:31 PM
  4. Stop a timer in other class
    By warchieflll in forum Advanced Java
    Replies: 15
    Last Post: 02-02-2011, 08:13 PM
  5. Help with Timer Class
    By morfasto in forum New To Java
    Replies: 2
    Last Post: 11-03-2009, 09:13 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
  •