// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calc extends JApplet implements ActionListener {
    private JTextField val;
    private JButton add;
    private JButton sub;
    private JButton mul;
    private JButton div;
    private JButton eql;
    private int acc;
    public Calc() {
        val = new JTextField();
        add = new JButton("+");
        add.addActionListener(this);
        sub = new JButton("-");
        sub.addActionListener(this);
        mul = new JButton("*");
        mul.addActionListener(this);
        div = new JButton("/");
        div.addActionListener(this);
        eql = new JButton("=");
        eql.addActionListener(this);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(val, BorderLayout.NORTH);
        JPanel btns = new JPanel();
        btns.setLayout(new GridLayout(3, 4));
        btns.add(add);
        btns.add(sub);
        btns.add(mul);
        btns.add(div);
        btns.add(eql);
        getContentPane().add(btns, BorderLayout.CENTER);
    }
    public void init() {
        acc = 0;
    }
    public void actionPerformed(ActionEvent ev) {
        if(ev.getSource() == add) {
            acc += Integer.parseInt(val.getText());
            val.setText("");
        } else if(ev.getSource() == sub) {
            acc -= Integer.parseInt(val.getText());
            val.setText("");
        } else if(ev.getSource() == mul) {
            acc *= Integer.parseInt(val.getText());
            val.setText("");
        } else if(ev.getSource() == div) {
            acc /= Integer.parseInt(val.getText());
            val.setText("");
        } else if(ev.getSource() == eql) {
            val.setText(Integer.toString(acc));
            acc = 0;
        }
    }
}
