Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JTextField;
  8.  
  9. public class Calculator
  10. extends JFrame
  11. implements ActionListener
  12. {
  13. private JLabel label1;
  14. private JLabel label2;
  15. private JLabel label3;
  16. private JLabel label4;
  17. private JTextField textfield1;
  18. private JTextField textfield2;
  19. private JTextField textfield3;
  20. private JButton boton1;
  21. private JButton boton2;
  22. private JButton boton3;
  23. private JButton boton4;
  24. private JButton boton5;
  25. private JButton boton6;
  26.  
  27. public Calculator()
  28. {
  29. setLayout(null);
  30. this.label1 = new JLabel("Calculatorul lui Man :)");
  31. this.label1.setBounds(80, 20, 200, 20);
  32. add(this.label1);
  33.  
  34.  
  35. this.label2 = new JLabel("X=");
  36. this.label2.setBounds(10, 60, 50, 20);
  37. add(this.label2);
  38.  
  39. this.label3 = new JLabel("Y=");
  40. this.label3.setBounds(150, 60, 50, 20);
  41. add(this.label3);
  42.  
  43. this.label4 = new JLabel("Rezultat ==> ");
  44. this.label4.setBounds(10, 150, 80, 20);
  45. add(this.label4);
  46.  
  47.  
  48. this.textfield3 = new JTextField();
  49. this.textfield3.setBounds(120, 150, 100, 30);
  50. this.textfield3.setBackground(Color.lightGray);
  51. add(this.textfield3);
  52.  
  53. this.textfield1 = new JTextField();
  54. this.textfield1.setBounds(30, 60, 100, 30);
  55. add(this.textfield1);
  56.  
  57. this.textfield2 = new JTextField();
  58. this.textfield2.setBounds(170, 60, 100, 30);
  59. add(this.textfield2);
  60.  
  61. this.boton1 = new JButton("+");
  62. this.boton1.setBounds(30, 200, 50, 30);
  63. add(this.boton1);
  64. this.boton1.addActionListener(this);
  65.  
  66. this.boton2 = new JButton("-");
  67. this.boton2.setBounds(90, 200, 50, 30);
  68. add(this.boton2);
  69. this.boton2.addActionListener(this);
  70.  
  71. this.boton3 = new JButton("*");
  72. this.boton3.setBounds(150, 200, 50, 30);
  73. add(this.boton3);
  74. this.boton3.addActionListener(this);
  75.  
  76.  
  77. this.boton4 = new JButton("/");
  78. this.boton4.setBounds(210, 200, 50, 30);
  79. add(this.boton4);
  80. this.boton4.addActionListener(this);
  81.  
  82. this.boton5 = new JButton("Repeta");
  83. this.boton5.setBounds(30, 250, 100, 30);
  84. add(this.boton5);
  85. this.boton5.addActionListener(this);
  86.  
  87. this.boton6 = new JButton("Iesire");
  88. this.boton6.setBounds(160, 250, 100, 30);
  89. add(this.boton6);
  90. this.boton6.addActionListener(this);
  91.  
  92. setTitle("Calculator 4 operatii(+ - * /)");
  93. }
  94.  
  95. public void actionPerformed(ActionEvent e)
  96. {
  97. if (e.getSource() == this.boton6) {
  98. System.exit(0);
  99. }
  100. double rezultat = 0.0D;
  101. String cad1 = this.textfield1.getText();
  102. String cad2 = this.textfield2.getText();
  103. double x = Double.parseDouble(cad1);
  104. double y = Double.parseDouble(cad2);
  105. if (e.getSource() == this.boton1)
  106. {
  107. rezultat = x + y;
  108. this.textfield3.setText(String.valueOf(rezultat));
  109. }
  110. else if (e.getSource() == this.boton2)
  111. {
  112. rezultat = x - y;
  113. this.textfield3.setText(String.valueOf(rezultat));
  114. }
  115. else if (e.getSource() == this.boton3)
  116. {
  117. rezultat = x * y;
  118. this.textfield3.setText(String.valueOf(rezultat));
  119. }
  120. else if (e.getSource() == this.boton4)
  121. {
  122. if (y == 0.0D)
  123. {
  124. setTitle("===>> Erroare !");
  125. }
  126. else
  127. {
  128. rezultat = x / y;
  129. this.textfield3.setText(String.valueOf(rezultat));
  130. }
  131. }
  132. else if (e.getSource() == this.boton5)
  133. {
  134. this.textfield1.setText(" ");
  135. this.textfield2.setText(" ");
  136. this.textfield3.setText(" ");
  137. }
  138. }
  139.  
  140. public static void main(String[] ar)
  141. {
  142. Calculator formulario1 = new Calculator();
  143. formulario1.setBounds(0, 0, 300, 350);
  144. formulario1.setVisible(true);
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement