I need little help in adding Sin & Cos functions to my calculater , and also in Layout (need textfield to be on the top), so please help me if you can..
that's my code:

package javaapplication3;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
//import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import java.lang.Math.*;

public class Calculator3 extends JApplet implements ActionListener
{

/**************************
    Data Members
***************************/

    private JTextField  JPanel;     //To accept user input and display result
    private JButton     JButton0,   //This JButton to pass 0 to JPanel
                JButton1,   //This JButton to pass 1 to JPanel
                JButton2,   //This JButton to pass 2 to JPanel
                JButton3,   //This JButton to pass 3 to JPanel
                JButton4,   //This JButton to pass 4 to JPanel
                JButton5,   //This JButton to pass 5 to JPanel
                JButton6,   //This JButton to pass 6 to JPanel
                JButton7,   //This JButton to pass 7 to JPanel
                JButton8,   //This JButton to pass 8 to JPanel
                JButton9,   //This JButton to pass 9 to JPanel
                JButtonDiv, //This JButton operate division
                JButtonMul, //This JButton operate multiplication
                JButtonAdd, //This JButton operate addition
                JButtonSub, //This JButton opearte subtraction
                JButtonAss, //This JButton return result
                JButtonOpar,    //This JButton call open parentheses
                JButtonCpar,    //This JButton call close parentheses
                JButtonPoint,   //This JButton to pass . to JPanel
                JButtonClear;   //This JButton clear JPanel
              private JButton   JButtonSin; //This JButton clear JPanel
              private JButton   JButtonCos;
          private int   Flag,       //Record the state after calculating
                Err_lve;    //State the error level


/*************************
    Initialize similar like Constructor, that is use for JApplet
**************************/

    //public void init()
    {
        //setBackground(Color.white);   //Set white backgroud

            JPanel = new JTextField();

        //JPanel.setBounds( 20, 20, 165, 25);
        add(JPanel);
                JPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

                setLayout(new GridLayout(5, 5, 5, 5));
        resize(205, 210);

        Flag = 0;
        Err_lve = 0;


        JButton0 = new JButton("0");
        //JButton0.setBounds (20, 160, 25, 25);
        add(JButton0);
        JButton1 = new JButton("1");
        //JButton1.setBounds (20, 125, 25, 25);
        add(JButton1);
        JButton2 = new JButton("2");
        //JButton2.setBounds (55, 125, 25, 25);
        add(JButton2);
        JButton3 = new JButton("3");
        //JButton3.setBounds (90, 125, 25, 25);
        add(JButton3);
        JButton4 = new JButton("4");
        //JButton4.setBounds (20, 90, 25, 25);
        add(JButton4);
        JButton5 = new JButton("5");
        //JButton5.setBounds (55, 90, 25, 25);
        add(JButton5);
        JButton6 = new JButton("6");
        //JButton6.setBounds (90, 90, 25, 25);
        add(JButton6);
        JButton7 = new JButton("7");
        //JButton7.setBounds (20, 55, 25, 25);
        add(JButton7);
        JButton8 = new JButton("8");
        //JButton8.setBounds (55, 55, 25, 25);
        add(JButton8);
        JButton9 = new JButton("9");
        //JButton9.setBounds (90, 55, 25, 25);
        add(JButton9);
        JButtonDiv = new JButton("/");
        //JButtonDiv.setBounds (160, 90, 25, 25);
        add(JButtonDiv);
        JButtonMul = new JButton("*");
        //JButtonMul.setBounds (125, 90, 25, 25);
        add(JButtonMul);
        JButtonAdd = new JButton("+");
        //JButtonAdd.setBounds (125, 55, 25, 25);
        add(JButtonAdd);
        JButtonSub = new JButton("-");
        //JButtonSub.setBounds (160, 55, 25, 25);
        add(JButtonSub);
        JButtonAss = new JButton("=");
        //JButtonAss.setBounds (90, 160, 25, 25);
        add(JButtonAss);
        JButtonOpar = new JButton("(");
        //JButtonOpar.setBounds (125, 125, 25, 25);
        add(JButtonOpar);
        JButtonCpar = new JButton(")");
        //JButtonCpar.setBounds (160, 125, 25, 25);
        add(JButtonCpar);
        JButtonPoint = new JButton(".");
        //JButtonPoint.setBounds (55, 160, 25, 25);
        add(JButtonPoint);
        JButtonClear = new JButton("Clear");
        //JButtonClear.setBounds (125, 160, 60, 25);
        add(JButtonClear);
                JButtonSin = new JButton("S");
        //JButtonAdd.setBounds (125, 55, 25, 25);
        add(JButtonSin);
                JButtonCos = new JButton("Cos");
        //JButtonAdd.setBounds (125, 55, 25, 25);
        add(JButtonCos);                


        JPanel.addActionListener(this);
        JButton0.addActionListener(this);
        JButton1.addActionListener(this);
        JButton2.addActionListener(this);
        JButton3.addActionListener(this);
        JButton4.addActionListener(this);
        JButton5.addActionListener(this);
        JButton6.addActionListener(this);
        JButton7.addActionListener(this);
        JButton8.addActionListener(this);
        JButton9.addActionListener(this);
        JButtonDiv.addActionListener(this);
        JButtonMul.addActionListener(this);
        JButtonAdd.addActionListener(this);
        JButtonSub.addActionListener(this);
        JButtonAss.addActionListener(this);
        JButtonOpar.addActionListener(this);
        JButtonCpar.addActionListener(this);
        JButtonPoint.addActionListener(this);
        JButtonClear.addActionListener(this);
                JButtonSin.addActionListener(this);
                JButtonCos.addActionListener(this);
    }


    /*
    Method:     paint
    Purpose:    Draw the outline of calculator *(this method can auto. draw when develop JApplet, but it isn't JApplet's method)
    Parameters: Graphics
    Returns:    None
    */

    /*public void paint(Graphics graphic)
    {
        graphic.setColor(Color.yellow);
        graphic.drawRoundRect(0, 0, 190, 195, 15, 15);
        graphic.setColor(Color.lightGray);
        graphic.drawRoundRect(1, 1, 191, 196, 15, 15);
        graphic.drawRoundRect(2, 2, 192, 197, 15, 15);
        graphic.drawRoundRect(3, 3, 193, 198, 15, 15);
        graphic.drawRoundRect(4, 4, 194, 199, 15, 15);
        graphic.setColor(Color.black);
        graphic.fillRoundRect(5, 5, 195, 200, 15, 15);
        graphic.setColor(Color.green);
        graphic.drawRoundRect(5, 5, 195, 200, 15, 15);
        graphic.setColor(Color.yellow);
        graphic.drawString("Data Structure", 45, 200);
    }
*/

    /*
    Method:     actionPerformed
    Purpose:    Event-handling
    Parameters: Action event
    Returns:    None
    */

    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() instanceof JButton)
        {
            JButton clickedJButton = (JButton) event.getSource();
            if (clickedJButton == JButtonClear)
            {
                ClearJPanel();
                Flag = 0;
            }
                else if (clickedJButton == JButtonAss)
                {
                    Err_lve = 0;
                    Compute();
                    Flag = 1;
                }
                    else
                    {
                        if (Flag == 1)
                        {
                            ClearJPanel();  //clear JPanel after calculation
                            Flag = 0;
                        }
                        DisplayEx(clickedJButton);
                    }
        }
        else
        {
            Err_lve = 0;
            Compute();
            Flag = 1;
        }
    }


    /*
    Method:     ClearJPanel
    Purpose:    Clear JPanel JTextField objects
    Parameters: None
    Returns:    None
    */

    private void ClearJPanel()
    {
        JPanel.setText("");
    }


    /*
    Method:     DisplayEx
    Purpose:    Display the whole expression that the user enters
    Parameters: clickedJButton
    Returns:    None
    */

    private void DisplayEx(JButton ClickedJButton)
    {
        String tmpstr;
        tmpstr = JPanel.getText();
        if (ClickedJButton == JButton0) JPanel.setText(tmpstr+"0");
        if (ClickedJButton == JButton1) JPanel.setText(tmpstr+"1");
        if (ClickedJButton == JButton2) JPanel.setText(tmpstr+"2");
        if (ClickedJButton == JButton3) JPanel.setText(tmpstr+"3");
        if (ClickedJButton == JButton4) JPanel.setText(tmpstr+"4");
        if (ClickedJButton == JButton5) JPanel.setText(tmpstr+"5");
        if (ClickedJButton == JButton6) JPanel.setText(tmpstr+"6");
        if (ClickedJButton == JButton7) JPanel.setText(tmpstr+"7");
        if (ClickedJButton == JButton8) JPanel.setText(tmpstr+"8");
        if (ClickedJButton == JButton9) JPanel.setText(tmpstr+"9");
        if (ClickedJButton == JButtonDiv)   JPanel.setText(tmpstr+"/");
        if (ClickedJButton == JButtonMul)   JPanel.setText(tmpstr+"*");
        if (ClickedJButton == JButtonAdd)   JPanel.setText(tmpstr+"+");
        if (ClickedJButton == JButtonSub)   JPanel.setText(tmpstr+"-");
        if (ClickedJButton == JButtonOpar)JPanel.setText(tmpstr+"(");
        if (ClickedJButton == JButtonCpar)JPanel.setText(tmpstr+")");
        if(ClickedJButton == JButtonPoint)JPanel.setText(tmpstr+".");
                if (ClickedJButton == JButtonSin)   JPanel.setText(tmpstr+"S");
                if (ClickedJButton == JButtonCos)   JPanel.setText(tmpstr+"Cos ");
        }


    /*
    Method:     Compute
    Purpose:    Calculate the expression and shown the result, when the user hit return key or "=" JButton.
    Parameters: The whole expression
    Returns:    None
    */

    private void Compute()
    {
        String  Tmpstr, Result="";
        char    ch;
        int i, no_of_char,
            no_of_par = 0;

        Tmpstr      = JPanel.getText();
        no_of_char  = Tmpstr.length();

        //Expression brief checking
        for (i = 0; i < no_of_char; i++)
        {
            ch = Tmpstr.charAt(i);
            if (ch == ')') no_of_par--;
            if (no_of_par < 0) Err_lve = 1;
            if (ch == '(') no_of_par++;
            if (ch < '(' || ch > '9' || ch == ',') Err_lve = 2;
            if (ch == '.' && (i+1 < Tmpstr.length()) )
                for ( int j = i+1; (j < Tmpstr.length()) && ((Character.isDigit(Tmpstr.charAt(j))) || ((Tmpstr.charAt(j))) == '.'); j++ )
                    if (Tmpstr.charAt(j) == '.') Err_lve = 3;
                                //If an operand has more than one point return error
        }//End of expression brief checking

        if (no_of_par != 0) Err_lve = 1;        //If open and close parentheses do not match return error

        if (Err_lve != 0) Err_msg(Err_lve);     //An error perform to prompt error message
        else Result = Calculate(Tmpstr);        //No error perform to calculate expression
        if (Err_lve != 0) Err_msg(Err_lve);     //An error perform to prompt error message
        else JPanel.setText(Result);            //No error show result
    }


    /*
    Method:     Calculate
    Purpose:    Implement the expression
    Parameters: Expression
    Returns:    None
    */

    private String Calculate(String expression)
    {
        String  result = expression, f_operand, r_operand;
        char    cha;
        int index, f_index, r_index,
            no_of_cha = result.length(),
            no_of_pare = 0, pare_match = 0, op_index = 0, cp_index = 0;

        if (Err_lve == 0)
        {
        //Checking Parentheses
        for (index = 0; index < no_of_cha; index++)
        {
            cha = result.charAt(index);

            if (cha == '(')
            {
                if (pare_match == 0)    op_index = index;
                pare_match ++;
                no_of_pare ++;
            }

            if (cha == ')')
            {
                pare_match --;
                if (pare_match == 0)    cp_index = index;
            }
        }//End of checking Parentheses

        if (op_index+1 == cp_index) Err_lve = 3;

        //Recursive Calculate, when parentheses existed
        if (Err_lve == 0 && no_of_pare > 0)
        {
        if ((op_index == 0) && (cp_index == (no_of_cha - 1)) && (op_index != cp_index)) result = Calculate(result.substring(op_index + 1, cp_index));
            else if (op_index == 0 && cp_index > 0)
            {
                if ( (Character.isDigit(result.charAt(cp_index+1))) ) Err_lve = 3;
                else
                {
                    result = Calculate(result.substring(op_index + 1, cp_index)) + result.substring(cp_index + 1);
                    no_of_pare--;
                    while(no_of_pare != 0)
                    {
                        result = Calculate(result);
                        no_of_pare--;
                    }
                }
            }
                else if ((op_index > 0) && (cp_index > 0) && (cp_index != no_of_cha -1))
                {
                    if ( (Character.isDigit(result.charAt(cp_index+1))) || (Character.isDigit(result.charAt(op_index-1))) ) Err_lve = 3;
                    else
                    {
                        result = result.substring(0, op_index) + Calculate(result.substring(op_index +1, cp_index)) + result.substring(cp_index +1);
                        no_of_pare--;
                        while(no_of_pare != 0)
                        {
                            result = Calculate(result);
                            no_of_pare--;
                        }
                    }
                }
                    else if (cp_index == no_of_cha -1 && op_index > 0)
                    {
                        if ( (Character.isDigit(result.charAt(op_index-1))) ) Err_lve = 3;
                        else
                        {
                            result = result.substring(0, op_index) + Calculate(result.substring(op_index + 1, cp_index));
                            no_of_pare--;
                            while(no_of_pare != 0)
                            {
                                result = Calculate(result);
                                no_of_pare--;
                            }
                        }
                    }
        }//End of recursive Calculate statement


        //Implement algorithm
        if (no_of_pare == 0 && Err_lve == 0)
        {
            if ( (!(Character.isDigit(result.charAt(0))) && (result.charAt(0) != '-')) || !(Character.isDigit(result.charAt(result.length()-1))) ) Err_lve = 3;

            //Implement multiply and divide first
            for (index = 0; index < result.length() && (Err_lve == 0); index++)
            {
                cha = result.charAt(index);

                if (cha == '*' || cha == '/')
                {
                    if ( !(Character.isDigit(result.charAt(index-1))) || ( !(Character.isDigit(result.charAt(index+1))) && (result.charAt(index+1) != '-') ) ) Err_lve = 3;
                    if (result.charAt(index+1) == '-')
                        if ( !(Character.isDigit(result.charAt(index+2))) ) Err_lve = 3;
                    if (Err_lve == 0)
                    {
                        f_index = index - 1;

                        if (f_index > 2)
                            if ( ((result.charAt(f_index-1)) == '-') && ((result.charAt(f_index-2)) == 'E') )
                                f_index = f_index - 2;

                        while ( (f_index > 0) && ((Character.isDigit(result.charAt(f_index-1))) || ((result.charAt(f_index-1)) == '.') || ((result.charAt(f_index-1)) == 'E')) )
                        {
                            f_index--;
                        }
                        if (f_index ==1)
                            if ((result.charAt(f_index-1)) == '-')
                                f_index--;
                        if (f_index > 2)
                            if ( ((result.charAt(f_index-1)) == '-') && !(Character.isDigit(result.charAt(f_index-2))) )
                                f_index--;
                        f_operand = result.substring(f_index, index);

                        r_index = index + 1;
                        while ( (r_index < result.length()-1) && ((Character.isDigit(result.charAt(r_index+1))) || ((result.charAt(r_index+1)) == '.') || ((result.charAt(r_index+1)) == 'E')) )
                        {
                            r_index++;
                            if (r_index < result.length()-2)
                                if ( ((result.charAt(r_index)) == 'E') && ((result.charAt(r_index+1)) == '-') )
                                    r_index++;
                        }
                        r_operand = result.substring(index+1, r_index+1);

                        if ( (f_index != 0) && (r_index != result.length()-1) )
                        {
                            if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') Err_lve = 4;             //If an answer is not a number return error
                            if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') Err_lve = 5;             //If an answer is Infinity return error

                            result = result.substring(0, f_index) + Algorithm(cha, f_operand, r_operand) + result.substring(r_index+1);
                            index = 0;
                        }
                            else if ( (f_index == 0) && (r_index == result.length()-1) )
                            {
                                if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') Err_lve = 4;         //If an answer is not a number return error
                                if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') Err_lve = 5;         //If an answer is Infinity return error

                                result = Algorithm(cha, f_operand, r_operand);
                            }
                                else if (f_index == 0)
                                {
                                    if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') Err_lve = 4;     //If an answer is not a number return error
                                    if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') Err_lve = 5;     //If an answer is Infinity return error

                                    result = Algorithm(cha, f_operand, r_operand) + result.substring(r_index+1);
                                    index = 0;
                                }
                                    else if (r_index == result.length()-1)
                                    {
                                        if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') Err_lve = 4; //If an answer is not a number return error
                                        if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') Err_lve = 5; //If an answer is Infinity return error

                                        result = result.substring(0, f_index) + Algorithm(cha, f_operand, r_operand);
                                    }
                    }
                }
            }//End of implement multiply and divide


            //Implement add and subtract
            for (index = 0; index < result.length() && (Err_lve == 0); index++)
            {
                if (index == 0 && result.charAt(index) == '-') index = 1;

                if (index > 0)
                    if ( ((result.charAt(index)) == 'E') && ((result.charAt(index+1)) == '-') )
                        index = index + 2;

                cha = result.charAt(index);

                if (cha == '+' || cha == '-')
                {
                    if ( !(Character.isDigit(result.charAt(index-1))) || ( !(Character.isDigit(result.charAt(index+1))) && (result.charAt(index+1) != '-') ) ) Err_lve = 3;
                    if (result.charAt(index+1) == '-')
                        if ( !(Character.isDigit(result.charAt(index+2))) ) Err_lve = 3;
                    if (Err_lve == 0)
                    {
                        f_index = 0;
                        f_operand = result.substring(f_index, index);

                        r_index = index + 1;
                        while ( (r_index < result.length()-1) && ((Character.isDigit(result.charAt(r_index+1))) || ((result.charAt(r_index+1)) == '.') || ((result.charAt(r_index+1)) == 'E')) )
                        {
                            r_index++;
                            if (r_index < result.length()-2)
                                if ( ((result.charAt(r_index)) == 'E') && ((result.charAt(r_index+1)) == '-') )
                                    r_index++;
                        }
                        r_operand = result.substring(index+1, r_index+1);
                        result = Algorithm(cha, f_operand, r_operand) + result.substring(r_index+1);
                        index = 0;
                    }
                }
            }//End of implement add and subtract

        }//End of implement algorithm

        }
        return result;
    }


    /*
    Method:     Algorithm
    Purpose:    Implement the simple expression
    Parameters: Operator, front value and rear value
    Returns:    None
    */

    private String Algorithm(char Operator, String F_operand, String R_operand)
    {
        Double  F, R;
        double  f, r,
            ans = 0;
        String  res;

        F = new Double(F_operand);
        R = new Double(R_operand);
        f = F.doubleValue();
        r = R.doubleValue();
                //double angle = r * 2.0 * Math.PI/360.0;

        if (Operator == '+') ans = f + r;
        if (Operator == '-') ans = f - r;
        if (Operator == '*') ans = f * r;
        if (Operator == '/') ans = f / r;
              //  if (Operator == 'S') ans = Math.sin(angle);
        res = Double.toString(ans);

        return res;
    }
