Advertisement
emmit

Calc

Sep 25th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. package calc;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.InputMismatchException;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JTextField;
  14.  
  15. public class Calculadora extends JFrame implements ActionListener{
  16.    
  17.     JButton soma, subtrai, multiplica, dividi;
  18.     JTextField caixa1, caixa2, result;
  19.     JLabel lb_num1, lb_num2, lb_calc, lb_resultado;
  20.    
  21.    
  22.     double valor1, valor2, resultado;
  23.    
  24.     public Calculadora(){
  25.         super("calculadora");
  26.         setVisible(true);
  27.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         setSize(400, 300);
  29.         setResizable(false);
  30.         setLocationRelativeTo(null); // faz aparecer no meio da tela
  31.         setLayout(null);
  32.         setBackground(Color.DARK_GRAY);
  33.        
  34.        
  35.        soma = new JButton("Somar");
  36.        add(soma);
  37.        soma.setBounds(10, 140, 80, 30);
  38.        soma.addActionListener(this);
  39.        soma.setActionCommand("somar");
  40.        
  41.        
  42.        subtrai = new JButton("Subtrair");
  43.        add(subtrai);
  44.        subtrai.setBounds(100, 140, 80, 30);
  45.        subtrai.addActionListener(this);
  46.        subtrai.setActionCommand("subtrair");
  47.        
  48.        
  49.        multiplica = new JButton("Multiplicar");
  50.        add(multiplica);
  51.        multiplica.setBounds(190, 140, 100, 30);
  52.        multiplica.addActionListener(this);
  53.        multiplica.setActionCommand("multiplicar");
  54.        
  55.        dividi = new JButton("Dividir");
  56.        add(dividi);      
  57.        dividi.setBounds(300, 140, 80, 30);
  58.        dividi.addActionListener(this);
  59.        dividi.setActionCommand("dividir");
  60.    
  61.        caixa1 = new JTextField();
  62.        add(caixa1);
  63.        caixa1.setBounds(45, 90, 120, 25);
  64.        
  65.        caixa2 = new JTextField();
  66.        add(caixa2);
  67.        caixa2.setBounds(195, 90, 120, 25);
  68.    
  69.        result = new JTextField();
  70.        add(result);
  71.        result.setBounds(130, 220, 120, 25);
  72.        result.setEditable(false);
  73.        
  74.        lb_num1 = new JLabel("Primeiro valor");
  75.        add(lb_num1);
  76.        lb_num1.setFont( new Font("serif", Font.ITALIC, 15));
  77.        lb_num1.setBounds(45, 70, 110, 25);
  78.        
  79.        lb_num2 = new JLabel("Segundo valor");
  80.        add(lb_num2);
  81.        lb_num2.setFont( new Font("serif", Font.ITALIC, 15));
  82.        lb_num2.setBounds(195, 70, 110, 25);
  83.        
  84.        lb_calc = new JLabel("Calculadora");
  85.        add(lb_calc);
  86.        lb_calc.setFont( new Font("serif", Font.ITALIC, 33));
  87.        lb_calc.setBounds(120, 20, 220, 25);
  88.    
  89.    
  90.     }
  91.    
  92.    
  93.    
  94.     public static void main(String[] args) {
  95.         new Calculadora();
  96.     }
  97.  
  98.    
  99.    
  100.    
  101.    
  102.  
  103.  
  104.     @Override
  105.     public void actionPerformed(ActionEvent e) {
  106.        
  107.         if(e.getActionCommand().equals("somar")){
  108.            
  109.             boolean i = tratar();
  110.            
  111.              if(i == true){
  112.                
  113.                 resultado = valor1 + valor2;
  114.                 result.setText(""+resultado);
  115.              } 
  116.            
  117.            
  118.         }else if(e.getActionCommand().equals("subtrair")){
  119.            
  120.             boolean i = tratar();
  121.            
  122.              if(i == true){
  123.                
  124.                 resultado = valor1 - valor2;
  125.                 result.setText(""+resultado);
  126.              } 
  127.            
  128.         }else if(e.getActionCommand().equals("multiplicar")){
  129.            
  130.             boolean i = tratar();
  131.            
  132.              if(i == true){
  133.                
  134.                 resultado = valor1 * valor2;
  135.                 result.setText(""+resultado);
  136.              } 
  137.            
  138.         }else if(e.getActionCommand().equals("dividir")){
  139.            
  140.             boolean i = tratar();
  141.            
  142.              if(i == true){
  143.                
  144.                 resultado = valor1 / valor2;
  145.                 result.setText(""+resultado);
  146.              } 
  147.            
  148.         }
  149.     }
  150.    
  151.    
  152.    
  153.    
  154.    
  155.    
  156.    
  157.     // metodo de tratamento de valores ---------------------------------------------------
  158.     public boolean tratar(){
  159.        
  160.         // tratamento de campos vazios
  161.         if(caixa1.getText().equals("") || caixa2.getText().equals("")){
  162.            
  163.                 if(caixa1.getText().equals(""))
  164.                     caixa1.setBackground(Color.RED);                   
  165.                
  166.                 if(caixa2.getText().equals(""))
  167.                     caixa2.setBackground(Color.RED);
  168.                
  169.                 JOptionPane.showMessageDialog(null,  "Campo vazio");               
  170.                
  171.             return false;
  172.            
  173.         }else{     
  174.            
  175.             // tratamento de valores invalidos
  176.             try{
  177.            
  178.                 valor1 = Double.parseDouble(caixa1.getText());
  179.  
  180.                 valor2 = Double.parseDouble(caixa2.getText());             
  181.        
  182.             }catch(NumberFormatException erro){
  183.                
  184.                 JOptionPane.showMessageDialog(null, "Digite apenas numeros");
  185.                 return false;
  186.             }      
  187.        
  188.         }
  189.        
  190.         // se tudo der certo ;)
  191.             caixa1.setBackground(Color.white);     
  192.             caixa2.setBackground(Color.white);
  193.         return true;
  194.        
  195.     }
  196.  
  197.  
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement