简体   繁体   中英

Java Calculator: Arithmetic operators do not appear on the screen

I made a calculator in Java but I want to show the arithmetic operators in the screen. I dont know why this dont happen, I think it is because the method calcular overwrite immediatly, I dont know. To make the reset in screen I used a boolean variable called reset.

I made comments to clarify the code. Maybe the error is in the class Operadores or in the method Calcular.

PD: Im relatively new in Java, so any suggestion to my code is well received.

Greetings:)

package reto1;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

//--------------------------------------------------------------------------------------------------------------------------------------------------------------//
public class Reto1 {

    public static void main(String[] args) {
        
        MarcoCalculadora micalculadora = new MarcoCalculadora();
        micalculadora.setVisible(true);
        micalculadora.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

               
    }
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------//
class MarcoCalculadora extends JFrame{
    
    //Establecemos el acomodo del marco de la calculadora
    BorderLayout acomodoMarco = new BorderLayout();
    
    //Hacemos el constructor para dar caracteristicas al marco
    public MarcoCalculadora(){
        setTitle("CALCULADORA");                                                      //Here I set a Title
        setLayout(acomodoMarco);                                                       //Here I set the layout
        setBounds(500,300, 380, 400);                                               //I set the bounds 
        LaminasCalculadoras milamina = new LaminasCalculadoras();    
        add(milamina);                                                                        //Adding 
    }
    
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------//
class LaminasCalculadoras extends JPanel{
    
    
    JTextField pantalla = new JTextField("");                   //Creating the object pantalla (screen)
    JPanel milamina2 = new JPanel();                                   //Creating the panel for the buttons
    private boolean reseteo = true;
    private double resultado;
    private String ultimaOperacion;
    private String h;
    
    public LaminasCalculadoras(){
        //////////////////////////////////Screen////////////////////////////////////////////////////////
        setLayout(new BorderLayout(20,20));                          //Layout of the screen
        pantalla.setEnabled(false);                                          //Setting enabled the screen to avoid write in it 
        pantalla.setPreferredSize(new Dimension(400,50));    //Size of the screen
        add(pantalla, BorderLayout.NORTH);                          //Adding the screen
        
        
        //////////////////////////////////Buttons////////////////////////////////////////////////////////         
        milamina2.setLayout(new GridLayout(4,4,20,20));       //Matrix of buttons
        add(milamina2, BorderLayout.CENTER);                     //Adding the buttons in the center of the panel that has the screen in the north
        ActionListener a = new Numeros();                   //Here I put the Actionlistener in the buttons
        ActionListener b = new Operadores();
        ActionListener c = new Borrado();
        ponerBoton("7", a);
        ponerBoton("8", a);
        ponerBoton("9", a);
        ponerBoton("/", b);
        ponerBoton("4", a);
        ponerBoton("5", a);
        ponerBoton("6", a);                                                    //Here I create the buttons with the method ponerBoton
        ponerBoton("*", b);
        ponerBoton("1", a);
        ponerBoton("2", a);
        ponerBoton("3", a);
        ponerBoton("-", b);
        ponerBoton("C", c);
        ponerBoton("0", a);
        ponerBoton("=",b);
        ponerBoton("+", b);
        ultimaOperacion="=";
    }
    
    
    private void ponerBoton(String numero, ActionListener oyente){      //Method to create the Buttons and activate the action listener
        JButton boton = new JButton(numero);
        boton.addActionListener(oyente);
        milamina2.add(boton);
    }
    
    //##################### Numbers ##############################//
    private class Numeros implements ActionListener{                       //Class to write the numbers in the screen

        @Override
        public void actionPerformed(ActionEvent e) {                         //Method
            String entrada=e.getActionCommand();                               //Getting the text of every button clicked
            if(reseteo==true){                                                              //Here I reset the screen after of click an arithemetic operator
                pantalla.setText(" ");
                reseteo=false; 
            }
                   
           pantalla.setText(pantalla.getText()+entrada);                     //Writting the text in screen
        }
        
    }
    
    //##################### Operators ##############################//
    private class Operadores implements ActionListener{                //Class to write the operators in the screen
                                                                                                   //In this class I call the method calcular to do the calculations
        @Override
        public void actionPerformed(ActionEvent e) {                      //Method to write the operators in screen
           String operador=e.getActionCommand();
           if(reseteo==false){                                                          //Here I want to reset the screen after click an operator
               h=pantalla.getText();                                                   //Here I get what it is in screen
               pantalla.setText(" ");
               reseteo=true;
           }
           

               
          pantalla.setText(operador);                                           //Here I want to write the operator

         calcular(Double.parseDouble(h));                                    //Here I call the method to do the calculations
         ultimaOperacion = operador;                                         //This variable tells what operation to do 
           
           
           
        }
        
        public void calcular(double x){                                         //Method to do the calculations
            switch (ultimaOperacion) {
                case "+":
                    resultado+=x;
                    break;
                case "-":
                    resultado-=x;
                    break;
                case "*":
                    resultado*=x;
                    break;
                case "/":
                    resultado/=x;
                    break;
                case "=":
                    resultado=x;
                    break;
                default:
                    break;
            }
            
            pantalla.setText(""+ resultado);

        }
        
    }
    
    //##################### Erase ##############################//
    private class Borrado implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            pantalla.setText(" ");
            reseteo = true;
        }
        
    }
    
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------//

Remove pantalla.setText(""+ resultado); from calcular(double x) and move it to the following place with the following condition:

calcular(Double.parseDouble(h));    //Here I call the method to do the calculations

if ("=".equals(operador)) {
    pantalla.setText(""+ resultado);
}

ultimaOperacion = operador;        //This variable tells what operation to do 

This will only print the calculated value when "=" is being pressed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM