Results 1 to 2 of 2
- 09-16-2012, 12:34 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 71
- Rep Power
- 0
Am I using the timer class correctly?
I followed a tutorial on building a timer program. Here's the code:
The problems are with these three statements: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) { } }
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?Java Code:timer = new Timer(1000, tc); timer.start(); timer.stop();
Thanks
Astralogic
- 09-16-2012, 12:41 PM #2
Member
- Join Date
- Mar 2012
- Posts
- 71
- Rep Power
- 0
Similar Threads
-
Java timer issue not running correctly
By alexandra12 in forum New To JavaReplies: 7Last Post: 08-16-2012, 10:47 PM -
Class and return statements question, not updating correctly
By killmenow in forum EclipseReplies: 2Last Post: 05-21-2012, 10:59 AM -
Timer Class Issue
By CuppaCoffee in forum New To JavaReplies: 3Last Post: 01-05-2012, 10:31 PM -
Stop a timer in other class
By warchieflll in forum Advanced JavaReplies: 15Last Post: 02-02-2011, 08:13 PM -
Help with Timer Class
By morfasto in forum New To JavaReplies: 2Last Post: 11-03-2009, 09:13 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks