Advertisement
everblut

Untitled

Aug 20th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. Import java. Awt. *;
  2. import java.awt.event. *;
  3. public class Calculadora extends Frame
  4. {
  5. Button bn[]=new Button[10];
  6. Button bn2[]=new Button[4];
  7. Button igual=new Button("=");
  8. TextField txt1=new TextField();
  9. int num1=0;
  10. char ope;
  11. public Calculadora()
  12. {
  13. setLayout(new FlowLayout());
  14. int i=0;
  15. for (i=0;i<bn. length="">
  16. {
  17. bn[i]=new Button(String. ValueOf(i+1));
  18. bn[i]. AddActionListener(new ActionListener()
  19. {
  20. public void actionPerformed(ActionEvent e)
  21. {
  22. escribirNumero(e);
  23. }
  24. });
  25. add(bn[i]);
  26. }
  27. for (i=0;i<bn2. length="">
  28. {
  29. bn2[i]=new Button();
  30. switch(i)
  31. {
  32. case 0:
  33. bn2[i]. SetLabel("+");
  34. break;
  35. case 1:
  36. bn2[i]. SetLabel("*");
  37. break;
  38. case 2:
  39. bn2[i]. SetLabel("-");
  40. break;
  41. case 3:
  42. bn2[i]. SetLabel("/");
  43. break;
  44. }
  45. bn2[i]. AddActionListener(new ActionListener()
  46. {
  47. public void actionPerformed(ActionEvent e)
  48. {
  49. almacenarOperador(e);
  50. }
  51. });
  52. add(bn2[i]);
  53. }
  54. igual. AddActionListener(new ActionListener()
  55. {
  56. public void actionPerformed(ActionEvent e)
  57. {
  58. mostrarResultado(e);
  59. }
  60. });
  61. add(igual);
  62. add(txt1);
  63. }
  64. public void escribirNumero(ActionEvent e)
  65. {
  66. Button boton=(Button)(e. GetSource());
  67. txt1. SetText(txt1. GetText()+boton. GetLabel());
  68. }
  69. public void almacenarOperador(ActionEvent e)
  70. {
  71. Button boton=(Button)(e. GetSource());
  72. ope=boton. GetLabel(). CharAt(0);
  73. num1=Integer. ParseInt(txt1. GetText());
  74. txt1. SetText("");
  75. }
  76. public void mostrarResultado(ActionEvent e)
  77. {
  78. int num2=Integer. ParseInt(txt1. GetText());
  79. int res=0;
  80. switch (ope)
  81. {
  82. case '+':
  83. res=num1+num2;
  84. break;
  85. case '-':
  86. res=num1-num2;
  87. break;
  88. case '*':
  89. res=num1*num2;
  90. break;
  91. case '/':
  92. res=num1/num2;
  93. break;
  94. }
  95. txt1. SetText(String. ValueOf(res));
  96. }
  97. public static void main (String args[])
  98. {
  99. Calculadora c=new Calculadora();
  100. c. SetVisible(true);
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement