Results 1 to 6 of 6
Thread: Proper swing timer syntax
- 06-03-2011, 11:21 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Proper swing timer syntax
I have been trying to get the proper syntax for the swing timer yet have not been able to get rid of all of the bugs that pop up. These are two ways that I have seen online. Netbeans has underlined the Timer constructor in both due to, as far as I can tell, incorrect type parameters, because Timer requires (int, ActionListener). If it is necessary to use an actionlistener, is there a way to have the timer start at the startup of the program?
Note that I have put in 'hello world' instead of my code, but the bug remains the same.
Java Code:private void timer() { //method 1 Action hi = new AbstractAction() { public void actionPerformed(ActionEvent e) { if (true) { System.out.println("hello world"); } } }; Timer t = new Timer(1000, hi).start(); //method 2 Timer timers = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("hello world"); } }); }
- 06-03-2011, 11:57 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
What bug?
What error are you getting and where?
- 06-03-2011, 02:18 PM #3
Show your import statement.
db
- 06-03-2011, 10:14 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
I have the entire program code below. I am trying to use the timer to animate the oval's movement across the JPanel. I doubt this is the issue, but it may be. I just get a 'cannot find symbol' explanation for the underlined Timer constructor, and my guess is that it is due to an incorrect Timer constructor:
Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JPanel; import java.util.Timer; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JFrame; import javax.swing.SwingUtilities; class MyPanel extends JPanel { private int ovalx = 100; private int ovaly = 80; public MyPanel() { setBorder(BorderFactory.createLineBorder(Color.black)); } private void timer() { //method 1 Action hi = new AbstractAction() { public void actionPerformed(ActionEvent e) { if (true) { System.out.println("hello world"); } } }; Timer t = new Timer(1000, hi).start(); //method 2 Timer timers = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("hello world"); } }); } private void moveOval(int x, int y) throws InterruptedException { for (int i = 0; i < 5; i++) { if (ovalx != x || ovaly != y) { repaint(ovalx, ovaly, 50, 50); ovalx = x; ovaly = y; repaint(ovalx, ovaly, 50, 50); Thread.sleep(200); } x = x + 30; y = y + 30; } } public void paintComponent(Graphics g) { super.paintComponent(g); g.fillOval(ovalx, ovaly, 50, 50); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { //System.out.println("Created GUI on EDT? "+ SwingUtilities.isEventDispatchThread()); JFrame f = new JFrame("Swing Paint Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new MyPanel()); f.pack(); f.setVisible(true); } }
- 06-03-2011, 10:38 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
I got it! You were right DarrylBurke. After copying, pasting, and finding bugs, I see that I imported java.awt.Timer, not javax.swing.Timer, and now it works. Thanks for your help!
- 06-04-2011, 02:13 AM #6
Similar Threads
-
help with Swing Timer animation
By tomas1991 in forum New To JavaReplies: 7Last Post: 03-19-2010, 09:06 AM -
Swing Timer Questions
By morfasto in forum New To JavaReplies: 9Last Post: 11-14-2009, 03:48 PM -
Timer on swing
By finzaiko in forum AWT / SwingReplies: 3Last Post: 04-02-2009, 07:45 AM -
Timer in Swing app - refreshing label
By qoqosz in forum New To JavaReplies: 5Last Post: 03-09-2009, 02:17 AM -
[SOLVED] Swing Timer issue
By Doctor Cactus in forum New To JavaReplies: 6Last Post: 03-03-2009, 12:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks