

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class Calculadora extends JFrame{
   private JLabel informa, resultadoLabel, linha;
   private JTextField primeiro, segundo, resultadoText;
   private JButton somar, subtrair;
   
   public Calculadora()
   {
      
      setTitle("Calculadora");
      
      Container c = getContentPane();
      c.setLayout(new FlowLayout());
      
      informa = new JLabel("Digite dois números: ");
      c.add(informa);
      primeiro=new JTextField(10);
      c.add(primeiro);
      segundo=new JTextField(10);
      c.add(segundo);
      somar= new JButton( "Somar" );
      c.add( somar );
      subtrair=new JButton("Subtrair");
      c.add(subtrair);
      resultadoLabel=new JLabel("Resultado");
      c.add(resultadoLabel);
      resultadoText=new JTextField(10);
      resultadoText.setEditable(false);
      c.add(resultadoText);
      
      Evento_Botao executar = new Evento_Botao();
      somar.addActionListener(executar);
      subtrair.addActionListener(executar);
      
      setSize(200, 190);
      
   }
   
   public static void main(String args[])
   {
      new Calculadora().show();
   }
   
   private class Evento_Botao implements ActionListener{
      public void actionPerformed(ActionEvent e)
      {
         
         double prim, seg, soma, subtrai;
         String s="";
         
         DecimalFormat twoDigits = new DecimalFormat("0.00");
         
         if(e.getSource() == somar)
         {
         
            
            prim=Double.parseDouble(primeiro.getText());
            seg=Double.parseDouble(segundo.getText());
            soma=prim+seg;
            s=twoDigits.format(soma);
         }
         if(e.getSource() == subtrair)
         {
            
            
            prim=Double.parseDouble(primeiro.getText());
            seg=Double.parseDouble(segundo.getText());
            subtrai=prim-seg;
            s=twoDigits.format(subtrai);;
         }
         resultadoText.setText(s);
         
      }
   }
}