import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer.*;
class Timer {
public static void main(String[] args) {
JFrame frame = new JFrame();
final int FIELD_WIDTH = 20;
final JTextField textField = new JTextField(FIELD_WIDTH);
frame.setLayout(new FlowLayout());
frame.add(textField);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
textField.setText(now.toString());
}
};
final int DELAY = 1000;
Timer t = new Timer();
t.start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
It could be a syntax error, but I don't think so because I copied this program straight out of a book. The line of code, 't.start();' has an error line under it saying that the start() method is undefined. At first, I thought that the start() method didn't exist, but I looked it up in the library.
The problem is that you're declaring your own Timer
class - so Timer t = new Timer()
is referring to your class rather than javax.swing.Timer
, and you don't declare a start
method. I'm pretty sure you want to use the javax.swing.Timer
class instead. So you want to remove the import javax.swing.Timer.*;
line, and rename your Timer
class to something else.
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.util.*;
public class TimerTest {
...
}
Having said that, you're not telling your timer to do anything...
See more on this question at Stackoverflow