Advertisement
eduensarceno

Calculadora.java

Apr 1st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JTextField;
  3. import javax.swing.JButton;
  4. import java.awt.FlowLayout;
  5. import java.awt.Dimension;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ActionEvent;
  8.  
  9. public class Calculadora extends JFrame
  10. {
  11.     private JTextField ac;
  12.     private JTextField input;
  13.     private JButton div;
  14.     private JButton mul;
  15.     private JButton res;
  16.     private JButton sum;
  17.  
  18.     public Calculadora()
  19.     {
  20.         setTitle("Calculadora");
  21.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         setLayout(new FlowLayout());
  23.         prepare();
  24.         pack();
  25.     }
  26.  
  27.     private void prepare(){
  28.         Dimension size = new Dimension(45,30);
  29.         ActionListener l = new ActionListener()
  30.         {
  31.             @Override
  32.             public void actionPerformed(ActionEvent e)
  33.             {
  34.                 double rsp = 0;
  35.                 String op = e.getActionCommand();
  36.                 switch(op){
  37.                     case SUMA :
  38.                         rsp = getAcumulador() +
  39.                               getEntrada();
  40.                         break;
  41.                     case RESTA :
  42.                         rsp = getAcumulador() -
  43.                               getEntrada();
  44.                         break;
  45.                     case MULTI :
  46.                         rsp = getAcumulador() *
  47.                               getEntrada();
  48.                         break;
  49.                     case DIVI :
  50.                         rsp = getAcumulador() /
  51.                               getEntrada();
  52.                         break;
  53.                 }
  54.                 ac.setText(Double.toString(rsp));
  55.             }
  56.         };
  57.  
  58.         ac = new JTextField("0.0");
  59.         input = new JTextField();
  60.         mul = new JButton(MULTI);
  61.         div = new JButton(DIVI);
  62.         res = new JButton(RESTA);
  63.         sum = new JButton(SUMA);
  64.  
  65.         mul.setActionCommand(MULTI);
  66.         div.setActionCommand(DIVI);
  67.         res.setActionCommand(RESTA);
  68.         sum.setActionCommand(SUMA);
  69.  
  70.         mul.addActionListener(l);
  71.         div.addActionListener(l);
  72.         res.addActionListener(l);
  73.         sum.addActionListener(l);
  74.  
  75.         ac.setPreferredSize(size);
  76.         input.setPreferredSize(size);
  77.         mul.setPreferredSize(size);
  78.         div.setPreferredSize(size);
  79.         res.setPreferredSize(size);
  80.         sum.setPreferredSize(size);
  81.        
  82.         ac.setEnabled(false);
  83.  
  84.         add(ac);
  85.         add(input);
  86.         add(sum);
  87.         add(res);
  88.         add(mul);
  89.         add(div);
  90.     }
  91.  
  92.     private double getAcumulador()
  93.     {
  94.         return Double.parseDouble(ac.getText());
  95.     }
  96.  
  97.     private double getEntrada()
  98.     {
  99.         return Double.parseDouble(input.getText());
  100.     }
  101.  
  102.  
  103.     private static final String SUMA = "+";
  104.     private static final String RESTA = "-";
  105.     private static final String MULTI = "*";
  106.     private static final String DIVI = "/";
  107.  
  108.     public static void main(String[] args)
  109.     {
  110.         (new Calculadora()).setVisible(true);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement