Construindo uma calculadora em java com swing

3 respostas
Rachel_da_Silveira_C

Olá Galera,

Estou construindo uma calculadora em java com swing.

Só não estou conseguindo obter o resultado quando quero somar, dividir, etc...

Andei pesquisando no google e também no fórum e acredito que tenho que setar o resultado no o text do JTextField ? Estou perdida!!

Alguma sugestão ?

Obrigada a Todos !!

package Exemplo;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class calc extends JFrame{
	
	
	//JButton botão;

	
	public static void main(String[] args) {
		calc a = new calc();
	}
	
	
	public calc(){
		super("CALCULADORA 2011 ");
		popularTela();
	
	}

	
	public void popularTela(){
	
		Container tela = getContentPane( );
		setLayout(null) ;
	
		final JTextField caixatexto = new JTextField();
		JButton enter = new JButton("ENTER" );
		JButton btclear = new JButton("CLEAR" );
		JButton botao0 = new JButton ("0");
		JButton botao1 = new JButton ("1");
		JButton botao2 = new JButton ("2");
		JButton botao3 = new JButton ("3");
		JButton botao4 = new JButton ("4");
		JButton botao5 = new JButton ("5");
		JButton botao6 = new JButton ("6");
		JButton botao7 = new JButton ("7");
		JButton botao8 = new JButton ("8");
		JButton botao9 = new JButton ("9");
		JButton igual = new JButton("=") ;
		JButton ponto = new JButton(".") ;
		JButton adicao = new JButton("+") ;
		JButton subtracao = new JButton("-") ;
		JButton multiplicacao = new JButton("*") ;
		JButton divisao = new JButton("/") ; //}
		

		caixatexto.setBounds(10,1,273, 30);
		enter.setBounds( 175,31,105, 30);
		botao0.setBounds( 65,131,50, 30);//pos col,pos lin,lar,comp
		
		btclear.addActionListener(new ActionListener(){
			
			public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "CLEAR");
			
			}
		});
		
		enter.addActionListener(new ActionListener(){
			
			public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "ENTER");
			
			}
		});
			
		botao0.addActionListener(new ActionListener(){
		

		   public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "0");
				
				
		   }
		});
											
		botao1.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "1");
				
				
			   }
			});
				
		botao2.addActionListener(new ActionListener() {
				
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "2");
				
				
			   }
			});
		
		botao3.addActionListener(new ActionListener() {
				
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "3");
				
				
			   }
			});
				
		botao4.addActionListener(new ActionListener() {
					
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "4");
				
				
			   }
			});
		
		botao5.addActionListener(new ActionListener() {
					
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "5");
				
				
			   }
			});
		
		botao6.addActionListener(new ActionListener() {
					
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "6");
				
				
			   }
			});
									
		botao7.addActionListener(new ActionListener() {
							
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "7");
				
				
			   }
			});
											
		botao8.addActionListener(new ActionListener() {
					
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "8");
				
				
			   }
			});
				
		botao9.addActionListener(new ActionListener() {
					
			public void actionPerformed(ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "9");
				
				
			   }
			});
		
		igual.addActionListener(new ActionListener(){
			
			public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "=");
				
			}
		});
		
		
		
		ponto.addActionListener(new ActionListener() {
			public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + ".");
			}
		});
		
		
		
		adicao.addActionListener(new ActionListener(){
					
			public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "+");
			
				
			}
		});
	
		subtracao.addActionListener(new ActionListener(){
					
		public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "-");
		
			}
		
		});
	
		
		
		multiplicacao.addActionListener(new ActionListener(){
					
		public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "*");
		
		}
		});

			
		divisao.addActionListener(new ActionListener(){
			
			public void actionPerformed (ActionEvent e) {
				caixatexto.setText(caixatexto.getText() + "/");
			
			}
		});
		
					 
		
		btclear.setBounds( 175,65,105, 30);
		botao1.setBounds( 10,98,50, 30);
		botao2.setBounds( 65,98,50, 30);
		botao3.setBounds( 120,98,50, 30);
		botao4.setBounds( 10,65,50, 30 );
		botao5.setBounds( 65,65,50, 30 );
		botao6.setBounds( 120,65,50, 30 );
		botao7.setBounds( 10,31,50, 30);
		botao8.setBounds( 65,31,50, 30);
		botao9.setBounds( 120,31,50, 30);
		igual.setBounds( 120,131,50, 30);
		ponto.setBounds( 10,131,50, 30);
		adicao.setBounds( 175,98,50, 30);
		subtracao.setBounds (230,98,50, 30);
		multiplicacao. setBounds( 175,131,50, 30);
		divisao.setBounds( 230,131,50, 30);
			
		
		tela.add(caixatexto);
		tela.add(botao0) ;
		tela.add(botao1) ;
		tela.add(botao2) ;
		tela.add(botao3) ;
		tela.add(botao4) ;
		tela.add(botao5) ;
		tela.add(botao6) ;
		tela.add(botao7) ;
		tela.add(botao8) ;
		tela.add(botao9) ;
		tela.add(btclear) ;
		tela.add(enter) ;
		tela.add(igual) ;
		tela.add(ponto) ;
		tela.add(adicao) ;
		tela.add(subtracao) ;
		tela.add(multiplicacao);
		tela.add(divisao) ;
		tela.add(enter);
	
		
		setSize( 300, 200 );
		setVisible( true );
		
			}
			
		}

3 Respostas

L

Rachel, postei inteiro pra vc dar uma olhada

public class calculadora extends JFrame
  implements ActionListener
{
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
JButton espaco = new JButton("");
  JButton espaco2 = new JButton("");
  JButton espaco3 = new JButton("");
  JButton mais = new JButton("+");
  JButton menos = new JButton("-");
  JButton vezes = new JButton("*");
  JButton dividir = new JButton("/");
  JButton zero = new JButton("0");
  JButton um = new JButton("1");
  JButton dois = new JButton("2");
  JButton tres = new JButton("3");
  JButton quatro = new JButton("4");
  JButton cinco = new JButton("5");
  JButton seis = new JButton("6");
  JButton sete = new JButton("7");
  JButton oito = new JButton("8");
  JButton nove = new JButton("9");
  JButton igual = new JButton("=");
  JButton CE = new JButton("CE");
  JButton ponto = new JButton(".");

  Scanner numeros = new Scanner(System.in);
  String sinal;
  int num1;
  int num2;
  int result;
  JTextField mostrador;
  JPanel calc;

  public calculadora()
  {
    setTitle("calculadora java");
    setResizable(false);
    setBackground(new Color(255, 245, 180));
    setDefaultCloseOperation(2);
    setSize(390, 550);

    setLocationRelativeTo(null);

    this.espaco = new JButton("");
    this.espaco2 = new JButton("");
    this.espaco3 = new JButton("");
    this.mais = new JButton("+");
    this.menos = new JButton("-");
    this.vezes = new JButton("*");
    this.dividir = new JButton("/");
    this.zero = new JButton("0");
    this.um = new JButton("1");
    this.dois = new JButton("2");
    this.tres = new JButton("3");
    this.quatro = new JButton("4");
    this.cinco = new JButton("5");
    this.seis = new JButton("6");
    this.sete = new JButton("7");
    this.oito = new JButton("8");
    this.nove = new JButton("9");
    this.igual = new JButton("=");
    this.ponto = new JButton(".");

    this.mais.setFont(new Font("Sans Serif", 1, 44));
    this.menos.setFont(new Font("Sans Serif", 1, 44));
    this.vezes.setFont(new Font("Sans Serif", 1, 44));
    this.dividir.setFont(new Font("Sans Serif", 1, 44));
    this.zero.setFont(new Font("Sans Serif", 1, 44));
    this.um.setFont(new Font("Sans Serif", 1, 44));
    this.dois.setFont(new Font("Sans Serif", 1, 44));
    this.tres.setFont(new Font("Sans Serif", 1, 44));
    this.quatro.setFont(new Font("Sans Serif", 1, 44));
    this.cinco.setFont(new Font("Sans Serif", 1, 44));
    this.seis.setFont(new Font("Sans Serif", 1, 44));
    this.sete.setFont(new Font("Sans Serif", 1, 44));
    this.oito.setFont(new Font("Sans Serif", 1, 44));
    this.nove.setFont(new Font("Sans Serif", 1, 44));
    this.igual.setFont(new Font("Sans Serif", 1, 44));
    this.ponto.setFont(new Font("Sans Serif", 1, 44));

    this.calc = new JPanel();
    this.calc.setLayout(null);
    this.mostrador = new JTextField("");
    this.mostrador.setSize(260, 80);
    this.mostrador.setLocation(10, 10);
    this.mostrador.setFont(new Font("Sans Serif", 1, 44));

    this.CE = new JButton("CE");
    this.CE.setSize(80, 80);
    this.CE.setLocation(280, 10);
    this.CE.setBackground(Color.red);
    this.CE.setFont(new Font("Sans Serif", 1, 30));

    this.sete.setSize(80, 80);
    this.sete.setLocation(10, 110);

    this.oito.setSize(80, 80);
    this.oito.setLocation(100, 110);

    this.nove.setSize(80, 80);
    this.nove.setLocation(190, 110);

    this.dividir.setSize(80, 80);
    this.dividir.setLocation(280, 110);

    this.quatro.setSize(80, 80);
    this.quatro.setLocation(10, 210);
    this.cinco.setSize(80, 80);
    this.cinco.setLocation(100, 210);
    this.seis.setSize(80, 80);
    this.seis.setLocation(190, 210);
    this.vezes.setSize(80, 80);
    this.vezes.setLocation(280, 210);

    this.um.setSize(80, 80);
    this.um.setLocation(10, 310);
    this.dois.setSize(80, 80);
    this.dois.setLocation(100, 310);
    this.tres.setSize(80, 80);
    this.tres.setLocation(190, 310);
    this.menos.setSize(80, 80);
    this.menos.setLocation(280, 310);

    this.zero.setSize(80, 80);
    this.zero.setLocation(10, 410);
    this.ponto.setSize(80, 80);
    this.ponto.setLocation(100, 410);
    this.igual.setSize(80, 80);
    this.igual.setLocation(190, 410);
    this.mais.setSize(80, 80);
    this.mais.setLocation(280, 410);

    this.ponto.setEnabled(false);

    getContentPane().add(this.mostrador);
    this.calc.add(this.um);
    this.calc.add(this.dois);
    this.calc.add(this.tres);
    this.calc.add(this.quatro);
    this.calc.add(this.cinco);
    this.calc.add(this.seis);
    this.calc.add(this.sete);
    this.calc.add(this.oito);
    this.calc.add(this.nove);
    this.calc.add(this.zero);
    getContentPane().add(this.CE);
    this.calc.add(this.mais);

    this.calc.add(this.menos);
    this.calc.add(this.vezes);
    this.calc.add(this.dividir);
    this.calc.add(this.igual);
    this.calc.add(this.ponto);

    this.calc.add(this.espaco);
    this.calc.add(this.espaco2);
    this.calc.add(this.espaco3);

    add(this.calc);
    this.CE.addActionListener(this);
    this.mais.addActionListener(this);
    this.menos.addActionListener(this);
    this.vezes.addActionListener(this);
    this.dividir.addActionListener(this);
    this.igual.addActionListener(this);
    this.zero.addActionListener(this);
    this.um.addActionListener(this);
    this.dois.addActionListener(this);
    this.tres.addActionListener(this);
    this.quatro.addActionListener(this);
    this.cinco.addActionListener(this);
    this.seis.addActionListener(this);
    this.sete.addActionListener(this);
    this.oito.addActionListener(this);
    this.nove.addActionListener(this);

    setBackground(new Color(255, 245, 180));
  }

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == this.CE) {
      CE();
    }

    if (e.getSource() == this.mais) {
      operMais();
    }
    if (e.getSource() == this.menos) {
      operMenos();
    }
    if (e.getSource() == this.dividir) {
      operDiv();
    }
    if (e.getSource() == this.vezes) {
      operVezes();
    }
    if (e.getSource() == this.zero)
    {
      String apice = this.mostrador.getText();
      String vaiLa = apice + "0";
      this.mostrador.setText(vaiLa);
    }

    if (e.getSource() == this.um) {
      String apice2 = this.mostrador.getText();
      String vaiLa2 = apice2 + "1";
      this.mostrador.setText(vaiLa2);
    }
    if (e.getSource() == this.dois) {
      String apice3 = this.mostrador.getText();
      String vaiLa3 = apice3 + "2";
      this.mostrador.setText(vaiLa3);
    }
    if (e.getSource() == this.tres) {
      String apice4 = this.mostrador.getText();
      String vaiLa4 = apice4 + "3";
      this.mostrador.setText(vaiLa4);
    }
    if (e.getSource() == this.quatro) {
      String apice5 = this.mostrador.getText();
      String vaiLa5 = apice5 + "4";
      this.mostrador.setText(vaiLa5);
    }
    if (e.getSource() == this.cinco) {
      String apice6 = this.mostrador.getText();
      String vaiLa6 = apice6 + "5";
      this.mostrador.setText(vaiLa6);
    }
    if (e.getSource() == this.seis) {
      String apice7 = this.mostrador.getText();
      String vaiLa7 = apice7 + "6";
      this.mostrador.setText(vaiLa7);
    }

    if (e.getSource() == this.sete) {
      String apice8 = this.mostrador.getText();
      String vaiLa8 = apice8 + "7";
      this.mostrador.setText(vaiLa8);
    }
    if (e.getSource() == this.oito) {
      String apice9 = this.mostrador.getText();
      String vaiLa9 = apice9 + "8";
      this.mostrador.setText(vaiLa9);
    }
    if (e.getSource() == this.nove) {
      String apice10 = this.mostrador.getText();
      String vaiLa10 = apice10 + "9";
      this.mostrador.setText(vaiLa10);
    }

    if (e.getSource() == this.igual)
      igual();
  }

  public void um()
  {
    this.mostrador.setText("1");
  }

  public void igual()
  {
    if (this.sinal.equals("+"))
    {
      this.num2 = Integer.parseInt(this.mostrador.getText());
      this.result = (this.num1 + this.num2);

      this.mostrador.setText("" + this.result);
      this.num1 = 0;
      this.num2 = 0;
    }

    if (this.sinal.equals("-")) {
      this.num2 = Integer.parseInt(this.mostrador.getText());
      this.result = (this.num1 - this.num2);
      this.mostrador.setText("" + this.result);
      this.num1 = 0;
      this.num2 = 0;
    }

    if (this.sinal.equals("*")) {
      this.num2 = Integer.parseInt(this.mostrador.getText());
      this.result = (this.num1 * this.num2);
      this.mostrador.setText("" + this.result);
      this.num1 = 0;
      this.num2 = 0;
    }

    if (this.sinal.equals("/")) {
      this.num2 = Integer.parseInt(this.mostrador.getText());
      this.result = (this.num1 / this.num2);
      this.mostrador.setText("" + this.result);
      this.num1 = 0;
      this.num2 = 0;
    }
  }

  public void operMais()
  {
    this.num1 = Integer.parseInt(this.mostrador.getText());
    this.sinal = "+";
    this.mostrador.setText("");
  }

  public void operMenos()
  {
    this.num1 = Integer.parseInt(this.mostrador.getText());
    this.sinal = "-";
    this.mostrador.setText("");
  }

  public void operVezes()
  {
    this.num1 = Integer.parseInt(this.mostrador.getText());
    this.sinal = "*";
    this.mostrador.setText("");
  }

  public void operDiv()
  {
    this.num1 = Integer.parseInt(this.mostrador.getText());
    this.sinal = "/";
    this.mostrador.setText("");
  }

  public void CE()
  {
    this.mostrador.setText("");
  }

  public static void main(String[] args)
  {
    new calculadora().setVisible(true);
  }
}
Rachel_da_Silveira_C

Testei o seu código, gostei .

Vou comparar com o meu , dar continuidade e melhorias.

aceito críticas, é bem vindo.

Depois que terminar, posto aqui para você ver como é que ficou !! ok ?

L

“Rachel da Silveira Campos”

ainda bem que gostou dela, simples !

ok irei aguardar, caso queira mandar por mp tbm sem problema…

Criado 27 de outubro de 2011
Ultima resposta 27 de out. de 2011
Respostas 3
Participantes 2