Alfoli

Calculadora

Feb 20th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.GridLayout;
  3. import java.awt.FlowLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.HashSet;
  7. import javax.swing.JFrame;
  8. import javax.swing.JButton;
  9. import javax.swing.JPanel;
  10. import javax.swing.JLabel;
  11. import javax.swing.JTextField;
  12. import javax.swing.SwingConstants;
  13.  
  14. public class Calculadora extends JFrame implements ActionListener{
  15.     JButton btnSoma, btnDiferenca, btnMult, btnDivide, btnClear;
  16.     JLabel txt1, txt2, txt3;
  17.     JTextField txtValor1, txtValor2, txtResul;
  18.     public void actionPerformed (ActionEvent e){
  19.         float num1, num2, resul;
  20.         try{
  21.             num1 = Float.parseFloat (txtValor1.getText());
  22.             num2 = Float.parseFloat (txtValor2.getText());
  23.         }catch (NumberFormatException erro){
  24.             num1=0;
  25.             num2=0;
  26.         }
  27.         if (e.getSource() == btnSoma){
  28.             resul = num1+num2;
  29.             txtResul.setText(String.format("%5.2f", resul));
  30.         }
  31.         else if (e.getSource()==btnDiferenca){
  32.             resul = num1-num2;
  33.             txtResul.setText(String.format("%5.2f", resul));
  34.         }
  35.         else if (e.getSource()== btnDivide){
  36.             resul = num1 / num2;
  37.             txtResul.setText (String.format("%5.2f", resul));
  38.         }
  39.         else if (e.getSource() == btnMult){
  40.             resul = num1 * num2;
  41.             txtResul.setText (String.format("%5.2f", resul));
  42.         }
  43.         else{
  44.             txtValor1.setText("");
  45.             txtValor2.setText("");
  46.             txtResul.setText("");
  47.         }
  48.        
  49.     }
  50.  
  51.  
  52. public Calculadora(){
  53.     setLayout (new BorderLayout());
  54.     btnSoma = new JButton ("+");
  55.     btnDiferenca = new JButton ("-");
  56.     btnDivide = new JButton ("/");
  57.     btnMult = new JButton ("x");
  58.     btnClear = new JButton ("Clear");
  59.     JPanel leste = new JPanel (new GridLayout (5,1));
  60.     leste.add(btnSoma);
  61.     leste.add(btnDiferenca);
  62.     leste.add(btnDivide);
  63.     leste.add(btnMult);
  64.     leste.add(btnClear);
  65.     add (leste, BorderLayout.EAST);
  66.     JPanel centro = new JPanel (new GridLayout (3,2));
  67.     txt1 = new JLabel ("Entre com o valor 1   :", SwingConstants.RIGHT);
  68.     txt2 = new JLabel ("Entre com o valor 2   :", SwingConstants.RIGHT);
  69.     txt3 = new JLabel ("Resultado   :", SwingConstants.RIGHT);
  70.     txtValor1 = new JTextField (5);
  71.     txtValor2 = new JTextField (5);
  72.     txtResul = new JTextField (5);
  73.     centro.add (txt1);
  74.     centro.add (txtValor1);
  75.     centro.add (txt2);
  76.     centro.add (txtValor2);
  77.     centro.add (txt3);
  78.     centro.add (txtResul);
  79.     add (centro, BorderLayout.CENTER);
  80.     btnSoma.addActionListener(this);
  81.     btnDiferenca.addActionListener(this);
  82.     btnMult.addActionListener(this);
  83.     btnDivide.addActionListener(this);
  84.     btnClear.addActionListener(this);
  85. }
  86.  
  87. public static void main (String [] args){
  88.     Calculadora tela = new Calculadora ();
  89.     tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  90.     tela.setSize (350,250);
  91.     tela.setLocation (500,300);
  92.     tela.setVisible(true);
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment