Guest User

Untitled

a guest
Jul 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. package com.javadesktop.aula10;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14. import javax.swing.UIManager;
  15.  
  16. public class Calculadora {
  17.  
  18. private JFrame frmCalculadoraSimples;
  19. private JTextField textVisor;
  20. float valorAnterior, valorAtual, resultado;
  21. int operacao;
  22.  
  23. /**
  24. * Startando a aplicação
  25. */
  26. public static void main(String[] args) {
  27. EventQueue.invokeLater(new Runnable() {
  28. public void run() {
  29. try {
  30. Calculadora window = new Calculadora();
  31. window.frmCalculadoraSimples.setVisible(true);
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. });
  37. }
  38.  
  39. /**
  40. * Criando a aplicação
  41. */
  42. public Calculadora() {
  43. initialize();
  44. }
  45.  
  46. /**
  47. * Iniciando o conteúdo da tela
  48. */
  49. private void initialize() {
  50. frmCalculadoraSimples = new JFrame();
  51. frmCalculadoraSimples.setTitle("Calculadora Simples");
  52. frmCalculadoraSimples.setBounds(100, 100, 213, 300);
  53. frmCalculadoraSimples.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.  
  55. JPanel panel = new JPanel();
  56. frmCalculadoraSimples.getContentPane().add(panel, BorderLayout.NORTH);
  57. panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
  58.  
  59. textVisor = new JTextField();
  60. textVisor.setEditable(false);
  61. textVisor.setForeground(UIManager.getColor("CheckBox.foreground"));
  62. textVisor.setBackground(UIManager.getColor("Button.focus"));
  63. panel.add(textVisor);
  64. textVisor.setColumns(10);
  65.  
  66. JPanel panel_1 = new JPanel();
  67. frmCalculadoraSimples.getContentPane().add(panel_1, BorderLayout.CENTER);
  68.  
  69. JButton btn3 = new JButton("3");
  70. btn3.addActionListener(new ActionListener() {
  71. public void actionPerformed(ActionEvent e) {
  72. textVisor.setText(textVisor.getText() + "3");
  73. }
  74. });
  75.  
  76. JButton btn1 = new JButton("1");
  77. btn1.addActionListener(new ActionListener() {
  78. public void actionPerformed(ActionEvent e) {
  79. textVisor.setText(textVisor.getText() + "1");
  80. }
  81. });
  82. panel_1.setLayout(new GridLayout(0, 4, 0, 0));
  83. panel_1.add(btn1);
  84.  
  85. JButton btn2 = new JButton("2");
  86. btn2.addActionListener(new ActionListener() {
  87. public void actionPerformed(ActionEvent arg0) {
  88. textVisor.setText(textVisor.getText() + "2");
  89. }
  90. });
  91. panel_1.add(btn2);
  92. panel_1.add(btn3);
  93.  
  94. JButton btn0 = new JButton("0");
  95. btn0.addActionListener(new ActionListener() {
  96. public void actionPerformed(ActionEvent e) {
  97. textVisor.setText(textVisor.getText() + "0");
  98. }
  99. });
  100.  
  101. JButton btn5 = new JButton("5");
  102. btn5.addActionListener(new ActionListener() {
  103. public void actionPerformed(ActionEvent e) {
  104. textVisor.setText(textVisor.getText() + "5");
  105. }
  106. });
  107.  
  108. JButton btn4 = new JButton("4");
  109. btn4.addActionListener(new ActionListener() {
  110. public void actionPerformed(ActionEvent e) {
  111. textVisor.setText(textVisor.getText() + "4");
  112. }
  113. });
  114. panel_1.add(btn4);
  115. panel_1.add(btn5);
  116.  
  117. JButton btn8 = new JButton("8");
  118. btn8.addActionListener(new ActionListener() {
  119. public void actionPerformed(ActionEvent e) {
  120. textVisor.setText(textVisor.getText() + "8");
  121. }
  122. });
  123.  
  124. JButton btn7 = new JButton("7");
  125. btn7.addActionListener(new ActionListener() {
  126. public void actionPerformed(ActionEvent e) {
  127. textVisor.setText(textVisor.getText() + "7");
  128. }
  129. });
  130.  
  131. JButton btn6 = new JButton("6");
  132. btn6.addActionListener(new ActionListener() {
  133. public void actionPerformed(ActionEvent e) {
  134. textVisor.setText(textVisor.getText() + "6");
  135. }
  136. });
  137. panel_1.add(btn6);
  138. panel_1.add(btn7);
  139. panel_1.add(btn8);
  140.  
  141. JButton btn9 = new JButton("9");
  142. btn9.addActionListener(new ActionListener() {
  143. public void actionPerformed(ActionEvent e) {
  144. textVisor.setText(textVisor.getText() + "9");
  145. }
  146. });
  147. panel_1.add(btn9);
  148. panel_1.add(btn0);
  149.  
  150. JButton btnC = new JButton("C");
  151. btnC.addActionListener(new ActionListener() {
  152. public void actionPerformed(ActionEvent e) {
  153. textVisor.setText(null);
  154. }
  155. });
  156.  
  157. JButton button = new JButton("=");
  158. button.addActionListener(new ActionListener() {
  159. public void actionPerformed(ActionEvent e) {
  160. valorAtual = Float.parseFloat(textVisor.getText());
  161.  
  162. if (operacao == 1) { resultado = valorAnterior + valorAtual; }
  163. if (operacao == 2) { resultado = valorAnterior - valorAtual; }
  164. if (operacao == 3) { resultado = valorAnterior / valorAtual; }
  165. if (operacao == 4) { resultado = valorAnterior * valorAtual; }
  166.  
  167. textVisor.setText( String.valueOf(resultado) );
  168. }
  169. });
  170. panel_1.add(button);
  171. panel_1.add(btnC);
  172.  
  173. JPanel panel_2 = new JPanel();
  174. frmCalculadoraSimples.getContentPane().add(panel_2, BorderLayout.SOUTH);
  175.  
  176. JButton btnAdd = new JButton("+");
  177. btnAdd.addActionListener(new ActionListener() {
  178. public void actionPerformed(ActionEvent e) {
  179. valorAnterior = Float.parseFloat(textVisor.getText());
  180. textVisor.setText(null);
  181. operacao = 1;
  182. }
  183. });
  184. panel_2.setLayout(new GridLayout(0, 4, 0, 0));
  185. panel_2.add(btnAdd);
  186.  
  187. JButton btnSub = new JButton("-");
  188. btnSub.addActionListener(new ActionListener() {
  189. public void actionPerformed(ActionEvent e) {
  190. valorAnterior = Float.parseFloat(textVisor.getText());
  191. textVisor.setText(null);
  192. operacao = 2;
  193. }
  194. });
  195. panel_2.add(btnSub);
  196.  
  197. JButton btnDiv = new JButton("/");
  198. btnDiv.addActionListener(new ActionListener() {
  199. public void actionPerformed(ActionEvent e) {
  200. valorAnterior = Float.parseFloat(textVisor.getText());
  201. textVisor.setText(null);
  202. operacao = 3;
  203. }
  204. });
  205. panel_2.add(btnDiv);
  206.  
  207. JButton btnMul = new JButton("*");
  208. btnMul.addActionListener(new ActionListener() {
  209. public void actionPerformed(ActionEvent e) {
  210. valorAnterior = Float.parseFloat(textVisor.getText());
  211. textVisor.setText(null);
  212. operacao = 4;
  213. }
  214. });
  215. panel_2.add(btnMul);
  216. }
  217.  
  218. }
Add Comment
Please, Sign In to add comment