Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import java. Awt. *;
- import java.awt.event. *;
- public class Calculadora extends Frame
- {
- Button bn[]=new Button[10];
- Button bn2[]=new Button[4];
- Button igual=new Button("=");
- TextField txt1=new TextField();
- int num1=0;
- char ope;
- public Calculadora()
- {
- setLayout(new FlowLayout());
- int i=0;
- for (i=0;i<bn. length="">
- {
- bn[i]=new Button(String. ValueOf(i+1));
- bn[i]. AddActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- escribirNumero(e);
- }
- });
- add(bn[i]);
- }
- for (i=0;i<bn2. length="">
- {
- bn2[i]=new Button();
- switch(i)
- {
- case 0:
- bn2[i]. SetLabel("+");
- break;
- case 1:
- bn2[i]. SetLabel("*");
- break;
- case 2:
- bn2[i]. SetLabel("-");
- break;
- case 3:
- bn2[i]. SetLabel("/");
- break;
- }
- bn2[i]. AddActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- almacenarOperador(e);
- }
- });
- add(bn2[i]);
- }
- igual. AddActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- mostrarResultado(e);
- }
- });
- add(igual);
- add(txt1);
- }
- public void escribirNumero(ActionEvent e)
- {
- Button boton=(Button)(e. GetSource());
- txt1. SetText(txt1. GetText()+boton. GetLabel());
- }
- public void almacenarOperador(ActionEvent e)
- {
- Button boton=(Button)(e. GetSource());
- ope=boton. GetLabel(). CharAt(0);
- num1=Integer. ParseInt(txt1. GetText());
- txt1. SetText("");
- }
- public void mostrarResultado(ActionEvent e)
- {
- int num2=Integer. ParseInt(txt1. GetText());
- int res=0;
- switch (ope)
- {
- case '+':
- res=num1+num2;
- break;
- case '-':
- res=num1-num2;
- break;
- case '*':
- res=num1*num2;
- break;
- case '/':
- res=num1/num2;
- break;
- }
- txt1. SetText(String. ValueOf(res));
- }
- public static void main (String args[])
- {
- Calculadora c=new Calculadora();
- c. SetVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement