微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java Calculator:算术运算符没有出现在屏幕上

如何解决Java Calculator:算术运算符没有出现在屏幕上

我用 Java 制作了一个计算器,但我想在屏幕上显示算术运算符。我不知道为什么这不会发生,我认为是因为方法计算立即覆盖,我不知道。为了在屏幕上进行重置,我使用了一个名为 reset 的布尔变量。

我做了注释以澄清代码错误可能出在 Operadores 类或 Calcular 方法中。

PD:我在 Java 方面相对较新,所以对我的代码的任何建议都很受欢迎。

你好:)

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;
        }
        
    }
    
}

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

解决方法

pantalla.setText(""+ resultado); 中移除 calcular(double x) 并将其移动到以下位置,条件如下:

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 

这只会在按下“=”时打印计算值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。