Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

calculator

Posted on 2006-05-27
9
Medium Priority
?
337 Views
Last Modified: 2010-03-31
I am trying to get the below program to work. I have added the button listeners and the program compiles. It does not work, i.e show numbers pressed or calculate. Can someone see what is missing???


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

public class GuiCalculator extends JFrame
{

//JPanel button = new JPanel();
JButton button = new JButton();
//JPanel CalButton = new JPanel();
//JButton CalButton= new JButton;

JPanel row10=new JPanel();
JTextField display = new JTextField(10);

JPanel row20= new JPanel();
JTextField display2= new JTextField(10);

JPanel row30=new JPanel();
JButton button1 =button("1");
JButton button2 = button("2");
JButton button3 = button("3");
JButton buttonfuncDiv= button("/");
//JButton button1=new JButton("1");
//JButton button2=new JButton("2");
//JButton button3=new JButton("3");
//JButton buttonfuncDiv=new JButton("/");

JPanel row40=new JPanel();
JButton button4= button("4");
JButton button5= button("5");
JButton button6= button("6");
JButton buttonfuncMul= button("*");

JPanel row50=new JPanel();
JButton button7 = button("7");
JButton button8 = button("8");
JButton button9 = button("9");
JButton buttonfuncSub= button("-");
//JButton button7=new JButton("7");
//JButton button8=new JButton("8");
//JButton button9=new JButton("9");
//JButton buttonfuncSub=new JButton("-");

JPanel row60=new JPanel();
JButton button0 = button("0");
JButton buttonfuncPer = button(".");
JButton buttonfuncAdd = button("+");
JButton buttonfuncEql=new JButton("=");
//JButton button0=new JButton("0");
//JButton buttonfuncPer=new JButton(".");
//JButton buttonfuncAdd=new JButton("+");
//JButton buttonfuncEql=new JButton("=");

boolean button_flag=true;
boolean opflag=true;

String total="";
String lastCalc="=";

public GuiCalculator()
{

super("Calculator");
setSize(250,250);
setLocation(250,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout layout=new FlowLayout();
Container pane=getContentPane();
pane.setLayout(layout);


FlowLayout  flow10= new FlowLayout();
row10.setLayout(flow10);
row10.add(display);
pane.add(row10);

FlowLayout  flow20= new FlowLayout();
row20.setLayout(flow20);
row10.add(display2);
pane.add(row20);

//button1 = button("1");
//button2 = button("2");
//button3 = button("3");
//buttonfuncDiv= button("/");
GridLayout flow30=new GridLayout(1,4,5,1);
row30.setLayout(flow30);
row30.add(button1);
row30.add(button2);
row30.add(button3);
row30.add(buttonfuncDiv);
pane.add(row30);

//button4 = button("4");
//button5 = button("5");
//button6 = button("6");
//buttonfuncMul=new JButton("*");
GridLayout flow40=new GridLayout(1,4,5,1);
row40.setLayout(flow40);
row40.add(button4);
row40.add(button5);
row40.add(button6);
row40.add(buttonfuncMul);
pane.add(row40);

//button7 = button("7");
//button8 = button("8");
//button9 = button("9");
//buttonfuncSub=new JButton("-");
GridLayout flow50=new GridLayout(1,4,5,1);
row50.setLayout(flow50);
row50.add(button7);
row50.add(button8);
row50.add(button9);
row50.add(buttonfuncSub);
pane.add(row50);

//button0 = button("0");
//buttonfuncPer = button(".");
//buttonfuncAdd = button("+");
//buttonfuncEql=new JButton("=");
GridLayout flow60=new GridLayout(1,4,5,1);
row60.setLayout(flow60);
row60.add(button0);
row60.add(buttonfuncPer);
row60.add(buttonfuncAdd);
row60.add(buttonfuncEql);
pane.add(row60);


//getContentPane().add( CalButton);//display);
getContentPane().add( button);

display.setEditable(false);
}



public JButton button(final String number)
{
JButton numButton = new JButton(number);


class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
if(button_flag)
{
display.setText(number);
}
else
{
display.setText(display.getText() + number);
}

button_flag=false;
}
}

ButtonListener buttonlistener = new ButtonListener();
button1.addActionListener(buttonlistener);
button2.addActionListener(buttonlistener);
button3.addActionListener(buttonlistener);
button4.addActionListener(buttonlistener);
button5.addActionListener(buttonlistener);
button6.addActionListener(buttonlistener);
button7.addActionListener(buttonlistener);
button8.addActionListener(buttonlistener);
button9.addActionListener(buttonlistener);
button0.addActionListener(buttonlistener);
buttonfuncDiv.addActionListener(buttonlistener);
buttonfuncMul.addActionListener(buttonlistener);
buttonfuncPer.addActionListener(buttonlistener);
buttonfuncAdd.addActionListener(buttonlistener);
buttonfuncEql.addActionListener(buttonlistener);

return numButton;

}

public JButton makenopButton(final String number)
{
JButton CalButton = new JButton(number);

class ButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent actionevent)
{
if(opflag)
{

button_flag=true;
total = display.getText();
lastCalc= number;
opflag=false;


}
else
{

calculate();
lastCalc= number;

display.setText(total);
opflag=false;
button_flag=true;
}

}


}


ButtonListener buttonlistener2 = new ButtonListener();
CalButton.addActionListener(buttonlistener2);
/*button1.addActionListener(buttonlistener2);
button2.addActionListener(buttonlistener2);
button3.addActionListener(buttonlistener2);
button4.addActionListener(buttonlistener2);
button5.addActionListener(buttonlistener2);
button6.addActionListener(buttonlistener2);
button7.addActionListener(buttonlistener2);
button8.addActionListener(buttonlistener2);
button9.addActionListener(buttonlistener2);
button0.addActionListener(buttonlistener2);
buttonfuncDiv.addActionListener(buttonlistener2);
buttonfuncMul.addActionListener(buttonlistener2);
buttonfuncPer.addActionListener(buttonlistener2);
buttonfuncAdd.addActionListener(buttonlistener2);
buttonfuncEql.addActionListener(buttonlistener2);
*/
return CalButton;


}


public void calculate()
{
if(lastCalc.equals("+"))
      {
      double num1 = Double.parseDouble(total);
      double num2 = Double.parseDouble(display.getText());
      double temp=num1+num2;
      total = Double.toString(temp);

      }
else if(lastCalc.equals("-"))
      {
      double num1 = Double.parseDouble(total);
      double num2 = Double.parseDouble(display.getText());
      double temp=num1-num2;
      total = Double.toString(temp);


      }
else if(lastCalc.equals("*"))
      {
      double num1 = Double.parseDouble(total);
      double num2 = Double.parseDouble(display.getText());
      double temp=num1*num2;
      total = Double.toString(temp);


      }
else if(lastCalc.equals("/"))
      {
      double num1 = Double.parseDouble(total);
      double num2 = Double.parseDouble(display.getText());
      double temp=num1/num2;
      total = Double.toString(temp);


      }

else if(lastCalc.equals("="))
      {
display.setText(total);
opflag=true;

      }

}

public static void main ( String[] args )
{

CalculatorFrame cal = new CalculatorFrame();
cal.setVisible( true );

      }

}

class Close extends WindowAdapter
{
public void closeWindow( WindowEvent e )
{
System.exit( 0 );
}
}

0
Comment
Question by:pgmtkl
9 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 16775811
SOme of your JButton ctors were wrong:


/**
 * 27-May-2006
 */


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class GuiCalculator extends JFrame {

      // JPanel button = new JPanel();
      JButton button = new JButton();

      // JPanel CalButton = new JPanel();
      // JButton CalButton= new JButton;

      JPanel row10 = new JPanel();

      JTextField display = new JTextField(10);

      JPanel row20 = new JPanel();

      JTextField display2 = new JTextField(10);

      JPanel row30 = new JPanel();

      JButton button1 = new JButton("1");

      JButton button2 = new JButton("2");

      JButton button3 = new JButton("3");

      JButton buttonfuncDiv = new JButton("/");

      // JButton button1=new JButton("1");
      // JButton button2=new JButton("2");
      // JButton button3=new JButton("3");
      // JButton buttonfuncDiv=new JButton("/");

      JPanel row40 = new JPanel();

      JButton button4 = new JButton("4");

      JButton button5 = new JButton("5");

      JButton button6 = new JButton("6");

      JButton buttonfuncMul = new JButton("*");

      JPanel row50 = new JPanel();

      JButton button7 = new JButton("7");

      JButton button8 = new JButton("8");

      JButton button9 = new JButton("9");

      JButton buttonfuncSub = new JButton("-");

      // JButton button7=new JButton("7");
      // JButton button8=new JButton("8");
      // JButton button9=new JButton("9");
      // JButton buttonfuncSub=new JButton("-");

      JPanel row60 = new JPanel();

      JButton button0 = new JButton("0");

      JButton buttonfuncPer = new JButton(".");

      JButton buttonfuncAdd = new JButton("+");

      JButton buttonfuncEql = new JButton("=");

      // JButton button0=new JButton("0");
      // JButton buttonfuncPer=new JButton(".");
      // JButton buttonfuncAdd=new JButton("+");
      // JButton buttonfuncEql=new JButton("=");

      boolean button_flag = true;

      boolean opflag = true;

      String total = "";

      String lastCalc = "=";

      public GuiCalculator() {

            super("Calculator");
            setSize(250, 250);
            setLocation(250, 60);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FlowLayout layout = new FlowLayout();
            Container pane = getContentPane();
            pane.setLayout(layout);

            FlowLayout flow10 = new FlowLayout();
            row10.setLayout(flow10);
            row10.add(display);
            pane.add(row10);

            FlowLayout flow20 = new FlowLayout();
            row20.setLayout(flow20);
            row10.add(display2);
            pane.add(row20);

            // button1 = new JButton("1");
            // button2 = new JButton("2");
            // button3 = new JButton("3");
            // buttonfuncDiv= new JButton("/");
            GridLayout flow30 = new GridLayout(1, 4, 5, 1);
            row30.setLayout(flow30);
            row30.add(button1);
            row30.add(button2);
            row30.add(button3);
            row30.add(buttonfuncDiv);
            pane.add(row30);

            // button4 = new JButton("4");
            // button5 = new JButton("5");
            // button6 = new JButton("6");
            // buttonfuncMul=new JButton("*");
            GridLayout flow40 = new GridLayout(1, 4, 5, 1);
            row40.setLayout(flow40);
            row40.add(button4);
            row40.add(button5);
            row40.add(button6);
            row40.add(buttonfuncMul);
            pane.add(row40);

            // button7 = new JButton("7");
            // button8 = new JButton("8");
            // button9 = new JButton("9");
            // buttonfuncSub=new JButton("-");
            GridLayout flow50 = new GridLayout(1, 4, 5, 1);
            row50.setLayout(flow50);
            row50.add(button7);
            row50.add(button8);
            row50.add(button9);
            row50.add(buttonfuncSub);
            pane.add(row50);

            // button0 = new JButton("0");
            // buttonfuncPer = new JButton(".");
            // buttonfuncAdd = new JButton("+");
            // buttonfuncEql=new JButton("=");
            GridLayout flow60 = new GridLayout(1, 4, 5, 1);
            row60.setLayout(flow60);
            row60.add(button0);
            row60.add(buttonfuncPer);
            row60.add(buttonfuncAdd);
            row60.add(buttonfuncEql);
            pane.add(row60);

            // getContentPane().add( CalButton);//display);
            getContentPane().add(button);

            display.setEditable(false);
      }

      public JButton button(final String number) {
            JButton numButton = new JButton(number);

            class ButtonListener implements ActionListener {
                  public void actionPerformed(ActionEvent actionevent) {
                        if (button_flag) {
                              display.setText(number);
                        } else {
                              display.setText(display.getText() + number);
                        }

                        button_flag = false;
                  }
            }

            ButtonListener buttonlistener = new ButtonListener();
            button1.addActionListener(buttonlistener);
            button2.addActionListener(buttonlistener);
            button3.addActionListener(buttonlistener);
            button4.addActionListener(buttonlistener);
            button5.addActionListener(buttonlistener);
            button6.addActionListener(buttonlistener);
            button7.addActionListener(buttonlistener);
            button8.addActionListener(buttonlistener);
            button9.addActionListener(buttonlistener);
            button0.addActionListener(buttonlistener);
            buttonfuncDiv.addActionListener(buttonlistener);
            buttonfuncMul.addActionListener(buttonlistener);
            buttonfuncPer.addActionListener(buttonlistener);
            buttonfuncAdd.addActionListener(buttonlistener);
            buttonfuncEql.addActionListener(buttonlistener);

            return numButton;

      }

      public JButton makenopButton(final String number) {
            JButton CalButton = new JButton(number);

            class ButtonListener implements ActionListener {

                  public void actionPerformed(ActionEvent actionevent) {
                        if (opflag) {

                              button_flag = true;
                              total = display.getText();
                              lastCalc = number;
                              opflag = false;

                        } else {

                              calculate();
                              lastCalc = number;

                              display.setText(total);
                              opflag = false;
                              button_flag = true;
                        }

                  }

            }

            ButtonListener buttonlistener2 = new ButtonListener();
            CalButton.addActionListener(buttonlistener2);
            /*
             * button1.addActionListener(buttonlistener2);
             * button2.addActionListener(buttonlistener2);
             * button3.addActionListener(buttonlistener2);
             * button4.addActionListener(buttonlistener2);
             * button5.addActionListener(buttonlistener2);
             * button6.addActionListener(buttonlistener2);
             * button7.addActionListener(buttonlistener2);
             * button8.addActionListener(buttonlistener2);
             * button9.addActionListener(buttonlistener2);
             * button0.addActionListener(buttonlistener2);
             * buttonfuncDiv.addActionListener(buttonlistener2);
             * buttonfuncMul.addActionListener(buttonlistener2);
             * buttonfuncPer.addActionListener(buttonlistener2);
             * buttonfuncAdd.addActionListener(buttonlistener2);
             * buttonfuncEql.addActionListener(buttonlistener2);
             */
            return CalButton;

      }

      public void calculate() {
            if (lastCalc.equals("+")) {
                  double num1 = Double.parseDouble(total);
                  double num2 = Double.parseDouble(display.getText());
                  double temp = num1 + num2;
                  total = Double.toString(temp);

            } else if (lastCalc.equals("-")) {
                  double num1 = Double.parseDouble(total);
                  double num2 = Double.parseDouble(display.getText());
                  double temp = num1 - num2;
                  total = Double.toString(temp);

            } else if (lastCalc.equals("*")) {
                  double num1 = Double.parseDouble(total);
                  double num2 = Double.parseDouble(display.getText());
                  double temp = num1 * num2;
                  total = Double.toString(temp);

            } else if (lastCalc.equals("/")) {
                  double num1 = Double.parseDouble(total);
                  double num2 = Double.parseDouble(display.getText());
                  double temp = num1 / num2;
                  total = Double.toString(temp);

            }

            else if (lastCalc.equals("=")) {
                  display.setText(total);
                  opflag = true;

            }

      }

      public static void main(String[] args) {
            GuiCalculator cal = new GuiCalculator();
            cal.setVisible(true);
      }

}

class Close extends WindowAdapter {
      public void closeWindow(WindowEvent e) {
            System.exit(0);
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16775816
Correction - some things got commented out there that shouldn't have - please check
0
 
LVL 1

Expert Comment

by:sriki77
ID: 16775827
First thing which is problem is that your "button" function changes the action listeners of all the buttons. when you invoke  button("4"), it must create and a button listener to the new button being created..not just alter all the listeners of all the buttons
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:pgmtkl
ID: 16775897
which button function? i have all the buttons associated with the action performed. I am new to java so it is still confusing to me.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16775911
Your code will run under my corrections. Don't worry about what i said about commenting out
0
 

Author Comment

by:pgmtkl
ID: 16775926
thanks it does complile. But the numbers that are pressed are not showing. I have the main set to visible.  is it working on your compiler? Should it be something else since nothing is appearing on the screen?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16777290
should look more like this:

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

public class GuiCalculator extends JFrame
{

//JPanel button = new JPanel();
JButton button = new JButton();
//JPanel CalButton = new JPanel();
//JButton CalButton= new JButton;

JPanel row10=new JPanel();
JTextField display = new JTextField(10);

JPanel row20= new JPanel();
JTextField display2= new JTextField(10);

JPanel row30=new JPanel();
JButton button1;
JButton button2;
JButton button3;
JButton buttonfuncDiv= button("/");
//JButton button1=new JButton("1");
//JButton button2=new JButton("2");
//JButton button3=new JButton("3");
//JButton buttonfuncDiv=new JButton("/");

JPanel row40=new JPanel();
JButton button4;
JButton button5= button("5");
JButton button6= button("6");
JButton buttonfuncMul= button("*");

JPanel row50=new JPanel();
JButton button7 = button("7");
JButton button8 = button("8");
JButton button9 = button("9");
JButton buttonfuncSub= button("-");
//JButton button7=new JButton("7");
//JButton button8=new JButton("8");
//JButton button9=new JButton("9");
//JButton buttonfuncSub=new JButton("-");

JPanel row60=new JPanel();
JButton button0 = button("0");
JButton buttonfuncPer = button(".");
JButton buttonfuncAdd = button("+");
JButton buttonfuncEql=new JButton("=");
//JButton button0=new JButton("0");
//JButton buttonfuncPer=new JButton(".");
//JButton buttonfuncAdd=new JButton("+");
//JButton buttonfuncEql=new JButton("=");

boolean button_flag=true;
boolean opflag=true;

String total="";
String lastCalc="=";

public GuiCalculator()
{

super("Calculator");
setSize(250,250);
setLocation(250,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout layout=new FlowLayout();
Container pane=getContentPane();
pane.setLayout(layout);


FlowLayout  flow10= new FlowLayout();
row10.setLayout(flow10);
row10.add(display);
pane.add(row10);

FlowLayout  flow20= new FlowLayout();
row20.setLayout(flow20);
row10.add(display2);
pane.add(row20);

//button1 = button("1");
//button2 = button("2");
//button3 = button("3");
//buttonfuncDiv= button("/");
GridLayout flow30=new GridLayout(1,4,5,1);
row30.setLayout(flow30);
row30.add(button1 = button("1"));
row30.add(button2 = button("2"));
row30.add(button3 = button("3"));
row30.add(buttonfuncDiv);
pane.add(row30);

//button4 = button("4");
//button5 = button("5");
//button6 = button("6");
//buttonfuncMul=new JButton("*");
GridLayout flow40=new GridLayout(1,4,5,1);
row40.setLayout(flow40);
row40.add(button4 = button("4"));
row40.add(button5 = button("5"));
row40.add(button6 = button("6"));
row40.add(buttonfuncMul);
pane.add(row40);

//button7 = button("7");
//button8 = button("8");
//button9 = button("9");
//buttonfuncSub=new JButton("-");
GridLayout flow50=new GridLayout(1,4,5,1);
row50.setLayout(flow50);
row50.add(button7 = button("7"));
row50.add(button8 = button("8"));
row50.add(button9 = button("9"));
row50.add(buttonfuncSub);
pane.add(row50);

//button0 = button("0");
//buttonfuncPer = button(".");
//buttonfuncAdd = button("+");
//buttonfuncEql=new JButton("=");
GridLayout flow60=new GridLayout(1,4,5,1);
row60.setLayout(flow60);
row60.add(button0);
row60.add(buttonfuncPer);
row60.add(buttonfuncAdd);
row60.add(buttonfuncEql);
pane.add(row60);


//getContentPane().add( CalButton);//display);
getContentPane().add( button);

display.setEditable(false);
}



public JButton button(final String number)
{
JButton numButton = new JButton(number);


class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
    JButton button = (JButton) actionevent.getSource();
    String number = button.getText();
if(button_flag)
{
display.setText(number);
}
else
{
display.setText(display.getText() + number);
}

button_flag=false;
}
}

ButtonListener buttonlistener = new ButtonListener();
numButton.addActionListener(buttonlistener);

return numButton;

}

public JButton makenopButton(final String number)
{
JButton CalButton = new JButton(number);

class ButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent actionevent)
{
if(opflag)
{

button_flag=true;
total = display.getText();
lastCalc= number;
opflag=false;


}
else
{

calculate();
lastCalc= number;

display.setText(total);
opflag=false;
button_flag=true;
}

}


}


ButtonListener buttonlistener2 = new ButtonListener();
CalButton.addActionListener(buttonlistener2);
/*button1.addActionListener(buttonlistener2);
button2.addActionListener(buttonlistener2);
button3.addActionListener(buttonlistener2);
button4.addActionListener(buttonlistener2);
button5.addActionListener(buttonlistener2);
button6.addActionListener(buttonlistener2);
button7.addActionListener(buttonlistener2);
button8.addActionListener(buttonlistener2);
button9.addActionListener(buttonlistener2);
button0.addActionListener(buttonlistener2);
buttonfuncDiv.addActionListener(buttonlistener2);
buttonfuncMul.addActionListener(buttonlistener2);
buttonfuncPer.addActionListener(buttonlistener2);
buttonfuncAdd.addActionListener(buttonlistener2);
buttonfuncEql.addActionListener(buttonlistener2);
*/
return CalButton;


}


public void calculate()
{
if(lastCalc.equals("+"))
     {
     double num1 = Double.parseDouble(total);
     double num2 = Double.parseDouble(display.getText());
     double temp=num1+num2;
     total = Double.toString(temp);

     }
else if(lastCalc.equals("-"))
     {
     double num1 = Double.parseDouble(total);
     double num2 = Double.parseDouble(display.getText());
     double temp=num1-num2;
     total = Double.toString(temp);


     }
else if(lastCalc.equals("*"))
     {
     double num1 = Double.parseDouble(total);
     double num2 = Double.parseDouble(display.getText());
     double temp=num1*num2;
     total = Double.toString(temp);


     }
else if(lastCalc.equals("/"))
     {
     double num1 = Double.parseDouble(total);
     double num2 = Double.parseDouble(display.getText());
     double temp=num1/num2;
     total = Double.toString(temp);


     }

else if(lastCalc.equals("="))
     {
display.setText(total);
opflag=true;

     }

}

public static void main ( String[] args )
{

    GuiCalculator cal = new GuiCalculator();
cal.setVisible( true );

     }

}

class Close extends WindowAdapter
{
public void closeWindow( WindowEvent e )
{
System.exit( 0 );
}
}


0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 1000 total points
ID: 16779184
Check out this COde. This works fine


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

public class GuiCalculator extends JFrame
{
      private static final long serialVersionUID = 1L;
      //JPanel button = new JPanel();
      JButton button = new JButton();
      //JPanel CalButton = new JPanel();
      //JButton CalButton= new JButton;
      
      JPanel row10=new JPanel();
      JTextField display = new JTextField(10);
      
      JPanel row20= new JPanel();
      JTextField display2= new JTextField(10);
      
      JPanel row30=new JPanel();
      JButton button1 =button("1");
      JButton button2 = button("2");
      JButton button3 = button("3");
      JButton buttonfuncDiv= button("/");
      //JButton button1=new JButton("1");
      //JButton button2=new JButton("2");
      //JButton button3=new JButton("3");
      //JButton buttonfuncDiv=new JButton("/");
      
      JPanel row40=new JPanel();
      JButton button4= button("4");
      JButton button5= button("5");
      JButton button6= button("6");
      JButton buttonfuncMul= button("*");
      
      JPanel row50=new JPanel();
      JButton button7 = button("7");
      JButton button8 = button("8");
      JButton button9 = button("9");
      JButton buttonfuncSub= button("-");
      //JButton button7=new JButton("7");
      //JButton button8=new JButton("8");
      //JButton button9=new JButton("9");
      //JButton buttonfuncSub=new JButton("-");
      
