Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/08/2013, 06:16
Avatar de cufu8583
cufu8583
 
Fecha de Ingreso: enero-2013
Ubicación: North Miami Beach
Mensajes: 24
Antigüedad: 11 años, 3 meses
Puntos: 1
Problema con Calculadora en java y Swing

Como estan? Queria saber si me ayuda con este problema que tengo en java. Estoy haciendo una calculadora en java usando Swing y AWT pero el problema es que cuando presiono un numero y depues el signo de sumar, restar, multiplicar o dividir desaparece el primer numero presionado.
Pro ejemplo:
si quiero sumar 5 + 7 entonces al presionar el signo + o cualquier otro signo para las operaciones desaparece el numero 5. Las operaciones las hace bien pero eso de que desaparesca el numero cuando se presiona el signo no me gusta y no he podido encontrar el problema.


Clase principal:
Código Java:
Ver original
  1. package calculadora;
  2. import javax.swing.*;
  3. class Calculadora{
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         SubCalculadora ventana = new SubCalculadora();
  8.         ventana.setBounds(10, 10, 280, 318);
  9.         ventana.setVisible(true);
  10.         ventana.setResizable(false);
  11.         ventana.setLocationRelativeTo(null);
  12.         ventana.setTitle("Calculadora");
  13.         ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.     }
  15. }

