What is my Timer constructor complaining about ?
Code:
package myPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SimpleGui1B
{
JButton button,button2,button3;
JFrame frame;
MyDrawPanel myPanel;
int x;
int y;
public void go()
{
Timer timer = new Timer(1000, new MyTimerActionListener());
myPanel = new MyDrawPanel();
frame = new JFrame();
button = new JButton("click me");
button2 = new JButton("click me");
button3 = new JButton("click me");
frame.add(myPanel);
button.addActionListener(new MyTimerActionListener());
button2.addActionListener(new MyTimerActionListener());
button3.addActionListener(new MyTimerActionListener());
myPanel.add(button);
myPanel.add(button2);
myPanel.add(button3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args)
{
SimpleGui1B gui = new SimpleGui1B();
gui.go();
}
class MyTimerActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i<100; i++)
{
x++;
y++;
System.out.println(i);
myPanel.repaint();
frame.repaint();
}
JButton temp = (JButton) e.getSource();
temp.setText("CLICKED");
temp.setEnabled(false);
}
}
class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.green);
g.fillOval(x, y, 40, 40);
System.out.println("REPAINTING THE COMPONENT");
}
}
}
For the line that has
Timer timer = new Timer(1000,new MyTimerActionListener());
I get:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor Timer(int, SimpleGui1B.MyTimerActionListener) is undefined
at myPackage.SimpleGui1B.go(SimpleGui1B.java:34)
at myPackage.SimpleGui1B.main(SimpleGui1B.java:29)
What is wrong with this, can anyone help ?
Re: What is my Timer constructor complaining about ?
The Java core classes have two Timer classes ,and you are using the wrong one, a util Timer:
import java.util.Timer;
Instead you want to use a Swing Timer:
import javax.swing.Timer;
Re: What is my Timer constructor complaining about ?
Wrong Timer; you want the javax.swing.Timer, not the java.util.Timer.
kind regards,
Jos
edit: way too slow, but it's Sunday so it doesn't count ;-)
Re: What is my Timer constructor complaining about ?
Re: What is my Timer constructor complaining about ?
Quote:
Originally Posted by
JosAH
edit: way too slow, but it's Sunday so it doesn't count ;-)
Some things never change :P:
db
Re: What is my Timer constructor complaining about ?
Quote:
Originally Posted by
DarrylBurke
Some things never change :P:
It was Fubarable's fault; honest; I didn't do anything so I'm innocent.
kind regards,
Jos ;-)
Re: What is my Timer constructor complaining about ?
You can't blame me. I live in a Grolsch-free zone (much to my lament).
Re: What is my Timer constructor complaining about ?
Quote:
Originally Posted by
Fubarable
You can't blame me. I live in a Grolsch-free zone (much to my lament).
What did you do to be punished like that?
kind regards,
Jos