      JPanel row60=new JPanel();
      JButton button0 = button("0");
      JButton buttonfuncPer = button(".");
      JButton buttonfuncAdd = button("+");
      JButton buttonfuncEql=new JButton("=");
      //JButton button0=new JButton("0");
      //JButton buttonfuncPer=new JButton(".");
      //JButton buttonfuncAdd=new JButton("+");
      //JButton buttonfuncEql=new JButton("=");
      
      boolean button_flag=true;
      boolean opflag=true;
      
      String total="";
      String lastCalc="=";

      public GuiCalculator()
      {
      
            super("Calculator");
            System.out.println("In COnstructor");
            setSize(250,250);
            setLocation(250,60);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FlowLayout layout=new FlowLayout();
            Container pane=getContentPane();
            pane.setLayout(layout);
            
            
            FlowLayout  flow10= new FlowLayout();
            row10.setLayout(flow10);
            row10.add(display);
            pane.add(row10);
            
            FlowLayout  flow20= new FlowLayout();
            row20.setLayout(flow20);
            row10.add(display2);
            pane.add(row20);
            
            //button1 = button("1");
            //button2 = button("2");
            //button3 = button("3");
            //buttonfuncDiv= button("/");
            GridLayout flow30=new GridLayout(1,4,5,1);
            row30.setLayout(flow30);
            row30.add(button1);
            row30.add(button2);
            row30.add(button3);
            row30.add(buttonfuncDiv);
            pane.add(row30);
            
            //button4 = button("4");
            //button5 = button("5");
            //button6 = button("6");
            //buttonfuncMul=new JButton("*");
            GridLayout flow40=new GridLayout(1,4,5,1);
            row40.setLayout(flow40);
            row40.add(button4);
            row40.add(button5);
            row40.add(button6);
            row40.add(buttonfuncMul);
            pane.add(row40);
            
            //button7 = button("7");
            //button8 = button("8");
            //button9 = button("9");
            //buttonfuncSub=new JButton("-");
            GridLayout flow50=new GridLayout(1,4,5,1);
            row50.setLayout(flow50);
            row50.add(button7);
            row50.add(button8);
            row50.add(button9);
            row50.add(buttonfuncSub);
            pane.add(row50);
            
            //button0 = button("0");
            //buttonfuncPer = button(".");
            //buttonfuncAdd = button("+");
            //buttonfuncEql=new JButton("=");
            GridLayout flow60=new GridLayout(1,4,5,1);
            row60.setLayout(flow60);
            row60.add(button0);
            row60.add(buttonfuncPer);
            row60.add(buttonfuncAdd);
            row60.add(buttonfuncEql);
            pane.add(row60);
            
            
            
            button1.addActionListener(new ButtonListener());
            button2.addActionListener(new ButtonListener());
            button3.addActionListener(new ButtonListener());
            button4.addActionListener(new ButtonListener());
            button5.addActionListener(new ButtonListener());
            button6.addActionListener(new ButtonListener());
            button7.addActionListener(new ButtonListener());
            button8.addActionListener(new ButtonListener());
            button9.addActionListener(new ButtonListener());
            button0.addActionListener(new ButtonListener());
            buttonfuncDiv.addActionListener(new ButtonListener());
            buttonfuncMul.addActionListener(new ButtonListener());
            buttonfuncPer.addActionListener(new ButtonListener());
            buttonfuncAdd.addActionListener(new ButtonListener());
            buttonfuncEql.addActionListener(new ButtonListener());
            buttonfuncSub.addActionListener(new ButtonListener());
            //getContentPane().add( CalButton);//display);
            getContentPane().add( button);
            
            display.setEditable(false);
            System.out.println("Constructor Returned");
      }



class ButtonListener implements ActionListener
{
      public void actionPerformed(ActionEvent actionevent)
      {
            String number="";
            
            if(actionevent.getSource()==button1){
                  number="1";                  
            }
            if(actionevent.getSource()==button2){
                  number="2";            
            }
            if(actionevent.getSource()==button3){
                  number="3";                  
            }
            if(actionevent.getSource()==button4){
                  number="4";            
            }
            if(actionevent.getSource()==button5){
                  number="5";                  
            }
            if(actionevent.getSource()==button6){
                  number="6";            
            }
            if(actionevent.getSource()==button7){
                  number="7";                  
            }
            if(actionevent.getSource()==button8){
                  number="8";            
            }
            if(actionevent.getSource()==button9){
                  number="9";            
            }
            if(actionevent.getSource()==button0){
                  number="0";                  
            }
            if(actionevent.getSource()==buttonfuncPer){
                  number=".";                  
            }
            if(actionevent.getSource()==buttonfuncAdd){
                  button_flag=true;
                  
                  if(opflag){
                        opflag=false;
                        total = display.getText();
                        lastCalc = "+";
                        return;
                        }
                  else{
                        opflag=false;
                        calculate();
                        lastCalc = "+";
                        display2.setText(total);
                        return;
                  }
                  
            }

            if(actionevent.getSource()==buttonfuncSub){
                  button_flag=true;
                  
                  if(opflag){
                        opflag=false;
                        lastCalc = "-";
                        total = display.getText();
                        return;
                  }
                  else{
                        opflag=false;
                        calculate();
                        lastCalc = "-";
                        display2.setText(total);
                        return;
                  }
                  
            }
            
            if(actionevent.getSource()==buttonfuncMul){
                  button_flag=true;
                  
                  if(opflag){
                        opflag=false;
                        total = display.getText();
                        lastCalc = "*";
                        return;
                        }
                  else{
                        opflag=false;
                        calculate();
                        lastCalc = "*";
                        display2.setText(total);
                        return;
                  }
            }

            if(actionevent.getSource()==buttonfuncDiv){
                  button_flag=true;
                  
                  if(opflag){
                        opflag=false;
                        total = display.getText();
                        lastCalc = "/";
                        return;
                        }
                  else{
                        opflag=false;
                        calculate();
                        lastCalc = "/";
                        display2.setText(total);
                        return;
                  }
            }

            if(actionevent.getSource()==buttonfuncEql){
                  button_flag=true;
                  if(!opflag)
                  {
                        calculate();
                        lastCalc="=";
                        display2.setText(total);
                  }
                  else
                  {
                        display2.setText(display.getText());
                  }
                  opflag=true;
                  return;
            }
            
            if(button_flag)
            {
                  display.setText(number);
            }
            else
            {
                  display.setText(display.getText() + number);
            }
      
            button_flag=false;
      }
}

      
      public JButton button(final String number)
      {
            JButton numButton = new JButton(number);
            return numButton;
      }



      public void calculate()
      {
      if(lastCalc.equals("+"))
           {
           double num1 = Double.parseDouble(total);
           double num2 = Double.parseDouble(display.getText());
           double temp=num1+num2;
           total = Double.toString(temp);
      
           }
      else if(lastCalc.equals("-"))
           {
           double num1 = Double.parseDouble(total);
           double num2 = Double.parseDouble(display.getText());
           double temp=num1-num2;
           total = Double.toString(temp);
      
      
           }
      else if(lastCalc.equals("*"))
           {
           double num1 = Double.parseDouble(total);
           double num2 = Double.parseDouble(display.getText());
           double temp=num1*num2;
           total = Double.toString(temp);
      
      
           }
      else if(lastCalc.equals("/"))
           {
           double num1 = Double.parseDouble(total);
           double num2 = Double.parseDouble(display.getText());
           double temp=num1/num2;
           total = Double.toString(temp);
      
      
           }
      
      else if(lastCalc.equals("="))
           {
            display.setText(total);
            opflag=true;
           }
      
      }

      public static void main ( String[] args )
      {
            GuiCalculator cal = new GuiCalculator();
            cal.setVisible( true );
     }
}
      
class Close extends WindowAdapter
{
      public void closeWindow( WindowEvent e )
      {
      System.exit( 0 );
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16779324
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question