Simple Calculator program in java using applet

Posted By: Java Examples - 9:03 PM

Share

& Comment

Java version of Simple Calculator is given bellow :
package frame;

import java.awt.BorderLayout;
import java.awt.Container;
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;

/**
 *
 * @author ACHCHUTHAN
 */
public class Calculatorpanel extends JApplet implements ActionListener {

    public Calculatorpanel() {
        setLayout(new BorderLayout());
        display = new JTextField("0");
        display.setEditable(false);
        add(display, "North");
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(4, 4));
        String buttons = "789/456*123-0.=+";
        for (int i = 0; i < buttons.length(); i++) {
            addButton(p, buttons.substring(i, i + 1));
        }
        add(p, "Center");
    }

    private void addButton(Container c, String s) {
        JButton b = new JButton(s);
        c.add(b);
        b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        String s = ae.getActionCommand();
        if ('0' < s.charAt(0) && s.charAt(0) <= '9' || s.equals(".")) {
            if (start) {
                display.setText(s);
            } else {
                display.setText(display.getText() + s);
            }
            start = false;
        } else {
            if (start) {
                if (s.equals("=")) {
                    display.setText(s);
                    start = false;
                } else {
                    op = s;
                }
            } else {
                double x = Double.parseDouble(display.getText());
                Calculate(x);
                op = s;
                start = true;
            }
        }
    }

    public void Calculate(double n) {
        if (op.equals("+")) {
            arg += n;
        } else if (op.equals("-")) {
            arg -= n;
        } else if (op.equals("*")) {
            arg *= n;
        } else if (op.equals("/")) {
            arg /= n;
        } else if (op.equals("=")) {
            arg = n;
        }
        display.setText(" " + arg);

    }
    private JTextField display;
    private double arg = 0;
    private String op = "=";
    private boolean start = true;
}
Java Applet version of Simple Calculator is given bellow :
package frame;

import java.awt.Container;
import javax.swing.JApplet;

/**
 *
 * @author ACHCHUTHAN
 */
public class CalculatorApplet extends JApplet {

    /**
     * Called by the browser or applet viewer to inform this applet that it has
     * been loaded into the system. It is always called before the first time
     * that the
     * start method is called. A subclass of
     * Applet should override this method if it has initialization
     * to perform. For example, an applet with threads would use the
     * init method to create the threads and the
     * destroy method to kill them. 
The implementation of this
     * method provided by the
     * Applet class does nothing.
     *
     * @see java.applet.Applet#destroy()
     * @see java.applet.Applet#start()
     * @see java.applet.Applet#stop()
     */
    public void init() {
        Container ContentPane = getContentPane();
        ContentPane.add(new Calculatorpanel());
    }
}
HTML version of Simple Calculator is given bellow :
<html>  
<head></head>  
<body> 
<applet CODE="CalculatorApplet.class" width="300" height="300">
</applet>  
</body>  
</html>
Simple Calculater

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

2 comments :

Post a Comment

Thank you for vising Java90.blogspot.com

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .