Project: BuildCalculator
Basic info: This program construtcs a simple calculator. Note that some calculations may yield unneccesarry decimal digits because I did not put a code to format the output variable which is a double. You can try that as an exercise.

This program contains: using Java classes JButton and  JTextField, using Layout Management, using Event Handling
The Code
// Calculator.java
// author: Yigit Oktar

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calculator extends JFrame
{
    private JPanel buttonPanel;
    private JPanel textPanel;
    private JButton buttons[];
    private JButton clear;
    private JTextField textOut;
    String string = "";
    int operate = -1;
    double valOne = 0;
    double valTwo = 0;
    double result = 0;
    int loop = -1;       
   
    public Calculator()
    {
        super("Calculator");
        clear = new JButton("c");
        buttons = new JButton[16];
        buttonPanel = new JPanel();
        textPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(4,4,3,3));
        textOut = new JTextField(17);
        FlowLayout flow = new FlowLayout();
        flow.setAlignment(FlowLayout.LEFT);
        textPanel.setLayout(flow);
        textPanel.add(clear);
        textPanel.add(textOut);
        add(textPanel, BorderLayout.NORTH);
        NumberHandler handler = new NumberHandler();
        for (int count = 0; count < 3; count++)
        {
            buttons[count] = new JButton("" + (count+1));
        }
         for (int count = 4; count < 7; count++)
         {
            buttons[count] = new JButton("" + (count));
         }
         for (int count = 8; count < 11; count++)
         {
             buttons[count] = new JButton("" + (count-1));
         }
        buttons[3] = new JButton("/");
        buttons[7] = new JButton("*");
        buttons[11] = new JButton("-");
        buttons[12] = new JButton("0");
        buttons[13] = new JButton(".");
        buttons[14] = new JButton("=");
        buttons[15] = new JButton("+");
       
        for (int count =0; count < 16; count++)
        {
            buttonPanel.add(buttons[count]);
            buttons[count].addActionListener(handler);
        }
        clear.addActionListener(handler);
        add(buttonPanel, BorderLayout.CENTER);
    } 
        private class NumberHandler implements ActionListener
        {
           
            static final int DIVIDE = 5;
            static final int MULT = 4;
            static final int SUB = 6;
            static final int ADD = 3;
           
            public void actionPerformed(ActionEvent event)
            {
                if(event.getSource()== clear)
                {
                    valOne=0;
                    valTwo=0;
                    operate = -1;
                    string = "";
                    textOut.setText("");
                }

                for (int count = 0; count <16; count++)
                {
                    if(operate == -1)
                    {
 
                    if (event.getSource() == buttons[count] && count < 3 )
                    string = string + (count+1);
                   
                    if (event.getSource() == buttons[count] && count == 3)
                    {
                        string = "";
                        operate = DIVIDE;
                    }
                   
                    if (event.getSource() == buttons[count] && count > 3 && count < 7 )
                    string = string + (count);
                   
                     if (event.getSource() == buttons[count] && count == 7)
                    {
                        operate = MULT;
                        string = "";
                    }
                  
                    if (event.getSource() == buttons[count] && count > 7 && count < 11 )
                    string = string + (count-1);
                   
                    if (event.getSource() == buttons[count] && count == 11)
                    {
                        operate = SUB;
                        string = "";
                    }
                    if (event.getSource() == buttons[count] && string !=  "" && count == 12)
                    string = string + "0";
                    if (event.getSource() == buttons[count] && !string.contains(".") && count == 13 && string == "")
                    string = string + "0."; 
                     if (event.getSource() == buttons[count] && !string.contains(".") && count == 13 && string != "")
                    string = string + ".";
                    if (event.getSource() == buttons[15])
                    {
                        operate = ADD;
                        string = "";
                    }  
                     if (!string.equals(""))
                    valOne = Double.parseDouble(string);
                    }
                }
                for (int count = 0; count <16; count++)
                   {
                    if(operate != -1)
                    {
                    if (event.getSource() == buttons[13] && !string.contains(".")&& string == "" )
                    string = string + "0.";
                     if (event.getSource() == buttons[count] && !string.contains(".") && count == 13 && string != "")
                    string = string + ".";
                    if (event.getSource() == buttons[count] && string != "" && count ==12 )
                    string = string + 0;
                       
                    if (event.getSource() == buttons[count] && count < 3 )
                    string = string + (count+1);
                   
                    if (event.getSource() == buttons[count] && count > 3 && count < 7 )
                    string = string + (count);
                  
                    if (event.getSource() == buttons[count] && count > 7 && count < 11 )
                    string = string + (count-1);
                   
                    if (!string.equals(""))      
                        valTwo = Double.parseDouble(string);
                   
                    if (event.getSource() == buttons[14])
                    {
                        switch (operate)
                        { 
                        case DIVIDE:
                            result =  valOne / valTwo;
                            break;
                        case MULT:
                            result =  valOne * valTwo;
                            break;
                        case SUB:
                            result =  valOne - valTwo;
                            break;
                        case ADD:
                            result =  valOne + valTwo;
                            break;
                       
                        }
                        string = string.valueOf(result);
                        valOne = result;
                        valTwo = 0;
                        operate = -1;
                    }
                    }
                   }

                    if(operate == -1 || string != "")
                    textOut.setText(string);
                }
                           
            }
        }
Sumbit something related to this project like a question, a suggestion, etc..
Project Discussion
import javax.swing.JFrame;

public class CalcTest extends JFrame
{
    public static void main (String args[])
    {
        Calculator calculator = new Calculator();
        calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        calculator.setSize(256,300);
        calculator.setVisible(true);
    }
}
See the Applet Form of this Code
Hosting by WebRing.