calculator

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 );
}
}

pgmtklAsked:
Who is Participating?
 
mukundha_expertCommented:
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
 
CEHJCommented:
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
 
CEHJCommented:
Correction - some things got commented out there that shouldn't have - please check
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
sriki77Commented:
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
 
pgmtklAuthor Commented:
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
 
CEHJCommented:
Your code will run under my corrections. Don't worry about what i said about commenting out
0
 
pgmtklAuthor Commented:
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
 
objectsCommented:
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
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.