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.)

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)
		{}
	}
}

Recommended Answers

All 3 Replies

In order for this method to execute:

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:

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:

someComponent.addActionListener(new gameinit());

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...

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());

Check the API.

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.