Erro ItemListener Java

1 resposta Resolvido
S
import java.awt.*;
import java.awt.event.<em>;

import javax.swing.</em>;

public class TestJRadioButton extends JFrame {

private JTextField campo;

private JRadioButton amarelo,vermelho,azul;

private ButtonGroup onlyOne;

public TestJRadioButton()

{

super(Teste de JRadioButton);

Container container= getContentPane();

container.setLayout(new FlowLayout());

campo= new JTextField(Clique na cor desejada e veja o resultado,25);

campo.setFont(new Font(Serif,Font.PLAIN,14));

container.add(campo);

amarelo= new JRadioButton(Amarelo);

container.add(amarelo);

vermelho= new JRadioButton(Vermelho);

container.add(vermelho);

azul= new JRadioButton(Azul);

container.add(azul);

onlyOne= new ButtonGroup();

onlyOne.add(amarelo);

onlyOne.add(vermelho);

onlyOne.add(azul);

TratadorRadioButton trat= new TratadorRadioButton();

amarelo.  (trat);

vermelho.addItemListener(trat);

azul.addItemListener(trat);

setSize(285,100);

setVisible(true);

}

public static void main(String[] args) {

TestJRadioButton aplicacao= new TestJRadioButton();

aplicacao.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE);

}

private class TratadorRadioButton implements Itemlistener {

private Color cor;

public void itemStateChanged(ItemEvent evento)

{

if (evento.getSource()==vermelho)

cor= Color.red;

if(evento.getSource()==amarelo)

cor= Color.yellow;

if(evento.getSource()==azul)

cor= Color.blue;

campo.setBackground(cor);

}

}

}

Alguém pode me explicar porque isso está acontecendo com o item listener?

Exception in thread main java.lang.Error: Unresolved compilation problems:

The method addItemListener(ItemListener) in the type AbstractButton is not applicable for the arguments (TestJRadioButton.TratadorRadioButton)

The method addItemListener(ItemListener) in the type AbstractButton is not applicable for the arguments (TestJRadioButton.TratadorRadioButton)

The method addItemListener(ItemListener) in the type AbstractButton is not applicable for the arguments (TestJRadioButton.TratadorRadioButton)

Itemlistener cannot be resolved to a type
at TestJRadioButton.<init>(TestJRadioButton.java:27)
at TestJRadioButton.main(TestJRadioButton.java:34)

1 Resposta

staroski
Solucao aceita

O método addItemListener espera receber como parâmetro um objeto do tipo ItemListener.
Se você analisar com calma, vai perceber que sua classe TratadorRadioButton não implementa a interface ItemListener.

Mas há outros erros de compilação que também precisam ser resolvidos.

Veja:

import java.awt.*;

import java.awt.event.;                // erro de compilação
import javax.swing.;                // erro de compilação

public class TestJRadioButton extends JFrame {
    private JTextField campo;
    private JRadioButton amarelo, vermelho, azul;
    private ButtonGroup onlyOne;

    public TestJRadioButton() {
        super("Teste de JRadioButton");
        Container container= getContentPane();
        container.setLayout(new FlowLayout());
        campo= new JTextField("Clique na cor desejada e veja o resultado",25);
        campo.setFont(new Font("Serif",Font.PLAIN,14));
        container.add(campo);
        amarelo= new JRadioButton("Amarelo");
        container.add(amarelo);
        vermelho= new JRadioButton("Vermelho");
        container.add(vermelho);
        azul= new JRadioButton("Azul");
        container.add(azul);
        onlyOne= new ButtonGroup();
        onlyOne.add(amarelo);
        onlyOne.add(vermelho);
        onlyOne.add(azul);
        TratadorRadioButton trat= new TratadorRadioButton();
        amarelo. (trat);                // erro de compilação
        vermelho.addItemListener(trat);
        azul.addItemListener(trat);
        setSize(285,100);
        setVisible(true);
    }

    public static void main(String[] args) {
        TestJRadioButton aplicacao = new TestJRadioButton();
        aplicacao.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class TratadorRadioButton implements Itemlistener {        // erro de compilação
        private Color cor;

        public void itemStateChanged(ItemEvent evento) {
            if (evento.getSource() == vermelho)
                cor = Color.red;
            if (evento.getSource() == amarelo)
                cor = Color.yellow;
            if (evento.getSource() == azul)
                cor = Color.blue;
            campo.setBackground(cor);
        }
    }
}
Criado 15 de janeiro de 2018
Ultima resposta 17 de jan. de 2018
Respostas 1
Participantes 2