Jogo da Velha dando erro

2 respostas
M

Ola pessoal estou tentando desenvolver um jogo em Java, usando o JButton, crie a Grid tudo certinho esta rodando ! O problema e no método cria_Mat, onde tenho que atribuir as funcionalidades dos botões usando:

addActionListener( new ActionListener() {
@override

public void actionPerformed(ActionEvent e){
Mas esta dando o seguinte erro ;

--------------------Configuration: --------------------
C:\Tabuleiro.java:36: cannot find symbol
symbol : class ActionListener
location: class Tabuleiro
mat[l][c].addActionListener( new ActionListener() {
^
C:\Tabuleiro.java:46: cannot find symbol
symbol : class ActionListener
location: class Tabuleiro
mat[l][c].addActionListener( new ActionListener() {
^
2 errors

Process completed.

segue o codigo completo :

import java.awt.*;
import javax.swing.*;  
import java.awt.Event.*;  
import javax.swing.border.EmptyBorder;  

    public class Tabuleiro extends JFrame{      
    	JButton cor;
    	JButton[][]mat;
    	JPanel PainelJogo;
    	int jogador,caracter;
    	String simbolo;
        

     public Tabuleiro(){
      super("Jogo da Velha ");
      ((JComponent)getContentPane()).setBorder(new EmptyBorder(5,5,5,5));
	 	  
	  	PainelJogo = new JPanel();
	  	PainelJogo.setLayout(new GridLayout(3,3));
	  	cor = new JButton();
	  	cria_mat();
	  	
	  	        
	  
     }      
    
    
        public void  cria_mat(){           	
    	JButton mat[][] = new JButton[3][3];
    	for(int l=0;l<3;l++){    	
    		for(int c=0;c<3;c++){
    			mat[l][c] = new JButton("");    			    	        		        		
      	        mat[l][c].setFont(new Font("Calibri",Font.BOLD,70));
      	        if(mat[l][c]==mat[0][0])
      	        mat[l][c].addActionListener( new ActionListener() {        	        
                @override
                
                public void actionPerformed(ActionEvent e){                
                jogada(1);
                }
                 
              });
            else
               if(mat[l][c]==mat[0][1])
                  mat[l][c].addActionListener( new ActionListener() { 
                  @override
                
                public void actionPerformed(ActionEvent e){                
                jogada(2);
                }
                 
              });
                   
                
                PainelJogo.add(mat[l][c]);
                
                add(PainelJogo);
                setLocation(470,200);
                setSize(400,400);
                setVisible(true);
                
              }  
           }         
    	
    } 
    
    public void jogada(int caracter ){	
    	
    	  

    	if(jogador == 0){
    	   simbolo = "X";
         
         }else{
         	
           simbolo = "O";
           jogador =0;
           
          }
          
          mat[caracter][caracter].setText(simbolo);
          mat[caracter][caracter].setEnabled(false);
    	   	
    	}
    	
    	
    }

2 Respostas

S

O Override está está minusculo, já verificou se é isso?

@Override
CWeiler

cannot find symbol
symbol : class ActionListener

Traduzindo: “não encontrei a referência para ActionListener”

Para este erro específico o import está errado, o correto é: “import java.awt.event.*;” com “E” minúsculo.

Após corrigir este erro você irá enfrentar o erro comentado pelo socialclub também.

Criado 2 de novembro de 2014
Ultima resposta 3 de nov. de 2014
Respostas 2
Participantes 3