Add actions to buttons

0

I have this code:

How to add actions to the buttons of this calculator, for the moment it is only the intefaz and try to put the sum.

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.Panel;

public class Calculadora extends JFrame
{
    private JTextField tfNumero = new JTextField(8);

    private JButton bSuma, bResta, bMultiplica, bDivide, bIgual, bClear, bPunto;
    private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
    private JButton bFactorial, bExpo, bRaiz, bCuadrado;
    private Panel panel1, panel2;

    private float n1, n2;
    private String operacion = "";
    private String strNumero = "";
    private int base, x;

    public Calculadora()
    {
        super("Calculadora");

        panel1 = new Panel();
        panel2 = new Panel();
        panel1.setLayout(new GridLayout(1,2));
        panel2.setLayout(new GridLayout(5,4));


        panel1.add(tfNumero);

        bClear = new JButton("clear");
        panel1.add(bClear);


        b7 = new JButton("7");
        panel2.add(b7);

        b8 = new JButton("8");
        panel2.add(b8);

        b9 = new JButton("9");
        panel2.add(b9);

        bSuma = new JButton("+");
        panel2.add(bSuma);

        b4 = new JButton("4");
        panel2.add(b4);

        b5 = new JButton("5");
        panel2.add(b5);

        b6 = new JButton("6");
        panel2.add(b6);

        bResta = new JButton("-");
        panel2.add(bResta);

        b1 = new JButton("1");
        panel2.add(b1);

        b2 = new JButton("2");
        panel2.add(b2);

        b3 = new JButton("3");
        panel2.add(b3);

        bMultiplica = new JButton("x");
        panel2.add(bMultiplica);

        b0 = new JButton("0");
        panel2.add(b0);

        bPunto = new JButton(".");
        panel2.add(bPunto);

        bIgual = new JButton("=");
        panel2.add(bIgual);

        bDivide = new JButton("/");
        panel2.add(bDivide);

        bFactorial = new JButton("x!");
        panel2.add(bFactorial);

        bRaiz = new JButton("sqrt(x)");
        panel2.add(bRaiz);

        bCuadrado = new JButton("x^2");
        panel2.add(bCuadrado);

        bExpo = new JButton("y^x");
        panel2.add(bExpo);


        setLayout(new FlowLayout());
        add(panel1);
        add(panel2);
        setSize(350,220);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource()==bSuma)
        {
            try
            {
                strNumero =tfNumero.getText();
                n1= Float.parseFloat(strNumero);
                operacion="suma";
                tfNumero.setText("");
                strNumero="";
            }
            catch(NumberFormatException nfe)
            {
            }
        }
    }


    public static void main(String args[])
    {
        Calculadora calculadora = new Calculadora();
        calculadora.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
    
asked by Cruzhn 15.02.2018 в 17:14
source

1 answer

2

That's because they do not have a actionListener , the buttons have them but they do not implement this ability to activate when something happens, for example, when pressed.

b1 = new JButton("1");
panel2.add(b1);

You have to add the listener using addActionListener , like this:

b1 = new JButton("1");
boton.addActionListener(AQUI_VA_EL_LISTENER);
panel2.add(b1);

You will ask ... and how do I add it?

Like this:

public class Calculadora extends JFrame

You deploy the ActionListener (interface)

public class Calculadora extends JFrame implements ActionListener

On your button you only put this when adding the actionListener

boton.addActionListener(this);
    
answered by 15.02.2018 / 17:20
source