subClase o donde estan los codigos de los botones y todo:
Código Java:
Ver original
  1. package calculadora;
  2. import java.awt.Color;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. public class SubCalculadora extends JFrame implements ActionListener {
  7.     private  JTextField campoTexto;
  8.     private JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,c,punto,div,sum,rest,mult,igual,retroceso ;
  9.     JButton salir;
  10.     String memoria1;
  11.     String signo;
  12.     String memoria2;
  13.    
  14.     public SubCalculadora(){
  15.         setLayout(null);
  16.        
  17.         campoTexto = new JTextField();
  18.         campoTexto.setBounds(10, 10, 255, 40);
  19.         add(campoTexto);
  20.  
  21.         b7 = new JButton("7");
  22.         b7.setBounds(10, 53, 50, 50);
  23.         add(b7);
  24.         b7.addActionListener(this);
  25.        
  26.         b8 = new JButton("8");
  27.         b8.setBounds(61, 53, 50, 50);
  28.         add(b8);
  29.         b8.addActionListener(this);
  30.        
  31.         b9 = new JButton("9");
  32.         b9.setBounds(112, 53, 50, 50);
  33.         add(b9);
  34.         b9.addActionListener(this);
  35.        
  36.         div = new JButton("/");
  37.         div.setBounds(163, 53, 50, 50);
  38.         add(div);
  39.         div.addActionListener(this);
  40.        
  41.         c = new JButton("CE");
  42.         c.setBounds(214, 53, 50, 50);
  43.         add(c);
  44.         c.addActionListener(this);
  45.        
  46.         b4 = new JButton("4");
  47.         b4.setBounds(10, 104, 50, 50);
  48.         add(b4);
  49.         b4.addActionListener(this);
  50.        
  51.         b5 = new JButton("5");
  52.         b5.setBounds(61, 104, 50, 50);
  53.         add(b5);
  54.         b5.addActionListener(this);
  55.        
  56.         b6 = new JButton("6");
  57.         b6.setBounds(112, 104, 50, 50);
  58.         add(b6);
  59.         b6.addActionListener(this);
  60.        
  61.         mult = new JButton("*");
  62.         mult.setBounds(163, 104, 50, 50);
  63.         add(mult);
  64.         mult.addActionListener(this);
  65.        
  66.         retroceso = new JButton("C");
  67.         retroceso.setBounds(214, 104, 50, 50);
  68.         add(retroceso);
  69.         retroceso.addActionListener(this);
  70.        
  71.         b1 = new JButton("1");
  72.         b1.setBounds(10, 155, 50, 50);
  73.         add(b1);
  74.         b1.addActionListener(this);
  75.        
  76.         b2 = new JButton("2");
  77.         b2.setBounds(61, 155, 50, 50);
  78.         add(b2);
  79.         b2.addActionListener(this);
  80.        
  81.         b3 = new JButton("3");
  82.         b3.setBounds(112, 155, 50, 50);
  83.         add(b3);
  84.         b3.addActionListener(this);
  85.        
  86.         rest = new JButton("-");
  87.         rest.setBounds(163, 155, 50, 50);
  88.         add(rest);
  89.         rest.addActionListener(this);
  90.        
  91.         b0 = new JButton("0");
  92.         b0.setBounds(10, 206, 50, 50);
  93.         add(b0);
  94.         b0.addActionListener(this);
  95.        
  96.         punto = new JButton(".");
  97.         punto.setBounds(61, 206, 50, 50);
  98.         add(punto);
  99.         punto.addActionListener(this);
  100.        
  101.         igual = new JButton("=");
  102.         igual.setBounds(112, 206, 50, 50);
  103.         add(igual);
  104.         igual.addActionListener(this);
  105.        
  106.         sum = new JButton("+");
  107.         sum.setBounds(163, 206, 50, 50);
  108.         add(sum);
  109.         sum.addActionListener(this);
  110.        
  111.         // Codigo boton Salir.
  112.         salir = new JButton("Salir");
  113.         salir.setBounds(61, 257, 100, 30);
  114.         add(salir);
  115.         salir.addActionListener(this);
  116.     }
  117.    
  118.     public void actionPerformed(ActionEvent e){
  119.        
  120.         if(e.getSource() == salir){
  121.             System.exit(0);
  122.             }
  123.      
  124.         if(e.getSource() == c){
  125.             campoTexto.setText("");
  126.         }
  127.        
  128.        
  129.         if(e.getSource() == b1){
  130.             campoTexto.setText(campoTexto.getText() + "1");
  131.         }
  132.        
  133.         if(e.getSource() == b2){
  134.             campoTexto.setText(campoTexto.getText() + "2");
  135.         }
  136.         // Concatenacion boton3
  137.         if(e.getSource() == b3){
  138.             campoTexto.setText(campoTexto.getText() + "3");
  139.         }
  140.         // Concatenacion boton4
  141.         if(e.getSource() == b4){
  142.             campoTexto.setText(campoTexto.getText() + "4");
  143.         }
  144.         // Concatenacion boton5
  145.         if(e.getSource() == b5){
  146.             campoTexto.setText(campoTexto.getText() + "5");
  147.         }
  148.         // Concatenacion boton6
  149.         if(e.getSource() == b6){
  150.             campoTexto.setText(campoTexto.getText() + "6");
  151.         }
  152.        
  153.         if(e.getSource() == b7){
  154.             campoTexto.setText(campoTexto.getText() + "7");
  155.         }
  156.        
  157.         if(e.getSource() == b8){
  158.             campoTexto.setText(campoTexto.getText() + "8");
  159.         }
  160.        
  161.         if(e.getSource() == b9){
  162.             campoTexto.setText(campoTexto.getText() + "9");
  163.         }
  164.        
  165.         if(e.getSource() == b0){
  166.             campoTexto.setText(campoTexto.getText() + "0");
  167.         }
  168.        
  169.        
  170.             String cadena1 = campoTexto.getText();
  171.             if(cadena1.length()>0){
  172.                 cadena1 = cadena1.substring(0, cadena1.length()-1);
  173.                 campoTexto.setText(cadena1);
  174.             }
  175.         }
  176.        
  177.        
  178.         if(e.getSource() == punto){
  179.             String cadena = campoTexto.getText();
  180.             if(cadena.length() <= 0){
  181.                 campoTexto.setText("0.");
  182.             }else{
  183.                 if(!existePunto(campoTexto.getText())){
  184.                     campoTexto.setText(campoTexto.getText() + ".");
  185.                 }
  186.             }
  187.         }// Fin del if de punto.
  188.        
  189.         // Boton de sumar(+)
  190.         if(e.getSource() == sum){
  191.             if(!campoTexto.getText().equals(""))// Comparamos que la pantalla de la calculadora no este en blanco.
  192.             {
  193.                 memoria1 = campoTexto.getText();
  194.                 signo = "+";
  195.                 campoTexto.setText("");                
  196.             }
  197.         }//Fin del if para Suma.
  198.        
  199.         // Boton de RESTAR(-)
  200.         if(e.getSource() == rest){
  201.             if(!campoTexto.getText().equals(""))// Comparamos que la pantalla de la calculadora no este en blanco.
  202.             {
  203.                 memoria1 = campoTexto.getText();
  204.                 signo = "-";
  205.                 campoTexto.setText("");                
  206.             }
  207.         }//Fin del if para resta
  208.        
  209.         // Boton de multiplicacion(*)
  210.         if(e.getSource() == mult){
  211.             if(!campoTexto.getText().equals(""))// Comparamos que la pantalla de la calculadora no este en blanco.
  212.             {
  213.                 memoria1 = campoTexto.getText();
  214.                 signo = "*";
  215.                 campoTexto.setText("");                
  216.             }
  217.         }//Fin del if para multiplicacion
  218.        
  219.         // Boton de Division(/)
  220.         if(e.getSource() == div){
  221.             if(!campoTexto.getText().equals(""))// Comparamos que la pantalla de la calculadora no este en blanco.
  222.             {
  223.                 memoria1 = campoTexto.getText();
  224.                 signo = "/";
  225.                 campoTexto.setText("");                
  226.             }
  227.         }
  228.        
  229.         // Boton de igual
  230.         if(e.getSource() == igual){
  231.             String resultado;
  232.             memoria2 = campoTexto.getText();
  233.             if(!memoria2.equals("")){
  234.                 resultado = calculadora(memoria1, memoria2, signo);
  235.                 campoTexto.setText(resultado);
  236.             }
  237.         }
  238.     }
  239.    
  240.     // Codigo para el metodo calculadora que usamos en el boton igual.
  241.     public static String calculadora(String memoria1, String memoria2, String signo){
  242.         Double resultado =0.0;
  243.         String respuesta;
  244.        
  245.         // SUMAR
  246.         if(signo.equals("+")){
  247.             resultado = Double.parseDouble(memoria1) + Double.parseDouble(memoria2);
  248.         }
  249.         // RESTAR
  250.         if(signo.equals("-")){
  251.             resultado = Double.parseDouble(memoria1) - Double.parseDouble(memoria2);
  252.         }
  253.         // MULTIPLICAR
  254.         if(signo.equals("*")){
  255.             resultado = Double.parseDouble(memoria1) * Double.parseDouble(memoria2);
  256.         }
  257.         // DIVIDIR
  258.         if(signo.equals("/")){
  259.             resultado = Double.parseDouble(memoria1) / Double.parseDouble(memoria2);
  260.         }
  261.         respuesta = resultado.toString();
  262.         return respuesta;
  263.     }
  264.    
  265.     // Codigo para el metodo existePunto.
  266.     public static boolean existePunto(String cadena){
  267.         boolean resultado = false;
  268.         for (int i = 0; i < cadena.length(); i++) {
  269.             if(cadena.substring(i, i+1).equals(".")){
  270.                 resultado = true;
  271.                 break;
  272.             }
  273.         }
  274.        return resultado;
  275.     }
  276.    
  277. }