/*private String Algorithm2(String Operator, String Operand)
    {
    Double F;
    double f=0;
   double answ=0;
    String result;
    F = new Double(Operand);
    double angle = f * 2.0 * Math.PI/360.0;
    if (Operator=="S") answ = Math.sin(angle);
        result = Double.toString(answ);

        return result;
}
*/
    /*
    Method:     Err_msg
    Purpose:    Prompt error message
    Parameters: Error level
    Returns:    None
    */

    private void Err_msg(int Err_lve)
    {
        switch(Err_lve)
        {
            case 1:
                JPanel.setText("Parentheses do not match");
                break;
            case 2:
                JPanel.setText("Invalid input");
                break;
            case 3:
                JPanel.setText("Invalid expression");
                break;
            case 4:
                JPanel.setText("Not a number exist");
                break;
            case 5:
                JPanel.setText("Infinity exist");
                break;
            default:
                JPanel.setText("Unknow error");
                break;
        }
    }
}
/*... End of Calculator.java ...*/

Please edit the post and place [code] [/code] tags around the code so that the formatting is preserved and it is readable.

I didn't read all of your code but I did notice these 2 lines: if (Operator == 'S') Correct because Operator is char if (Operator=="S") It won't behave the way you want it because:

The "S" is a String object. The Operator is a different String object. They might have the same value but they are not the SAME object, so the '==' will never return true.

99% of the cases you will need to do this: if (Operator.equals("S")) The '==' will work only like this:

String a  = "s";
String b = a;
(b==a) //true
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.