Ajuda em calculadora

2 respostas
C

Oi,

Gostaria de saber se alguém pode me ajudar a resolver essa calculadora, está mostrando o valor correto qd é clicado no sinal de igual, porém não consigo mostrar esse mesmo resulta quando é clicado nos sinais e já tem uma conta, por exemplo, 2 + 3 qd clica em + novamente já tem que mostrar o 5, e só mostra com o igual.

Segue o código abaixo.

Obrigada.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class FrmCalculadora extends JFrame implements ActionListener, KeyListener {
	private JButton btnBackspace, btnCE, btnC, btn7, btn8, btn9, btnDivisao, btn4,
			btn5, btn6, btnMultiplicao, btn1, btn2, btn3, btnSubtracao, btn0,
			btnVirgula, btnSoma, btnIgual;
	private JTextField txtResultado = new JTextField(15);
	private String result = "";
	private String sinal;
	private long acumulador, total;
	private int tecla = 0;
	private String teclaPressed = null;

	public FrmCalculadora() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setSize(200, 200);
		setResizable(false);
		setLocationRelativeTo(null);
		setTitle("GridLayout");
		Container P = getContentPane();

		JPanel pnl = new JPanel();
		pnl.setLayout(new GridLayout(1, 3, 4, 4));
		btnBackspace = new JButton("Backspace");
		btnBackspace.setForeground(Color.red);
		btnCE = new JButton("CE");
		btnCE.setForeground(Color.red);
		btnC = new JButton("C");
		btnC.setForeground(Color.red);
		pnl.add(btnBackspace);
		pnl.add(btnCE);
		pnl.add(btnC);

		JPanel pnl2 = new JPanel();
		pnl2.setLayout(new GridLayout(4, 6, 4, 4));
		btn7 = new JButton("7");
		btn7.setForeground(Color.blue);
		pnl2.add(btn7);
		btn8 = new JButton("8");
		btn8.setForeground(Color.blue);
		pnl2.add(btn8);
		btn9 = new JButton("9");
		btn9.setForeground(Color.blue);
		pnl2.add(btn9);
		btnDivisao = new JButton("/");
		btnDivisao.setForeground(Color.red);
		pnl2.add(btnDivisao);
		btn4 = new JButton("4");
		btn4.setForeground(Color.blue);
		pnl2.add(btn4);
		btn5 = new JButton("5");
		btn5.setForeground(Color.blue);
		pnl2.add(btn5);
		btn6 = new JButton("6");
		btn6.setForeground(Color.blue);
		pnl2.add(btn6);
		btnMultiplicao = new JButton("*");
		btnMultiplicao.setForeground(Color.red);
		pnl2.add(btnMultiplicao);
		btn1 = new JButton("1");
		btn1.setForeground(Color.blue);
		pnl2.add(btn1);
		btn2 = new JButton("2");
		btn2.setForeground(Color.blue);
		pnl2.add(btn2);
		btn3 = new JButton("3");
		btn3.setForeground(Color.blue);
		pnl2.add(btn3);
		btnSubtracao = new JButton("-");
		btnSubtracao.setForeground(Color.red);
		pnl2.add(btnSubtracao);
		btn0 = new JButton("0");
		btn0.setForeground(Color.blue);
		pnl2.add(btn0);
		btnVirgula = new JButton(",");
		btnVirgula.setForeground(Color.blue);
		pnl2.add(btnVirgula);
		btnIgual = new JButton("=");
		btnIgual.setForeground(Color.red);
		pnl2.add(btnIgual);
		btnSoma = new JButton("+");
		btnSoma.setForeground(Color.red);
		pnl2.add(btnSoma);

		JPanel pnl1 = new JPanel();
		pnl1.setLayout(new BorderLayout(3, 3));
		txtResultado.setHorizontalAlignment(JTextField.RIGHT);
		txtResultado.setEditable(false);
		pnl1.add(txtResultado, BorderLayout.NORTH);
		pnl1.add(pnl, BorderLayout.CENTER);
		pnl1.add(pnl2, BorderLayout.SOUTH);

		P.add(pnl1);

		txtResultado.addActionListener(this);
		btn1.addActionListener(this);
		btn2.addActionListener(this);
		btn3.addActionListener(this);
		btn4.addActionListener(this);
		btn5.addActionListener(this);
		btn6.addActionListener(this);
		btn7.addActionListener(this);
		btn8.addActionListener(this);
		btn9.addActionListener(this);
		btn0.addActionListener(this);
		btnBackspace.addActionListener(this);
		btnCE.addActionListener(this);
		btnC.addActionListener(this);
		btnDivisao.addActionListener(this);
		btnMultiplicao.addActionListener(this);
		btnSoma.addActionListener(this);
		btnSubtracao.addActionListener(this);
		btnVirgula.addActionListener(this);
		btnIgual.addActionListener(this);
		txtResultado.addKeyListener(this);
	}

	public static void main(String[] args) {
		FrmCalculadora c = new FrmCalculadora();
		c.setVisible(true);
	}

	public void actionPerformed(ActionEvent evt) {
		String textOrigem = evt.getActionCommand();
		if ("123456789".contains(textOrigem))
			result = result + textOrigem;
		if ((textOrigem.equals("0")) && (result.equals("")))
			result = textOrigem + ",";
		else if ((textOrigem.equals("0")) && (result != ""))
			result += textOrigem;
		if ((textOrigem.equals(",")) && (result.equals("")))
			result = "0" + textOrigem;
		else if ((textOrigem.equals(",")) && (result != "")) {
			if (!result.contains(","))
				result += textOrigem;
		}

		if (textOrigem.equals("C"))
			result = "";

		txtResultado.setText(result);

		if (("/*-+").contains(textOrigem)) {
			acumulador = Long.parseLong(result);
			sinal = textOrigem;
			result = "";
		}

		if (textOrigem.equals("=")) {
			long totalCalcular = Calcular(acumulador, sinal, result);
			txtResultado.setText(String.valueOf(totalCalcular));
			result = String.valueOf(totalCalcular);
		}
	}

	public long Calcular(long acumulador, String sinal, String result) {
		if (sinal.equals("+"))
			total = acumulador + Long.parseLong(result);
		if (sinal.equals("-"))
			total = acumulador - Long.parseLong(result);
		if (sinal.equals("/"))
			total = acumulador / Long.parseLong(result);
		if (sinal.equals("*"))
			total = acumulador * Long.parseLong(result);
		return total;
	}

	public void keyPressed(KeyEvent e) {
		tecla = e.getKeyCode();
		teclaPressed = e.getKeyText(tecla);
		txtResultado.setText(teclaPressed);
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}
}

2 Respostas

pimenta

Ao que parece você só faz o cálculo se for clicado “=”… sempre que for clicado um sinal você também faz o cálculo…

Abraços,

C

sim, o meu problema é tratar aquele result… se eu coloco o cálculo do igual nos sinais o result é “” aí não dá, se eu tiro o result ele não apaga o número q tinha antes…

:frowning:

Criado 18 de maio de 2008
Ultima resposta 18 de mai. de 2008
Respostas 2
Participantes 2