Aqui les dejo el código completo
Código:
Saludos,import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Calc extends Applet implements ActionListener { private Button cero, uno, dos, tres, cuatro, cinco, seis, siete, ocho, nueve; private Button porc, mult, sum, resta, div, mc, mr, mmas, ce, punto, igual, signo; private TextField entrada; private double men, Ope1, Ope2, Total; private char Ope; private Panel board; private boolean OpePresionado; public void init(){ men = 0; OpePresionado = false; uno = new Button("1"); dos = new Button("2"); tres = new Button("3"); cuatro = new Button("4"); cinco = new Button("5"); seis = new Button("6"); siete = new Button("7"); ocho = new Button("8"); nueve = new Button("9"); cero = new Button("0"); porc = new Button("%"); mult = new Button("*"); sum = new Button("+"); resta = new Button("-"); div = new Button("/"); mc = new Button("MC"); mr = new Button("MR"); mmas = new Button("M+"); ce = new Button("CE"); punto = new Button("."); igual = new Button("="); signo = new Button("+/-"); entrada = new TextField(20); setLayout(new BorderLayout()); add("North",entrada); board = new Panel(); board.setLayout(new GridLayout(5,5,6,6)); porc.addActionListener(this); board.add(porc); siete.addActionListener(this); board.add(siete); ocho.addActionListener(this); board.add(ocho); nueve.addActionListener(this); board.add(nueve); signo.addActionListener(this); board.add(signo); mc.addActionListener(this); board.add(mc); cuatro.addActionListener(this); board.add(cuatro); cinco.addActionListener(this); board.add(cinco); seis.addActionListener(this); board.add(seis); div.addActionListener(this); board.add(div); mr.addActionListener(this); board.add(mr); uno.addActionListener(this); board.add(uno); dos.addActionListener(this); board.add(dos); tres.addActionListener(this); board.add(tres); mult.addActionListener(this); board.add(mult); mmas.addActionListener(this); board.add(mmas); cero.addActionListener(this); board.add(cero); punto.addActionListener(this); board.add(punto); igual.addActionListener(this); board.add(igual); resta.addActionListener(this); board.add(resta); ce.addActionListener(this); board.add(ce); sum.addActionListener(this); board.add(sum); add("Center",board); resize(200,200); } public void actionPerformed(ActionEvent ev) { try{ Object obj = ev.getSource(); if(obj==uno || obj==dos || obj==tres || obj==cuatro || obj==cinco || obj==seis || obj==siete || obj==ocho || obj==nueve || obj==cero || obj==punto){ if (OpePresionado == false) entrada.setText(entrada.getText()+ev.getActionCommand()); else{ entrada.setText(""); entrada.setText(entrada.getText()+ev.getActionCommand()); OpePresionado = false; } } else{ if (obj == ce) entrada.setText(""); else if (obj == mc) men = 0.0; else if (obj == mr) entrada.setText(Double.toString(men)); else if (obj == mmas) men += Double.parseDouble(entrada.getText()); else if (obj == porc) {} //Poner Codigo else if (obj == sum){ OpePresionado = true; Ope1 = Double.parseDouble(entrada.getText()); Ope = '+'; } else if (obj == resta){ OpePresionado = true; Ope1 = Double.parseDouble(entrada.getText()); Ope = '-'; } else if (obj == mult){ OpePresionado = true; Ope1 = Double.parseDouble(entrada.getText()); Ope = '*'; } else if (obj == div){ OpePresionado = true; Ope1 = Double.parseDouble(entrada.getText()); Ope = '/'; } else if (obj == igual){ Ope2 = Double.parseDouble(entrada.getText()); OpePresionado = true; switch(Ope){ case '+': entrada.setText(Double.toString(Ope1+Ope2)); Ope1 = 0.0; Ope2 = 0.0; Ope = ' '; break; case '-': entrada.setText(Double.toString(Ope1-Ope2)); Ope1 = 0.0; Ope2 = 0.0; Ope = ' '; break; case '*': entrada.setText(Double.toString(Ope1*Ope2)); Ope1 = 0.0; Ope2 = 0.0; Ope = ' '; break; case '/': entrada.setText(Double.toString(Ope1/Ope2)); Ope1 = 0.0; Ope2 = 0.0; Ope = ' '; break; } } else if (obj == signo) entrada.setText(Double.toString(Double.parseDouble(entrada.getText())*(-1))); else if (obj == porc) entrada.setText(Double.toString(Double.parseDouble(entrada.getText())/100)); } } catch(Exception e){ entrada.setText("Error..."); } } }
Joe Peña