Duvida com JButton!

5 respostas
J

Quando vou rodar o codigo abaixo, o Netbeans acusa o seguinte erro:

[color=red]Exception in thread "main" java.lang.ExceptionInInitializerError
at exercicio3a.Exercicio3A.main(Exercicio3A.java:20)
Caused by: java.lang.RuntimeException: Uncompilable source code - exercicio3a.Interface3A.ButtonHandler is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
at exercicio3a.Interface3A.(Interface3A.java:58)
... 1 more
Java Result: 1[/color]

Porque o código não compila?

[size=18]MAIN:[/size]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercicio3a;

import javax.swing.JFrame;

/**
 *
 * @author Usuario
 */
public class Exercicio3A {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Interface3A calculos = new Interface3A();
        calculos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        calculos.setSize(210,160);
        calculos.setVisible(true);
    }
}
[size=18]CLASSE:[/size]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercicio3a;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;

/**
 *
 * @author Usuario
 */
public class Interface3A extends JFrame {

    private JTextField entrada1;
    private JTextField entrada2;
    private JPanel painel1;
    private JLabel texto1;
    private JLabel texto2;
    private JLabel texto3;
    private JLabel texto4;
    private JButton calcular;
    private double base, altura, area, perimetro;

    public Interface3A() {
        super("Retangulo");
        super.setLayout(new FlowLayout());

        entrada1 = new JTextField();
        entrada2 = new JTextField();
        texto1 = new JLabel("Valor da base:");
        texto2 = new JLabel("Valor da altura:");
        texto3 = new JLabel();
        texto4 = new JLabel();
        calcular = new JButton("Calcular");
        painel1 = new JPanel();

        painel1.add(texto1);
        painel1.add(entrada1);
        painel1.add(texto2);
        painel1.add(entrada2);
        painel1.add(texto3);
        painel1.add(texto4);
        painel1.add(calcular);
        painel1.setLayout(new GridLayout(4,2,1,1));

        super.add(painel1);

        ButtonHandler registro = new ButtonHandler();
        calcular.addActionListener(registro);

    }

    private class ButtonHandler implements ActionListener {

        public void actionerformed(ActionEvent event) {

            base = Double.parseDouble(entrada1.getText());
            altura = Double.parseDouble(entrada2.getText());
            area = base * altura;
            perimetro = (2 * base) + (2 * altura);
            texto3.setText(String.format("Area = %2.f", area));
            texto4.setText(String.format("Perimetro = %2.f", perimetro));

        }
    }
}

5 Respostas

ViniGodoy

Dica, leia a mensagem de erro:
ButtonHandler is not abstract and does not override abstract method actionPerformed

 olhe sua classe:

private class ButtonHandler implements ActionListener {

public void [color=red]actionerformed[/color](ActionEvent event) {

E aí corrija o nome do método.

J

Sei um pouco o que esta acontecendo, porem não estou conseguindo resolver esse problema.
O que deveria ser feito?

ViniGodoy

Voce deve escrever actionPerformed no trecho que destaquei em vermelho. Você esqueceu da letra P.

J
Ja coloquei isso!

Quando executo ele aparece a JFrame, porem quando o botao calcular e pressionado aparece os seguintes erros:

[color=red]

Exception in thread AWT-EventQueue-0 java.util.UnknownFormatConversionException: Conversion = 2

at java.util.Formatter.checkText(Formatter.java:2547)

at java.util.Formatter.parse(Formatter.java:2533)

at java.util.Formatter.format(Formatter.java:2469)

at java.util.Formatter.format(Formatter.java:2423)

at java.lang.String.format(String.java:2845)

at exercicio3a.Interface3A$ButtonHandler.actionPerformed(Interface3A.java:67)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)

at java.awt.Component.processMouseEvent(Component.java:6504)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)

at java.awt.Component.processEvent(Component.java:6269)

at java.awt.Container.processEvent(Container.java:2229)

at java.awt.Component.dispatchEventImpl(Component.java:4860)

at java.awt.Container.dispatchEventImpl(Container.java:2287)

at java.awt.Component.dispatchEvent(Component.java:4686)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)

at java.awt.Container.dispatchEventImpl(Container.java:2273)

at java.awt.Window.dispatchEventImpl(Window.java:2713)

at java.awt.Component.dispatchEvent(Component.java:4686)

at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)

at java.awt.EventQueue.access$000(EventQueue.java:101)

at java.awt.EventQueue$3.run(EventQueue.java:666)

at java.awt.EventQueue$3.run(EventQueue.java:664)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)

at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)

at java.awt.EventQueue$4.run(EventQueue.java:680)

at java.awt.EventQueue$4.run(EventQueue.java:678)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

[/color]
ViniGodoy

Deveria ser %.2f e não %2.f como você escreveu. Preste MUITA atencao ao copiar código.

Criado 7 de março de 2012
Ultima resposta 7 de mar. de 2012
Respostas 5
Participantes 2