Results 1 to 4 of 4
  1. #1
    Rose88 is offline Member
    Join Date
    Apr 2009
    Posts
    19
    Rep Power
    0

    Default Basic Calculator

    my homework is to build a calculator with these conditions:
    1. to add blue and red buttons and color the panel.
    2. add a + button which calculates numbers .
    3. add = button which displaying the result.
    4. add C button which clears the result field.
    5. when u press X the window closes and write a suitable message.

    my problem was when i'm doing 1+2+3+4 it only counts 3+4 and forgets the first numbers and counts just the last two. my code is like this:

    /**
    * JFarme package
    */
    package org.eclipse.swingapp;

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

    import java.awt.Color;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class Calculator extends JFrame implements ActionListener{

    private static final long serialVersionUID = 1L;

    private final int INPUT_MODE = 0;
    private final int RESULT_MODE = 1;
    private int displayMode;
    private boolean clearOnNextDigit=true;
    private int lastNumber,result;
    private String lastOperator; // @jve:decl-index=0:

    private JPanel jContentPane = null;

    private JButton but1 = null;

    private JButton but2 = null;

    private JButton but3 = null;

    private JButton but4 = null;

    private JButton but5 = null;

    private JButton but6 = null;

    private JButton but7 = null;

    private JButton but8 = null;

    private JButton but9 = null;

    private JButton but0 = null;

    private JButton butPlus = null;

    private JButton butEqual = null;

    private JTextField jTextField = null;

    private JButton butC = null;

    private JButton ButRed = null;

    private JButton butBlue = null;

    /**
    * This is the default constructor
    */
    public Calculator() {
    super();
    initialize();
    }

    /**
    * This method initializes this
    *
    * @return void
    */
    private void initialize() {
    this.setSize(300, 200);
    this.setContentPane(getJContentPane());
    this.setTitle("Calculator");
    }

    /**
    * This method initializes jContentPane
    *
    * @return javax.swing.JPanel
    */
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
    gridBagConstraints17.gridx = 4;
    gridBagConstraints17.gridy = 4;
    GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
    gridBagConstraints16.gridx = 4;
    gridBagConstraints16.gridy = 5;
    GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
    gridBagConstraints14.gridx = 1;
    gridBagConstraints14.gridy = 5;
    GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
    gridBagConstraints13.fill = GridBagConstraints.VERTICAL;
    gridBagConstraints13.gridy = 1;
    gridBagConstraints13.weightx = 1.0;
    gridBagConstraints13.insets = new Insets(0, 0, 2, 2);
    gridBagConstraints13.gridx = 4;
    GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
    gridBagConstraints11.gridx = 0;
    gridBagConstraints11.gridy = 4;
    GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
    gridBagConstraints10.gridx = 2;
    gridBagConstraints10.gridy = 4;
    GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
    gridBagConstraints9.gridx = 1;
    gridBagConstraints9.gridy = 4;
    GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
    gridBagConstraints8.gridx = 2;
    gridBagConstraints8.gridy = 3;
    GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
    gridBagConstraints7.gridx = 1;
    gridBagConstraints7.gridy = 3;
    GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
    gridBagConstraints6.gridx = 0;
    gridBagConstraints6.gridy = 3;
    GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
    gridBagConstraints5.gridx = 2;
    gridBagConstraints5.gridy = 1;
    GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
    gridBagConstraints4.gridx = 1;
    gridBagConstraints4.gridy = 1;
    GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
    gridBagConstraints3.gridx = 0;
    gridBagConstraints3.gridy = 1;
    GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
    gridBagConstraints2.gridx = 2;
    gridBagConstraints2.gridy = 0;
    GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
    gridBagConstraints1.gridx = 1;
    gridBagConstraints1.gridy = 0;
    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    jContentPane = new JPanel();
    jContentPane.setLayout(new GridBagLayout());
    jContentPane.add(getBut1(), gridBagConstraints);
    jContentPane.add(getBut2(), gridBagConstraints1);
    jContentPane.add(getBut3(), gridBagConstraints2);
    jContentPane.add(getBut4(), gridBagConstraints3);
    jContentPane.add(getBut5(), gridBagConstraints4);
    jContentPane.add(getBut6(), gridBagConstraints5);
    jContentPane.add(getBut7(), gridBagConstraints6);
    jContentPane.add(getBut8(), gridBagConstraints7);
    jContentPane.add(getBut9(), gridBagConstraints8);
    jContentPane.add(getBut0(), gridBagConstraints9);
    jContentPane.add(getButPlus(), gridBagConstraints10);
    jContentPane.add(getButEqual(), gridBagConstraints11);
    jContentPane.add(getJTextField(), gridBagConstraints13);
    jContentPane.add(getButC(), gridBagConstraints14);
    jContentPane.add(getButRed(), gridBagConstraints16);
    jContentPane.add(getButBlue(), gridBagConstraints17);
    this.ButRed.addActionListener(this);
    this.butBlue.addActionListener(this);
    this.butC.addActionListener(this);
    this.butPlus.addActionListener(this);
    this.butEqual.addActionListener(this);
    this.but0.addActionListener(this);
    this.but1.addActionListener(this);
    this.but2.addActionListener(this);
    this.but3.addActionListener(this);
    this.but4.addActionListener(this);
    this.but5.addActionListener(this);
    this.but6.addActionListener(this);
    this.but7.addActionListener(this);
    this.but8.addActionListener(this);
    this.but9.addActionListener(this);
    }
    return jContentPane;
    }

    /**
    * This method initializes but1
    *
    * @return javax.swing.JButton
    */
    private JButton getBut1() {
    if (but1 == null) {
    but1 = new JButton();
    but1.setText("1");
    }
    return but1;
    }

    /**
    * This method initializes but2
    *
    * @return javax.swing.JButton
    */
    private JButton getBut2() {
    if (but2 == null) {
    but2 = new JButton();
    but2.setText("2");
    }
    return but2;
    }

    /**
    * This method initializes but3
    *
    * @return javax.swing.JButton
    */
    private JButton getBut3() {
    if (but3 == null) {
    but3 = new JButton();
    but3.setText("3");
    }
    return but3;
    }

    /**
    * This method initializes but4
    *
    * @return javax.swing.JButton
    */
    private JButton getBut4() {
    if (but4 == null) {
    but4 = new JButton();
    but4.setText("4");
    }
    return but4;
    }

    /**
    * This method initializes but5
    *
    * @return javax.swing.JButton
    */
    private JButton getBut5() {
    if (but5 == null) {
    but5 = new JButton();
    but5.setText("5");
    }
    return but5;
    }

    /**
    * This method initializes but6
    *
    * @return javax.swing.JButton
    */
    private JButton getBut6() {
    if (but6 == null) {
    but6 = new JButton();
    but6.setText("6");
    }
    return but6;
    }

    /**
    * This method initializes but7
    *
    * @return javax.swing.JButton
    */
    private JButton getBut7() {
    if (but7 == null) {
    but7 = new JButton();
    but7.setText("7");
    }
    return but7;
    }

    /**
    * This method initializes but8
    *
    * @return javax.swing.JButton
    */
    private JButton getBut8() {
    if (but8 == null) {
    but8 = new JButton();
    but8.setText("8");
    }
    return but8;
    }

    /**
    * This method initializes but9
    *
    * @return javax.swing.JButton
    */
    private JButton getBut9() {
    if (but9 == null) {
    but9 = new JButton();
    but9.setText("9");
    }
    return but9;
    }

    /**
    * This method initializes but0
    *
    * @return javax.swing.JButton
    */
    private JButton getBut0() {
    if (but0 == null) {
    but0 = new JButton();
    but0.setText("0");
    }
    return but0;
    }

    /**
    * This method initializes butPlus
    *
    * @return javax.swing.JButton
    */
    private JButton getButPlus() {
    if (butPlus == null) {
    butPlus = new JButton();
    butPlus.setText("+");
    }
    return butPlus;
    }

    /**
    * This method initializes butEqual
    *
    * @return javax.swing.JButton
    */
    private JButton getButEqual() {
    if (butEqual == null) {
    butEqual = new JButton();
    butEqual.setName("");
    butEqual.setText("=");
    }
    return butEqual;
    }

    /**
    * This method initializes jTextField
    *
    * @return javax.swing.JTextField
    */
    private JTextField getJTextField() {
    if (jTextField == null) {
    jTextField = new JTextField();
    jTextField.setPreferredSize(new Dimension(80, 20));
    }
    return jTextField;
    }

    /**
    * This method initializes butC
    *
    * @return javax.swing.JButton
    */
    private JButton getButC() {
    if (butC == null) {
    butC = new JButton();
    butC.setText("C");
    }
    return butC;
    }

    /**
    * This method initializes ButRed
    *
    * @return javax.swing.JButton
    */
    private JButton getButRed() {
    if (ButRed == null) {
    ButRed = new JButton();
    ButRed.setName("");
    ButRed.setPreferredSize(new Dimension(59, 26));
    ButRed.setText("Red");
    }
    return ButRed;
    }

    /**
    * This method initializes butBlue
    *
    * @return javax.swing.JButton
    */
    private JButton getButBlue() {
    if (butBlue == null) {
    butBlue = new JButton();
    butBlue.setText("Blue");
    }
    return butBlue;
    }
    /**
    * This method clears the result field
    *
    */
    private void clearAll() {
    setDisplayString("0");
    lastOperator = "0";
    lastNumber = 0;
    displayMode =0;
    result=0;
    clearOnNextDigit = true;
    }
    /**
    *
    * @param String s
    */
    private void setDisplayString(String s){
    this.jTextField.setText(s);
    }
    /**
    *
    * @return text
    */
    private String getDisplayString (){
    return this.jTextField.getText();
    }
    /**
    *
    * @param digit
    */
    private void addDigitToDisplay(int digit){
    if (clearOnNextDigit)
    setDisplayString("");

    String inputString = getDisplayString();
    setDisplayString(inputString+ digit);
    displayMode = INPUT_MODE;
    clearOnNextDigit = false;
    }
    /**
    *
    * @return int
    */
    private int getNumberInDisplay() {
    String input = this.jTextField.getText();
    return Integer.parseInt(input);
    }
    /**
    *
    * @return result
    */
    private int equalOperation(){
    result = lastNumber + getNumberInDisplay();
    lastOperator = "0";
    return result;
    }
    /**
    * Performe Action
    */
    public void actionPerformed(ActionEvent ev) {
    // TODO Auto-generated method stub
    String arg= ev.getActionCommand();
    if (arg.equals ("Red"))
    this.jContentPane.setBackground(Color.red);
    else if (arg.equals ("Blue"))
    this.jContentPane.setBackground(Color.blue);
    else if(arg.equals("C"))
    this.clearAll();
    else if(arg.equals("+")){
    lastNumber=getNumberInDisplay();
    clearOnNextDigit = true;
    lastOperator = "+";
    }
    else if(arg.equals("=")){
    displayResult(this.equalOperation());
    }
    else{
    addDigitToDisplay(Integer.parseInt(arg));
    }

    }
    /**
    *
    * @param i
    */
    private void displayResult(int i) {
    // TODO Auto-generated method stub
    setDisplayString(Integer.toString(result));
    lastNumber = result;
    clearOnNextDigit = true;
    }
    /**
    * Main Method
    * @param args
    */

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


    }

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    11

    Default

    Need to make sure lastNumber gets updated with the value of result.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    I cannot even find the code that adds together all of the entered numbers. Can you add a comment line that shows where this is (maybe write it in all caps or something to make it stand out)? Also, it's recommended you post code using code tags &91;code&93; // your code here &91;/code&93; and format it using tabs. Here is an example of the resulting code.

    Java Code:
    /**
    * JFarme package
    */
    package org.eclipse.swingapp;
    
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    
    import java.awt.Color;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Calculator extends JFrame implements ActionListener{
    
        private static final long serialVersionUID = 1L;
    
        private final int INPUT_MODE = 0;
        private final int RESULT_MODE = 1;
        private int displayMode;
        private boolean clearOnNextDigit=true;
        private int lastNumber,result;
        private String lastOperator; // @jve:decl-index=0:
    
        private JPanel jContentPane = null;
    
        private JButton but1 = null;
    
        private JButton but2 = null;
    
        private JButton but3 = null;
    
        private JButton but4 = null;
    
        private JButton but5 = null;
    
        private JButton but6 = null;
    
        private JButton but7 = null;
    
        private JButton but8 = null;
    
        private JButton but9 = null;
    
        private JButton but0 = null;
    
        private JButton butPlus = null;
    
        private JButton butEqual = null;
    
        private JTextField jTextField = null;
    
        private JButton butC = null;
    
        private JButton ButRed = null;
    
        private JButton butBlue = null;
    
        /**
        * This is the default constructor
        */
        public Calculator() {
            super();
            initialize();
        }
    
        /**
        * This method initializes this
        *
        * @return void
        */
        private void initialize() {
            this.setSize(300, 200);
            this.setContentPane(getJContentPane());
            this.setTitle("Calculator");
        }
    
        /**
        * This method initializes jContentPane
        *
        * @return javax.swing.JPanel
        */
        private JPanel getJContentPane() {
            if (jContentPane == null) {
                GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
                gridBagConstraints17.gridx = 4;
                gridBagConstraints17.gridy = 4;
                GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
                gridBagConstraints16.gridx = 4;
                gridBagConstraints16.gridy = 5;
                GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
                gridBagConstraints14.gridx = 1;
                gridBagConstraints14.gridy = 5;
                GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
                gridBagConstraints13.fill = GridBagConstraints.VERTICAL;
                gridBagConstraints13.gridy = 1;
                gridBagConstraints13.weightx = 1.0;
                gridBagConstraints13.insets = new Insets(0, 0, 2, 2);
                gridBagConstraints13.gridx = 4;
                GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
                gridBagConstraints11.gridx = 0;
                gridBagConstraints11.gridy = 4;
                GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
                gridBagConstraints10.gridx = 2;
                gridBagConstraints10.gridy = 4;
                GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
                gridBagConstraints9.gridx = 1;
                gridBagConstraints9.gridy = 4;
                GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
                gridBagConstraints8.gridx = 2;
                gridBagConstraints8.gridy = 3;
                GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
                gridBagConstraints7.gridx = 1;
                gridBagConstraints7.gridy = 3;
                GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
                gridBagConstraints6.gridx = 0;
                gridBagConstraints6.gridy = 3;
                GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
                gridBagConstraints5.gridx = 2;
                gridBagConstraints5.gridy = 1;
                GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
                gridBagConstraints4.gridx = 1;
                gridBagConstraints4.gridy = 1;
                GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
                gridBagConstraints3.gridx = 0;
                gridBagConstraints3.gridy = 1;
                GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
                gridBagConstraints2.gridx = 2;
                gridBagConstraints2.gridy = 0;
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
                gridBagConstraints1.gridx = 1;
                gridBagConstraints1.gridy = 0;
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
                gridBagConstraints.gridx = 0;
                gridBagConstraints.gridy = 0;
                jContentPane = new JPanel();
                jContentPane.setLayout(new GridBagLayout());
                jContentPane.add(getBut1(), gridBagConstraints);
                jContentPane.add(getBut2(), gridBagConstraints1);
                jContentPane.add(getBut3(), gridBagConstraints2);
                jContentPane.add(getBut4(), gridBagConstraints3);
                jContentPane.add(getBut5(), gridBagConstraints4);
                jContentPane.add(getBut6(), gridBagConstraints5);
                jContentPane.add(getBut7(), gridBagConstraints6);
                jContentPane.add(getBut8(), gridBagConstraints7);
                jContentPane.add(getBut9(), gridBagConstraints8);
                jContentPane.add(getBut0(), gridBagConstraints9);
                jContentPane.add(getButPlus(), gridBagConstraints10);
                jContentPane.add(getButEqual(), gridBagConstraints11);
                jContentPane.add(getJTextField(), gridBagConstraints13);
                jContentPane.add(getButC(), gridBagConstraints14);
                jContentPane.add(getButRed(), gridBagConstraints16);
                jContentPane.add(getButBlue(), gridBagConstraints17);
                this.ButRed.addActionListener(this);
                this.butBlue.addActionListener(this);
                this.butC.addActionListener(this);
                this.butPlus.addActionListener(this);
                this.butEqual.addActionListener(this);
                this.but0.addActionListener(this);
                this.but1.addActionListener(this);
                this.but2.addActionListener(this);
                this.but3.addActionListener(this);
                this.but4.addActionListener(this);
                this.but5.addActionListener(this);
                this.but6.addActionListener(this);
                this.but7.addActionListener(this);
                this.but8.addActionListener(this);
                this.but9.addActionListener(this);
            }
            return jContentPane;
        }
    
        /**
        * This method initializes but1
        *
        * @return javax.swing.JButton
        */
        private JButton getBut1() {
            if (but1 == null) {
                but1 = new JButton();
                but1.setText("1");
            }
            return but1;
        }
    
        /**
        * This method initializes but2
        *
        * @return javax.swing.JButton
        */
        private JButton getBut2() {
            if (but2 == null) {
                but2 = new JButton();
                but2.setText("2");
            }
            return but2;
        }
    
        /**
        * This method initializes but3
        *
        * @return javax.swing.JButton
        */
        private JButton getBut3() {
            if (but3 == null) {
                but3 = new JButton();
                but3.setText("3");
            }
            return but3;
        }
    
        /**
        * This method initializes but4
        *
        * @return javax.swing.JButton
        */
        private JButton getBut4() {
            if (but4 == null) {
                but4 = new JButton();
                but4.setText("4");
            }
            return but4;
        }
    
        /**
        * This method initializes but5
        *
        * @return javax.swing.JButton
        */
        private JButton getBut5() {
            if (but5 == null) {
                but5 = new JButton();
                but5.setText("5");
            }
            return but5;
        }
    
        /**
        * This method initializes but6
        *
        * @return javax.swing.JButton
        */
        private JButton getBut6() {
            if (but6 == null) {
                but6 = new JButton();
                but6.setText("6");
            }
            return but6;
        }
    
        /**
        * This method initializes but7
        *
        * @return javax.swing.JButton
        */
        private JButton getBut7() {
            if (but7 == null) {
                but7 = new JButton();
                but7.setText("7");
            }
            return but7;
        }
    
        /**
        * This method initializes but8
        *
        * @return javax.swing.JButton
        */
        private JButton getBut8() {
            if (but8 == null) {
                but8 = new JButton();
                but8.setText("8");
            }
            return but8;
        }
    
        /**
        * This method initializes but9
        *
        * @return javax.swing.JButton
        */
        private JButton getBut9() {
            if (but9 == null) {
                but9 = new JButton();
                but9.setText("9");
            }
            return but9;
        }
    
        /**
        * This method initializes but0
        *
        * @return javax.swing.JButton
        */
        private JButton getBut0() {
            if (but0 == null) {
                but0 = new JButton();
                but0.setText("0");
            }
            return but0;
        }
    
        /**
        * This method initializes butPlus
        *
        * @return javax.swing.JButton
        */
        private JButton getButPlus() {
            if (butPlus == null) {
                butPlus = new JButton();
                butPlus.setText("+");
            }
            return butPlus;
        }
    
        /**
        * This method initializes butEqual
        *
        * @return javax.swing.JButton
        */
        private JButton getButEqual() {
            if (butEqual == null) {
                butEqual = new JButton();
                butEqual.setName("");
                butEqual.setText("=");
            }
            return butEqual;
        }
    
        /**
        * This method initializes jTextField
        *
        * @return javax.swing.JTextField
        */
        private JTextField getJTextField() {
            if (jTextField == null) {
                jTextField = new JTextField();
                jTextField.setPreferredSize(new Dimension(80, 20));
            }
            return jTextField;
        }
    
        /**
        * This method initializes butC
        *
        * @return javax.swing.JButton
        */
        private JButton getButC() {
            if (butC == null) {
                butC = new JButton();
                butC.setText("C");
            }
            return butC;
        }
    
        /**
        * This method initializes ButRed
        *
        * @return javax.swing.JButton
        */
        private JButton getButRed() {
            if (ButRed == null) {
                ButRed = new JButton();
                ButRed.setName("");
                ButRed.setPreferredSize(new Dimension(59, 26));
                ButRed.setText("Red");
            }
            return ButRed;
        }
    
        /**
        * This method initializes butBlue
        *
        * @return javax.swing.JButton
        */
        private JButton getButBlue() {
            if (butBlue == null) {
                butBlue = new JButton();
                butBlue.setText("Blue");
            }
            return butBlue;
        }
        /**
        * This method clears the result field
        *
        */
        private void clearAll() {
            setDisplayString("0");
            lastOperator = "0";
            lastNumber = 0;
            displayMode =0;
            result=0;
            clearOnNextDigit = true;
        }
        /**
        *
        * @param String s
        */
        private void setDisplayString(String s){
            this.jTextField.setText(s);
        }
        /**
        *
        * @return text
        */
        private String getDisplayString (){
            return this.jTextField.getText();
        }
        /**
        *
        * @param digit
        */
        private void addDigitToDisplay(int digit){
            if (clearOnNextDigit)
                setDisplayString("");
    
            String inputString = getDisplayString();
            setDisplayString(inputString+ digit);
            displayMode = INPUT_MODE;
            clearOnNextDigit = false;
        }
        /**
        *
        * @return int
        */
        private int getNumberInDisplay() {
            String input = this.jTextField.getText();
            return Integer.parseInt(input);
        }
        /**
        *
        * @return result
        */
        private int equalOperation(){
            result = lastNumber + getNumberInDisplay();
            lastOperator = "0";
            return result;
        }
        /**
        * Performe Action
        */
        public void actionPerformed(ActionEvent ev) {
            // TODO Auto-generated method stub
            String arg= ev.getActionCommand();
            if (arg.equals ("Red"))
                this.jContentPane.setBackground(Color.red);
            else if (arg.equals ("Blue"))
                this.jContentPane.setBackground(Color.blue);
            else if(arg.equals("C"))
                this.clearAll();
            else if(arg.equals("+")){
                lastNumber=getNumberInDisplay();
                clearOnNextDigit = true;
                lastOperator = "+";
            }
            else if(arg.equals("=")){
                displayResult(this.equalOperation());
            }
            else{
                addDigitToDisplay(Integer.parseInt(arg));
            }
    
        }
        /**
        *
        * @param i
        */
        private void displayResult(int i) {
            // TODO Auto-generated method stub
            setDisplayString(Integer.toString(result));
            lastNumber = result;
            clearOnNextDigit = true;
        }
        /**
        * Main Method
        * @param args
        */
    
        public static void main(String[] args){
            Calculator c=new Calculator();
            c.setVisible(true);
        }
    
    
    }

  4. #4
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

Similar Threads

  1. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 PM
  2. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 AM
  3. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 06:33 AM
  4. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 04:27 PM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 12:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •