| | |
actionPerformed won't work
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
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.)
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.)
Java Syntax (Toggle Plain Text)
import java.awt.event.*; import javax.swing.*; public class gameinit implements ActionListener { public static int number; public static JFrame p; public static Timer t; public static JLabel Iz; public static char rar = 'T'; public static void main(String args[]) { p = new JFrame("Version 2.4"); p.setSize(600,393); NullListener Blanky = new NullListener(); t = new Timer(50, Blanky); t.start(); Maps.map0(); p.setVisible(true); p.addKeyListener(new KeyListen()); } public void actionPerformed(ActionEvent e) { Izzy.hittable(); Izzy.movement(); if(rar == 'T') { System.out.print("Sky High(Grand Nuave)"); } } static class KeyListen implements KeyListener { KeyListen(){} public void keyPressed(KeyEvent e) { if(e.getKeyCode() == 39) { number = 1; Controls.test(number); } if(e.getKeyCode() == 37) { number = 2; Controls.test(number); } if(e.getKeyCode() == 32) { number = 3; Controls.test(number); } } public void keyReleased(KeyEvent e) { if(e.getKeyCode() == 39 || e.getKeyCode() == 37) { char direction = Controls.test(number), dir; Izzy.stop(direction); } } public void keyTyped(KeyEvent e) {} } }
0
#2 Nov 19th, 2009
In order for this method to execute:
You need to add its listener to a component. The methods in the 'KeyListen' are executed because you do this:
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:
Java Syntax (Toggle Plain Text)
public void actionPerformed(ActionEvent e) { Izzy.hittable(); Izzy.movement(); if(rar == 'T') { System.out.print("Sky High(Grand Nuave)"); } }
You need to add its listener to a component. The methods in the 'KeyListen' are executed because you do this:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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 -
- Svane Vulfbad -
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
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...
Java Syntax (Toggle Plain Text)
C:\Documents and Settings\agnasp\Skrivbord\game02\gameinit.java:23: cannot find symbol symbol : method addActionListener(gameinit) location: class javax.swing.JFrame p.addActionListener(new gameinit());
0
#4 Nov 19th, 2009
Check the API.
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)
Java Syntax (Toggle Plain Text)
p.addActionListener(new gameinit());
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 -
- Svane Vulfbad -
![]() |
Similar Threads
- Need 2 techies for tech-support, work from home (Tech / IT Consultant Job Offers)
- Exiting 1 out of 2 JFrames using a JButton? (Java)
- Panels, different kinds, and how they work together. (Java)
- Help getting this applet to work! (Java)
- Source Code that don't work? (Java)
- beans bound property: getNewValue() doesn't work out. Code attached (Java)
Other Threads in the Java Forum
- Previous Thread: Need to assign a value?
- Next Thread: Help with Recursion
Views: 471 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener add android applet application arguments array arraylist arrays binary build c++ chat class classes client code combobox compare compile component convert data database design detection draw eclipse error event exception file filei/o game givemetehcodez graphics gui helpwithhomework html ide image images inheritance input interface j2me java javafx jframe jpanel julia linked linked-list list loop method methods mobile netbeans newbie number object oracle os parameter pattern phone pixel print printing problem program programming project read recursion remote remove return robot scanner search server service set sms socket sort source sql string swing system test text thread time tree user






