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

public class TwoCalc extends JFrame implements ActionListener {

    float var1   =  0;
    float var2   =  0;
    String entry = "";

    JButton    one    = new JButton("1");
    JButton    two    = new JButton("2");
    JButton    three  = new JButton("3");
    JButton    four   = new JButton("4");
    JButton    five   = new JButton("5");
    JButton    six    = new JButton("6");
    JButton    seven  = new JButton("7");
    JButton    eight  = new JButton("8");
    JButton    nine   = new JButton("9");
    JButton    zero   = new JButton("0");
    JButton    period = new JButton(" .");
    JButton    plus   = new JButton("  +  ");
    JButton    minus  = new JButton("  -  ");
    JButton    mult   = new JButton("  x  ");
    JButton    divide = new JButton("  /  ");
    JButton    pct    = new JButton("%");
    JButton    equal  = new JButton("     =     ");
    JButton    clear  = new JButton(" C ");
    JTextField result = new JTextField("0", 15);
    JLabel v1 = new JLabel();
    JLabel v2 = new JLabel();

    public static void main(String[] arguments) {
        TwoCalc frame = new TwoCalc();
    }

    public TwoCalc() {

        setTitle("Calcuate Two Numbers");
        setSize(220, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container  window = getContentPane();   //get ref to ContentPane

        FlowLayout layout = new FlowLayout();   //create a FlowLayout
        window.setLayout(layout);               //Flow the window using layout

        Color c = new Color(200,255,255);       //create an RGB color
        window.setBackground(c);                //set the background color

        window.add(result);
        window.add(one);
        window.add(two);
        window.add(three);
        window.add(plus);
        window.add(four);
        window.add(five);
        window.add(six);
        window.add(minus);
        window.add(seven);
        window.add(eight);
        window.add(nine);
        window.add(mult);
        window.add(pct);
        window.add(zero);
        window.add(period);
        window.add(divide);
        window.add(v1);
        window.add(equal);
        window.add(clear);
        window.add(v2);

        setVisible(true);                       //make window visible

        one.addActionListener(this);            //add an event handler on button
        two.addActionListener(this);        
        three.addActionListener(this);      
        four.addActionListener(this);       
        five.addActionListener(this);       
        six.addActionListener(this);        
        seven.addActionListener(this);      
        eight.addActionListener(this);      
        nine.addActionListener(this);       
        zero.addActionListener(this);       
        plus.addActionListener(this);       
        minus.addActionListener(this);      
        mult.addActionListener(this);       
        divide.addActionListener(this);     
        equal.addActionListener(this);      
        clear.addActionListener(this);      
    }

    public void actionPerformed(ActionEvent evt) {

        Object source = evt.getSource();        //which field caused the event?

    if (source == one)
        entry += "1";
    if (source == two)
        entry += "2";
    if (source == plus) {
        var2 = Float.parseFloat(entry);  
        var1 += var2;
        entry = "";
    }
    if (source == equal) {
        result.setText(" " + var1);  
        var1 += var2;
        entry = "";
    }
    if (source == clear) {
        var1 = 0;  
        var2 = 0;
        entry = "";
    }
    result.setText(entry);
    v1.setText(" " + var1);
    v2.setText(" " + var2); 

/*
        try {
            float value1 = Float.parseFloat(one.getText());
            float value2 = Float.parseFloat(two.getText());
            result.setText("" + (value1 + value2));
        }
        catch (NumberFormatException e) {
            result.setText("???");
        }
*/
     }

}