6 Replies - 15666 Views - Last Post: 02 May 2009 - 08:07 PM Rate Topic: -----

#1 kakarot12  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 27-March 09

Error Message: ActionListener cannot be resolved to a type

Posted 02 May 2009 - 06:32 PM

So, this is my code:
 pStartButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				pGame.startGame();
				pFrame.requestFocus();
			}
		});
		pGame.addScoreListener(new ScoreListener()
		{
			public void scoreChange(ScoreEvent e)
			{
				pScoreLabel.setText("Score: "+ e.getScore());
			}
		});



however, i get an error message saying that ActionListener cannot be resolved to a type. How can that be? I have the appropriate packages imported already, so why?

btw, this is a snippet of the full code. if the full code is needed, i will post that as well. I don't understand why ActionListener is a problem!! Assistance is greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Error Message: ActionListener cannot be resolved to a type

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Error Message: ActionListener cannot be resolved to a type

Posted 02 May 2009 - 07:18 PM

Looks like you created your own event type. Is that all done correctly?

Well, ScoreListener isn't in the default Java library, could be in some add on I'm unaware of.
Was This Post Helpful? 0
  • +
  • -

#3 kakarot12  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 27-March 09

Re: Error Message: ActionListener cannot be resolved to a type

Posted 02 May 2009 - 07:25 PM

View PostKYA, on 2 May, 2009 - 06:18 PM, said:

Looks like you created your own event type. Is that all done correctly?

Well, ScoreListener isn't in the default Java library, could be in some add on I'm unaware of.


Yes, scoreListener was an event that I created for use in the program. That doesn't have any errors associated with it, but for some reason when I go to add ActionListener to one of my buttons, it give me that error message.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Error Message: ActionListener cannot be resolved to a type

Posted 02 May 2009 - 07:27 PM

On the actionListerner button or the ScoreListener? Since you most likely extended actionListener for your scoreListener I'm confused, could you be a bit more specific, copy/paste the error and line please :)
Was This Post Helpful? 0
  • +
  • -

#5 kakarot12  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 27-March 09

Re: Error Message: ActionListener cannot be resolved to a type

Posted 02 May 2009 - 07:50 PM

View PostKYA, on 2 May, 2009 - 06:27 PM, said:

On the actionListerner button or the ScoreListener? Since you most likely extended actionListener for your scoreListener I'm confused, could you be a bit more specific, copy/paste the error and line please :)


import javax.swing.*;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class TetrisGUI implements KeyListener
{
	final TetrisGame 	 pGame		= new TetrisGame();
	final TetrisBoardGUI pBoardGUI	= new TetrisBoardGUI();
	final JFrame		 pFrame	   = new JFrame("Tetris ");
	final JButton		pStartButton = new JButton("Start");
	final JLabel		 pScoreLabel  = new JLabel("Score: ");
	
	public TetrisGUI()
	{
		final Container pane = pFrame.getContentPane();
		
		pane.setLayout(new BorderLayout());
		pane.add(pBoardGUI, BorderLayout.CENTER);
		pane.add(pStartButton, BorderLayout.SOUTH);
		pane.add(pScoreLabel, BorderLayout.NORTH);
		
ERROR IS HERE--> pStartButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				pGame.startGame();
				pFrame.requestFocus();
			}
		});
		pGame.addScoreListener(new ScoreListener()
		{
			public void scoreChange(ScoreEvent e)
			{
				pScoreLabel.setText("Score: "+ e.getScore());
			}
		});
		pFrame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		
		pGame.addBoardListener(pBoardGUI);
		pFrame.addKeyListener(this);
		pFrame.pack();
		pFrame.show();
	}
	public void keyPressed(KeyEvent e)
	{
		switch(e.getKeyCode())
		{
		case KeyEvent.VK_LEFT  : pGame.move(TetrisPiece.LEFT);
		case KeyEvent.VK_RIGHT : pGame.move(TetrisPiece.RIGHT);
		case KeyEvent.VK_UP	: pGame.move(TetrisPiece.UP);
		case KeyEvent.VK_DOWN  : pGame.move(TetrisPiece.DOWN);
		case KeyEvent.VK_SHIFT : pGame.move(TetrisPiece.FALL);
		}
	}
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}
	public static void main(String []args)
	{
		new TetrisGUI();
	}
	public class TetrisBoardGUI extends JPanel implements BoardListener
	{
		private TetrisBoard pBoard;
		public void boardChange(BoardEvent e)
		{
			pBoard=(TetrisBoard)e.getSource();
			repaint();
		}
		public void paintComponent(Graphics graphics)
		{
			final int width = getBounds().width;
			final int height = getBounds().height;
			
			final Image  image = createImage(width,height);
			final Graphics g = image.getGraphics();
			
			if(pBoard!=null)
			{
				final int numCols = pBoard.getColumns();
				final int numRows= pBoard.getRows();
				for(int cols=0; cols <numCols; cols++)
				{
					for(int rows= 0; rows < numRows; rows++)
					{
						final int piece = pBoard.getPieceAt(cols,rows);
						if(piece != TetrisBoard.EMPTY_BLOCK)
						{
							g.setColor(getPieceColor(piece));
							drawBlock(g,(cols*width/numCols)+1,(rows*height/numRows)+ 1, (height/numRows - 1), piece);
							
						}
					}
				}
			}
			g.setColor(Color.blue);
			g.drawRect(0,0,width-1,height-1);
			graphics.drawImage(image, 0,0,width, height, null);
		}
		private void drawBlock(Graphics g, int x, int y, int width, int height)
		{
			g.fillRect(x, y, width, height);
			g.setColor(Color.black);
			g.drawRect(x, y, width, height);
		}
		public Dimension getPreferredSize()
		{
			return new Dimension(200,600);
		}
		private Color getPieceColor(int color)
		{
			Color result= null;
			switch(color)
			{
			case TetrisPiece.L_PIECE : result = new Color(24,105,198); //turquoise
										break;
			case TetrisPiece.J_PIECE: result = new Color(206,56,173); //purple
									  break;
			case TetrisPiece.I_PIECE: result= new Color(41,40,206); //blue
										break;
			case TetrisPiece.Z_PIECE: result = new Color(212, 0, 0); //red
										break;
			case TetrisPiece.S_PIECE : result = new Color(82,154,16); //green
										break;
			case TetrisPiece.O_PIECE: result= new Color(123,121,123); //gray
										break;
			case TetrisPiece.T_PIECE: result = new Color(156,142,8); //yellow
			}
			return result;
		}
	}

}



that is the full code. the line with the error in it is designated with ERROR IS HERE-->.
I can't add an Action listener to the Start button. When I do, it says that ActionListener cannot be resolved to a type.

the error message is the following:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	ActionListener cannot be resolved to a type



when i add Action listener in the line where it says implements KeyListener, I still get an error.

This post has been edited by kakarot12: 02 May 2009 - 07:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Error Message: ActionListener cannot be resolved to a type

Posted 02 May 2009 - 07:58 PM

import java.awt.event.*;



Rather then

import java.awt.Event.*;




Then adding an actionListener works for me:

buttonName.addActionListener(
				new ActionListener(){
					public void actionPerformed( ActionEvent event ) {
					}
				}
);


This post has been edited by KYA: 02 May 2009 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#7 kakarot12  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 27-March 09

Re: Error Message: ActionListener cannot be resolved to a type

Posted 02 May 2009 - 08:07 PM

Thanks. The method is fine now.

When I click the start button, nothing appears!!! Why!!!

This post has been edited by kakarot12: 02 May 2009 - 08:12 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1