actionPerformed won't work

Thread Solved

Join Date: Oct 2009
Posts: 10
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster

actionPerformed won't work

 
0
  #1
Nov 19th, 2009
I can't figure out what's wrong - I was remaking the code from an older and messier version, but now I've hit a roadblock because what I say in the actionPerformed section just doesn't happen.

I suspect that it's because of the Timer, which a friend adviced me to link to an empty method (NullListener). But even when I move around using the keyListener, nothing happens! Why?

(Please remember that the things currently in the actionPerformed section are only there so I can see if it works at all, so the problem can't be within the method itself.)

  1. import java.awt.event.*;
  2. import javax.swing.*;
  3.  
  4. public class gameinit implements ActionListener
  5. {
  6. public static int number;
  7. public static JFrame p;
  8. public static Timer t;
  9. public static JLabel Iz;
  10. public static char rar = 'T';
  11.  
  12. public static void main(String args[])
  13. {
  14. p = new JFrame("Version 2.4");
  15. p.setSize(600,393);
  16.  
  17. NullListener Blanky = new NullListener();
  18. t = new Timer(50, Blanky);
  19. t.start();
  20. Maps.map0();
  21. p.setVisible(true);
  22. p.addKeyListener(new KeyListen());
  23. }
  24.  
  25. public void actionPerformed(ActionEvent e)
  26. {
  27. Izzy.hittable();
  28. Izzy.movement();
  29.  
  30. if(rar == 'T')
  31. {
  32. System.out.print("Sky High(Grand Nuave)");
  33. }
  34. }
  35.  
  36. static class KeyListen implements KeyListener
  37. {
  38. KeyListen(){}
  39.  
  40. public void keyPressed(KeyEvent e)
  41. {
  42. if(e.getKeyCode() == 39)
  43. {
  44. number = 1;
  45. Controls.test(number);
  46. }
  47. if(e.getKeyCode() == 37)
  48. {
  49. number = 2;
  50. Controls.test(number);
  51. }
  52. if(e.getKeyCode() == 32)
  53. {
  54. number = 3;
  55. Controls.test(number);
  56. }
  57. }
  58.  
  59. public void keyReleased(KeyEvent e)
  60. {
  61. if(e.getKeyCode() == 39 || e.getKeyCode() == 37)
  62. {
  63. char direction = Controls.test(number), dir;
  64. Izzy.stop(direction);
  65. }
  66. }
  67.  
  68. public void keyTyped(KeyEvent e)
  69. {}
  70. }
  71. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2,062
Reputation: javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold 
Solved Threads: 262
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Postaholic
 
0
  #2
Nov 19th, 2009
In order for this method to execute:
  1. public void actionPerformed(ActionEvent e)
  2. {
  3. Izzy.hittable();
  4. Izzy.movement();
  5.  
  6. if(rar == 'T')
  7. {
  8. System.out.print("Sky High(Grand Nuave)");
  9. }
  10. }

You need to add its listener to a component. The methods in the 'KeyListen' are executed because you do this:
  1. p.addKeyListener(new KeyListen());

When you want the actionPerformed method to execute?
Also since it it the gameinit class that implements the listener, it is an instance of that class that you will add:
  1. someComponent.addActionListener(new gameinit());
I murdered thousands for the Emperor and he gave me nothing except his damning silence. Now his lapdogs yap for every life I take, while the gods promise me the galaxy.
- Svane Vulfbad -
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: CuteCat is an unknown quantity at this point 
Solved Threads: 0
CuteCat CuteCat is offline Offline
Newbie Poster
 
0
  #3
Nov 19th, 2009
Strangely, I tried that - but the program complains about not finding the command. When looking at my old program, I found that it didn't contain the command, and thought that no such command was needed...

  1. C:\Documents and Settings\agnasp\Skrivbord\game02\gameinit.java:23: cannot find symbol
  2. symbol : method addActionListener(gameinit)
  3. location: class javax.swing.JFrame
  4. p.addActionListener(new gameinit());
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2,062
Reputation: javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold javaAddict is a splendid one to behold 
Solved Threads: 262
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Postaholic
 
0
  #4
Nov 19th, 2009
Check the API.
  1. p.addActionListener(new gameinit());
The JFrame class doesn't have such method: addActionListener.
Then search the API some more to find which elements accept an ActionListener (Buttons and such)
I murdered thousands for the Emperor and he gave me nothing except his damning silence. Now his lapdogs yap for every life I take, while the gods promise me the galaxy.
- Svane Vulfbad -
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 471 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC