Need help with Java...

Associate
Joined
22 Aug 2008
Posts
1,104
Location
London
Im not too fluent with Java programming, but my lecturer wanted me to do something with it to demonstrate algorithm of something like a cat and mouse chase...

so far i've managed to get a grid somewhat working with 1 error to get rid of, but cant seem to get a solution for it.

Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;

import javax.swing.JLabel;


class algorithm extends JLabel implements ActionListener
{

    private JFrame window = new JFrame("Cheetah Rabbit Game "); 			// creating window
    private static int R_Board;									//Change the number to set the grid size

	private JLabel [][]cell= new JLabel [R_Board][R_Board];
	private String letter="";
	private int count = 0;
	private int rowIn= 0;
	private int colIn =0;


public algorithm()
	{
		window.setSize(100*R_Board,100*R_Board);				//what size the window will appear
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setLayout(new GridLayout(R_Board,R_Board));

                		for (int i=0;i<R_Board;i++)
		{
			for (int j=0;j<R_Board;j++)
			{
				cell[i][j] = new JLabel();
				cell[i][j].ActionListener();
				window.add(cell[i][j]);


				}
		}
	   window.setVisible(true);					//making the window visible


}
 public void actionPerformed(java.awt.event.ActionEvent a)
	{

		letter = "R";
		for (int x=0;x<R_Board;x++)
		{
			for (int y=0;y<R_Board;y++)
			{
				if(a.getSource()==cell[x][y])
				{
					cell[x][y].setText(letter);
					cell[x][y].setEnabled(true);
					rowIn=x;
					colIn=y;


					letter = "C";

			        { if(a.getSource()==cell[x][y])
							{
								cell[x][y].setText(letter);
								cell[x][y].setEnabled(true);
								rowIn=x;
								colIn=y;
							}

					    }

                    }
			    }
			}
}


}

Java is so highly frustrating... just this one error just wont go away no matter what i do (yes i've done my google searching and speaking to lecturers)

C:\Users\Chris\Desktop\Algorithm work\algorithm.java :35: cannot find symbol
symbol : method ActionListener()
location: class javax.swing.JLabel
cell[j].ActionListener();
^
1 error

Tool completed with exit code 1

Any help would be highly appreciated..
 
Last edited:
That error translates to: The method ActionListener() is undefined for the type JLabel

JLabel does not have a method called ActionListener.
 
What are you trying to do with this for loop?

Code:
for (int j=0;j<R_Board;j++) {
   celli][j] = new JLabel();
   cell[i][j].ActionListener();
   window.add(cell[i][j]);
}

You have an array that contains an array of JLabels, as RobH said
Code:
cell[i][j].ActionListener();

Doesn't make any sense?
 
Back
Top Bottom