Advertisement
gtw7375

Calculadora em Java

Oct 3rd, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class ProvaRecuperacao extends JFrame implements ActionListener{ //NUMBERFORMATEXCEPTION
  7.  
  8. JLabel titulo = new JLabel("CALCULADORA");
  9. JLabel texto1 = new JLabel("Primeiro valor:");
  10. JLabel texto2 = new JLabel("Ssegundo valor:");
  11.  
  12.  
  13.  
  14. JTextField caixa1 = new JTextField();
  15. JTextField caixa2 = new JTextField();
  16.  
  17.  
  18.  
  19. JButton soma = new JButton("+");
  20. JButton subtra = new JButton("-");
  21. JButton multi = new JButton("*");
  22. JButton divisao = new JButton("/");
  23.  
  24.  
  25.  
  26.  
  27. public ProvaRecuperacao(){
  28.  
  29. setSize(300,250);
  30. setLocation(500,200);
  31. setDefaultCloseOperation(EXIT_ON_CLOSE);
  32. getContentPane().setLayout(null);
  33. getContentPane().setBackground(Color.LIGHT_GRAY);
  34. setResizable(false);
  35.  
  36.  
  37.  
  38. titulo.setBounds(110, 10, 100, 30);
  39. getContentPane().add(titulo);
  40.  
  41. texto1.setBounds(40,65,100,20);
  42. getContentPane().add(texto1);
  43.  
  44. texto2.setBounds(40,105,100,20);
  45. getContentPane().add(texto2);
  46.  
  47.  
  48.  
  49. caixa1.setBounds(130,66,100,20);
  50. getContentPane().add(caixa1);
  51.  
  52. caixa2.setBounds(135,106,100,20);
  53. getContentPane().add(caixa2);
  54.  
  55.  
  56.  
  57. soma.setBounds(40,160,45,20);
  58. getContentPane().add(soma);
  59. soma.addActionListener(this);
  60.  
  61. subtra.setBounds(100,160,45,20);
  62. getContentPane().add(subtra);
  63. subtra.addActionListener(this);
  64.  
  65. multi.setBounds(160,160,45,20);
  66. getContentPane().add(multi);
  67. multi.addActionListener(this);
  68.  
  69. divisao.setBounds(220,160,45,20);
  70. getContentPane().add(divisao);
  71. divisao.addActionListener(this);
  72. }
  73.  
  74. public static void main(String[] args) {
  75.  
  76. ProvaRecuperacao janela = new ProvaRecuperacao();
  77. janela.setVisible(true);
  78.  
  79. }
  80.  
  81.  
  82. public void actionPerformed(ActionEvent acao) {
  83.  
  84.  
  85.  
  86. /* if(caixa1.getText().matches("//D*.*")) {
  87.  
  88. JOptionPane.showMessageDialog(null, "Digite numeros na caixa 1.");
  89. }
  90.  
  91. if(caixa2.getText().matches("//D*.*")) {
  92. JOptionPane.showMessageDialog(null, "Digite numeros na caixa 2.");
  93. //return;
  94. } */
  95.  
  96.  
  97.  
  98.  
  99. if(acao.getSource()==soma){
  100.  
  101. try{
  102.  
  103.  
  104. if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
  105. JOptionPane.showMessageDialog(null, "Caixas vazias.");
  106. return;
  107. }
  108.  
  109.  
  110. if(caixa1.getText().equals("")){
  111. JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
  112.  
  113. }
  114.  
  115.  
  116. if(caixa2.getText().equals("")){
  117. JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
  118. // return;
  119. }
  120.  
  121.  
  122.  
  123.  
  124. Double num1 = Double.parseDouble(caixa1.getText());
  125. Double num2 = Double.parseDouble(caixa2.getText());
  126. Double resul = num1 + num2;
  127.  
  128. JOptionPane.showMessageDialog(null, resul);
  129. }
  130.  
  131.  
  132.  
  133. catch(NumberFormatException e) {
  134.  
  135.  
  136.  
  137.  
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. if(acao.getSource()==subtra){
  147.  
  148. try{
  149. if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
  150. JOptionPane.showMessageDialog(null, "Caixas vazias.");
  151. return;
  152. }
  153.  
  154.  
  155. if(caixa1.getText().equals("")){
  156. JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
  157.  
  158. }
  159.  
  160.  
  161. if(caixa2.getText().equals("")){
  162. JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
  163. return;
  164. }
  165.  
  166.  
  167.  
  168.  
  169. Double num1 = Double.parseDouble(caixa1.getText());
  170. Double num2 = Double.parseDouble(caixa2.getText());
  171. Double resul = num1 - num2;
  172.  
  173. JOptionPane.showMessageDialog(null, resul);
  174. }
  175.  
  176.  
  177.  
  178. catch(NumberFormatException e) {
  179.  
  180. }
  181. }
  182.  
  183. if(acao.getSource()==multi){
  184.  
  185. try{
  186. if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
  187. JOptionPane.showMessageDialog(null, "Caixas vazias.");
  188. return;
  189. }
  190.  
  191.  
  192. if(caixa1.getText().equals("")){
  193. JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
  194.  
  195. }
  196.  
  197.  
  198. if(caixa2.getText().equals("")){
  199. JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
  200. return;
  201. }
  202.  
  203.  
  204.  
  205.  
  206. Double num1 = Double.parseDouble(caixa1.getText());
  207. Double num2 = Double.parseDouble(caixa2.getText());
  208. Double resul = num1 * num2;
  209.  
  210. JOptionPane.showMessageDialog(null, resul);
  211. }
  212.  
  213.  
  214.  
  215. catch(NumberFormatException e) {
  216.  
  217. }
  218. }
  219.  
  220. if(acao.getSource()==divisao){
  221.  
  222. try{
  223. if((caixa1.getText().equals(""))&&((caixa2.getText().equals("")))){
  224. JOptionPane.showMessageDialog(null, "Caixas vazias.");
  225. return;
  226. }
  227.  
  228.  
  229. if(caixa1.getText().equals("")){
  230. JOptionPane.showMessageDialog(null, "Caixa 1 vazia.");
  231.  
  232. }
  233.  
  234.  
  235. if(caixa2.getText().equals("")){
  236. JOptionPane.showMessageDialog(null, "Caixa 2 vazia.");
  237. return;
  238. }
  239.  
  240.  
  241.  
  242.  
  243. Double num1 = Double.parseDouble(caixa1.getText());
  244. Double num2 = Double.parseDouble(caixa2.getText());
  245. Double resul = num1 / num2;
  246.  
  247. JOptionPane.showMessageDialog(null, resul);
  248. }
  249.  
  250.  
  251.  
  252. catch(NumberFormatException e) {
  253.  
  254. }
  255. }
  256. }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement