Actionlistener does not identify the recovered button

Asked

Viewed 125 times

3

My goal is to click the button Andar and then select on the board which direction the character will be moved to, as shown in the image:

inserir a descrição da imagem aqui

My button Andar has a ActionListener and is treated as follows:

public void actionPerformed(ActionEvent e) {
    Object escolha = e.getSource();

    if(escolha==andar)
        moverHumano(escolha);
}

But the guy "Object escolha" seems to detect no action within the class "moverHumano", even after adding the ActionListener positions on the character’s side.

This if, for example, is never understood to be true:

if(escolha == this.tabela[POSIÇÃO X][POSIÇÃO Y])

What I want to do is possible that way?

Below, the code of the MAIN class:

    package teste;
public class TESTE {
    public static void main(String[] args) {
        Tabuleiro cenario = new Tabuleiro();
        cenario.criarTela();
    }

}

BOARD CLASS:

package teste;

public class Tabuleiro extends JFrame implements ActionListener{
    private JButton tabela[][],andar,cura,sair,debug,percepcao,vida;
    private GridLayout layoutTabuleiro;
    Humano jogador1;

    public Tabuleiro(){ 
        super("Zumbicídio");
        setSize(800,600);
        setResizable(false);
        layoutTabuleiro = new GridLayout(14,14);
         setSize(800,600);
        jogador1 = new Humano();
        setVisible(true);
    }

    public void criarTela(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //ADICIONANDO ELEMENTOS
        this.visualizarTabuleiro();
        //MOSTRANDO A JANELA
        this.setVisible(true);
    }
    //No método abaixo eu adiciono tudo no tabuleiro, por isso ficou grande, mas boa parte pode ser ignorada. Acho que o que importa mesmo é ter adicionado os ActionListener, o que fiz logo no início. Adicionei printf's de teste nos locais que não funcionam no meu código original (isso tá lá embaixo, perto do método do ActionListener)
    public void visualizarTabuleiro(){
    final JPanel telaTabuleiro = new JPanel();
    int i, j;

    telaTabuleiro.setLayout(layoutTabuleiro);
    telaTabuleiro.setPreferredSize(new Dimension(600,550));

    // ADICIONANDO ACTION LISTENER
    tabela = new JButton[10][10];
    for(i=0;i<10;i++){      
        for(j=0;j<10;j++){
            this.tabela[i][j] = new JButton(new ImageIcon("res/images/desconhecido.png/"));
            this.tabela[i][j].addActionListener(this);  
        }
    }

    //ADICIONANDO ELEMENTOS

    //1 LINHA
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //2 LINHA (PRIMEIRA DO TABULEIRO) **************

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[0][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //3 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[1][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //4 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[2][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //5 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[3][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //6 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[4][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //7 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[5][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //8 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[6][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //9 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[7][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //10 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[8][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //11 LINHA

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    for(j=0;j<10;j++){
        telaTabuleiro.add(tabela[9][j]);
    }
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //12 LINHA
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //13 LINHA
    telaTabuleiro.add(new Label(" "));
    vida = new JButton("hp");
    telaTabuleiro.add(vida);
    percepcao = new JButton("percep");

    telaTabuleiro.add(percepcao);

    telaTabuleiro.add(new Label(" "));
    andar = new JButton("andar");
    andar.addActionListener(this);
    telaTabuleiro.add(andar);

    telaTabuleiro.add(new Label(" "));
    cura = new JButton("cura");
    cura.addActionListener(this);
    telaTabuleiro.add(cura);

    telaTabuleiro.add(new Label(" "));
    debug = new JButton("debug");
    debug.addActionListener(this);
    telaTabuleiro.add(debug);
    telaTabuleiro.add(new Label(" "));

    telaTabuleiro.add(new Label(" "));
    sair = new JButton("sair");
    sair.addActionListener(this);
    telaTabuleiro.add(sair);
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //14 LINHA
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label("    Vida "));
    telaTabuleiro.add(new Label("  Percep. "));

    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label("   Andar"));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label("   Cura"));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label("   Debug"));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label("     Sair"));
    telaTabuleiro.add(new Label(" "));
    telaTabuleiro.add(new Label(" "));

    //ADICIONANDO NO CONTENTPANE
    this.getContentPane().add(telaTabuleiro, BorderLayout.NORTH);
}

public void moverHumano(Object escolha){
    if(escolha==tabela[0][0]){
        System.out.println("Selecionou a primeira posição!"); // O QUE DESEJO TESTAR E NÃO ESTÁ FUNCIONANDO
    }
}

@Override
public void actionPerformed(ActionEvent ae) {  
    Object escolha = ae.getSource();
    if(escolha==andar){
        System.out.println("Escolheu andar");
        moverHumano(escolha);
    }
}

HUMAN CLASS:

    package teste;
public class Humano {
    private int saude, percep;
    Humano(){
        this.saude = 5;
        this.percep = 3;
    }

    public int getSaude(){
        return this.saude;
    }   

    public int getPercepcao(){
        return this.percep;
    }

}

1 answer

1

I found some errors and listed them so you can check if they may be the cause of the problem:

  • All graphic applications involving the swing API should be dispatched to the EDT;

  • Do not use class Label. If you are programming in java-swing, the component that should be used is JLabel. This needs to be kept in mind, as almost all components of the package javax.swing has equivalents in the java.awt, and as swing is an evolution of awt, you should always use the components of the package javax.swing;

  • no need to implement ActionListener in a JFrame. If you’re going to apply the Listener to just one button, do this adding the inline Listener or if the Listener is applied to other buttons, create a class the isolated part that implements the interface ActionListener.

  • Both in the method moverHumano() how much in the ActionListener, you are treating a button component like Object, and trying to compare to a variable of type JButton, and this verification will never be true, even if the same component is passed there. Inside the actionPerformed() you need to cast for JButton before comparing:

@Override
public void actionPerformed(ActionEvent ae) {
    JButton escolha = (JButton) ae.getSource();
    if (escolha == andar) {
        System.out.println("Escolheu andar");
        moverHumano(escolha);
    }
}

And in the method moverHumano(), instead of receiving a Object, change to JButton. Remembering that for the simple fact that the system is applied to a button, in this condition the cast will always be safe, just watch out for cases where the system is shared with different components (as between JButton and JToggleButton).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.