I just wanna test my event,but it doesnt work,anything wrong?
Pls help...thanks..

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


class Frame extends JFrame 
{
    private static final Font numFont = new Font("monspaced", Font.PLAIN, 14);
    private static final Font font = new Font("monspaced", Font.PLAIN, 14);      
    private JTextField displayField;       // display result / input.
    
    //... Variables representing state of the calculator
    String Status;          //String to hold the status of various parts 
                            //of the program
    boolean OperatorKey;    //the 'flag' that will signify whether or not any 
                            //operator button has been pressed
   
    boolean FunctionKey;    //the 'flag' that will signify whether or not any f
                            //function button has been pressed

    int Counter;            //counts the number of digits entered

    double Result;          //the answer displayed, as well as the second 
                            //operator taken for an operation
    
    int Operator;           //an integer value to indicate which operator
                            //button was pressed
                            
    double Operand;         //the first number entered for an operation
    
    double Mem;             //the variable which holds whatever value the user 
                            //wants kept in "memory"
    
    boolean DecimalFlag;    //the 'flag' that will signify whether or not the 
                            //decimal button has been pressed
    
    boolean SignFlag;       //the 'flag' that will signify whether or not the 
                            //plus/minus button has been pressed                     
                            
    JButton button1 = new JButton("1");
    JButton button2 = new JButton("2");
    JButton button3 = new JButton("3");
    JButton button4 = new JButton("4");
    JButton button5 = new JButton("5");
    JButton button6 = new JButton("6");
    JButton button7 = new JButton("7");
    JButton button8 = new JButton("8");
    JButton button9 = new JButton("9");
    JButton button0 = new JButton("0");        
    JButton buttonDecimal  = new JButton(".");                      
    
    JButton buttonMinus    = new JButton("-");
    JButton buttonMultiply = new JButton("x");
    JButton buttonPlus     = new JButton("+");
    JButton buttonEquals   = new JButton("=");
    JButton buttonDivide   = new JButton("/");
    JButton buttonClear    = new JButton("C");
    JButton buttonMPlus    = new JButton("M+");
    JButton buttonMClear   = new JButton("MC");
    JButton buttonMR       = new JButton("MR");
    JButton buttonPercent  = new JButton("%");
    JButton buttonOneOverX = new JButton("1/X");
    JButton buttonSqr      = new JButton("sq");
    JButton buttonSqrRoot  = new JButton("sqrt");
    
    //This label will display all error messages
    JLabel errorMsg = new JLabel(" ");
    
    /* This label is just to the left of the Display Label displayField, and will 
     * indicate whether or not a value is being held in the calculator's "memory"
     */ 
    Label LabelMem = new Label(" ",Label.RIGHT);         
   
    public static void main(String[] args) 
    {
        //... Set the Look and Feel to that of system we're running on.
        try 
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } 
        
        catch (Exception unused) 
        {
            ; // Ignore exception because we can't do anything.  Will use default.
        }
        
        //... Create the window.
        Frame window = new Frame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
        
    public Frame() 
    {              
        //... Set attributes of the display field
        displayField = new JTextField("0", 12);
        displayField.setHorizontalAlignment(JTextField.RIGHT);
        displayField.setFont(font);     
        
        //... Use one listener for all numeric keys.
        ActionListener numListener = new NumListener();
        
        //... Layout numeric keys in a grid.  Generate the buttons
        //    in a loop from the chars in a string.
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(5, 3, 2, 2));
        
        // This is the declaration of all numeric buttons to be used in the applet
        button1.addActionListener(numListener);
        button1.setFont(numFont);
        buttonPanel.add(button1);

        button2.addActionListener(numListener);
        button2.setFont(numFont);
        buttonPanel.add(button2);
        
        button3.addActionListener(numListener);
        button3.setFont(numFont);
        buttonPanel.add(button3);
        
        button4.addActionListener(numListener);
        button4.setFont(numFont);
        buttonPanel.add(button4);
        
        button5.addActionListener(numListener);
        button5.setFont(numFont);
        buttonPanel.add(button5);
        
        button6.addActionListener(numListener);
        button6.setFont(numFont);
        buttonPanel.add(button6);
        
        button6.addActionListener(numListener);
        button6.setFont(numFont);
        buttonPanel.add(button6);
        
        button7.addActionListener(numListener);
        button7.setFont(numFont);
        buttonPanel.add(button7);
        
        button8.addActionListener(numListener);
        button8.setFont(numFont);
        buttonPanel.add(button8);
        
        button9.addActionListener(numListener);
        button9.setFont(numFont);
        buttonPanel.add(button9);
        
        button0.addActionListener(numListener);
        button0.setFont(numFont);
        buttonPanel.add(button0);
        
        buttonDecimal.addActionListener(numListener);
        buttonDecimal.setFont(numFont);
        buttonPanel.add(buttonDecimal);

        //... Create panel with gridlayout to hold operator buttons.
        JPanel opPanel1 = new JPanel();
        opPanel1.setLayout(new GridLayout(4, 2, 2, 2));
                
        buttonPlus.addActionListener(numListener);
        buttonPlus.setFont(font);
        opPanel1.add(buttonPlus);
        
        buttonMinus.addActionListener(numListener);
        buttonMinus.setFont(font);
        opPanel1.add(buttonMinus);
        
        buttonMultiply.addActionListener(numListener);
        buttonMultiply.setFont(font);
        opPanel1.add(buttonMultiply);
        
        buttonDivide.addActionListener(numListener);
        buttonDivide.setFont(font);
        opPanel1.add(buttonDivide);
        
        buttonEquals.addActionListener(numListener);
        buttonEquals.setFont(font);
        opPanel1.add(buttonEquals);
        
        buttonSqr.addActionListener(numListener);
        buttonSqr.setFont(font);
        opPanel1.add(buttonSqr);
        
        buttonPercent.addActionListener(numListener);
        buttonPercent.setFont(font);
        opPanel1.add(buttonPercent);
        
        buttonOneOverX.addActionListener(numListener);
        buttonOneOverX.setFont(font);
        opPanel1.add(buttonOneOverX);
        
        JPanel opPanel2 = new JPanel();
        opPanel2.setLayout(new GridLayout(4, 1, 2, 2));

        buttonMPlus.addActionListener(numListener);
        buttonMPlus.setFont(font);
        opPanel2.add(buttonMPlus);
        
        buttonMClear.addActionListener(numListener);
        buttonMClear.setFont(font);
        opPanel2.add(buttonMClear);
        
        buttonMR.addActionListener(numListener);
        buttonMR.setFont(font);
        opPanel2.add(buttonMR);
        
        buttonMPlus.addActionListener(numListener);
        buttonMPlus.setFont(font);
        opPanel2.add(buttonMPlus);
        
        JPanel msgPanel = new JPanel();
        msgPanel.add(errorMsg);
        
        //... Layout the top-level content panel.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout(5, 5));
        content.add(displayField, BorderLayout.NORTH );
        content.add(buttonPanel   , BorderLayout.CENTER);
        content.add(opPanel1       , BorderLayout.EAST);
        content.add(opPanel2    , BorderLayout.WEST);
        content.add(msgPanel    , BorderLayout.SOUTH);
        
        content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        
        //... Finish building the window (JFrame)
        this.setContentPane(content);
        this.pack();
        this.setTitle("Calculator");
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        
        Clicked_Clear();
    }//end constructor
    
    /* The following integer values are being set as "final" because
     * they are going to be used for determining what button has been
     * pushed
     */
    public final static int OpMinus=11,
                            OpMultiply=12,
                            OpPlus=13,
                            OpDivide=15,
                            OpMPlus=19,
                            OpMClear=20,
                            OpMR=21;
                            
    class NumListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent event)
        {
            
                /* Declares an object named "src" to represent the event.getSource()
                 * method. This shortens the code that follows.
                 */
                
             Object src = event.getSource();
        
             /* Checks to see if the source of the event which src is representing 
              * * is a Button and can behave like a Button
              * * */
              if (src instanceof Button)
              {
                  /* Checks to see if Status string holds the value ERROR OR if the 
                   * * source of the event is the buttonClear ("C") button
                   * * */
                   if ((Status != "ERROR") || (src == buttonClear))
                   {
                       /* The following conditions check for numeric buttons that have 
                        *  been pressed, and then called the method NumericButton and 
                        *  send it an integer value depending on the value of the button 
                        *  pressed
                        */
                       
                       if (src == button1)
                       {
                           NumericButton(1);
                       }
                       if (src == button2)
                       {
                           NumericButton(2);
                       }
                       if (src == button3)
                       {
                           NumericButton(3);
                       }
                       if (src == button4)
                       {
                           NumericButton(4);
                       }
                       if (src == button5)
                       {
                           NumericButton(5);
                       }
                       if (src == button6)
                       {
                           NumericButton(6);
                       }
                       if (src == button7)
                       {
                           NumericButton(7);
                       }
                       if (src == button8)
                       {
                           NumericButton(8);
                       }
                       if (src == button9)
                       {
                           NumericButton(9);
                       }
                       if (src == button0)
                       {
                          NumericButton(0);
                       }

                       /* The following conditions check for operator buttons that have 
                        * been pressed, and then call the OperatorButton method, sending 
                        * the corresponding integer value 
                        */
                    
                       if (src == buttonMinus)
                       {
                          OperatorButton(11);
                       }
                       if (src == buttonMultiply)
                       {
                          OperatorButton(12);
                       }
                       if (src == buttonPlus)
                       {
                          OperatorButton(13);
                       }
                       if (src == buttonEquals)
                       {
                          OperatorButton(14);
                       }
                       if (src == buttonDivide)
                       {
                          OperatorButton(15);
                       }

                       /* This condition checks if the decimal button has been pressed, 
                        * and then calls the DecimalButton method
                        */
                
                       if (src == buttonDecimal)
                       {
                          DecimalButton();
                       }

                       /* This condition checks if the percent button has been pressed, 
                        * and then calls the PercentButton method
                        */
                    
                       if (src == buttonPercent)
                       {
                          PercentButton();
                       }

                       /* This condition checks if the clear button has been pressed, 
                        * and then calls the Clicked_Clear method
                        */
                
                       if (src == buttonClear)
                       {
                          Clicked_Clear();
                       }
                    
                       /* This condition checks if the Plus Minus Button has been 
                        * pressed, and then calls the PlusMinusButton method
                        */
                
                       //------if (src == buttonNegative)
                       //------{
                       //------    PlusMinusButton();
                       //------}

                       /* This condition checks if the Square Button has been pressed, 
                        * and then calls the SqrButton method
                        */
                
                       if (src == buttonSqr)
                       {
                          SqrButton();
                       }

                       /* This condition checks if the Square Root Button has been
                        * pressed, and then calls the SqrRootButton method
                        */
                
                       if (src == buttonSqrRoot)
                       {
                          SqrRootButton();
                        }
                
                        /* This condition checks if the One over X Button has been 
                         * pressed, and then calls the OneOverXButton method
                         */
                
                        if (src == buttonOneOverX)
                        {
                            OneOverXButton();
                        }

                        /* These conditions checks to see if any of the Memory Buttons 
                         * have been pressed, and then calls the MemoryButton method and 
                         * sends them the corresponding integer values
                         */
                    
                        if (src == buttonMPlus)
                        {   
                            MemoryButton(19);
                        }

                        if (src == buttonMClear)
                        {
                            MemoryButton(20);
                        }

                        if (src == buttonMR)
                        {
                            MemoryButton(21);
                        }
                    }
                }
            }
               
                  
    /* This method is called whenever a numeric button (0-9) is pushed. */
    
    public void NumericButton(int i) 
    {   
        DisplayError(" ");          //Clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the lcdDisplay of the calculator
         */
        String Display = displayField.getText();

        /* Checks if an operator key has just been pressed, and if it has, 
         * then the limit of 20 digits will be reset for the user so that 
         * they can enter in up to 20 new numbers
         */
        if (OperatorKey == true)
        {
            Counter = 0;
        }
        
        Counter = Counter + 1;      //increments the counter

        /* This is a condition to see if the number currently displayed is zero OR 
         * an operator key other than +, -, *, or / has been pressed.
         */
        if ((Display == "0") || (Status == "FIRST"))
        {
            Display= "";            //Do not display any new info
        }

        if (Counter < 21)           //if more than 20 numbers are entered
        {
            //The number just entered is appended to the string currently displayed
            Display = Display + String.valueOf(i);
        }
        else
        {
            //call the DisplayError method and send it an error message string
            DisplayError("Digit Limit of 20 Digits Reached");
        }

        displayField.setText(Display);  //sets the text of the displayField                     
                                        //Label

        Status = "VALID";               //sets the Status string to valid
        OperatorKey = false;            //no operator key was pressed
        FunctionKey = false;            //no function key was pressed
    }
    
    /* This method is called whenever an operator button is pressed, and is    
     * sent an integer value representing the button pressed.
     */
    public void OperatorButton(int i) 
    {       
        DisplayError(" ");          //Clears the error message field
                
        /* Creates a new Double object with the specific purpose of retaining
         * the string currently on the displayField label, and then immediately
         * converts that string into a double-precision real number and then 
         * gives that number to the variable Result.
         */
        Result = (new Double(displayField.getText())).doubleValue();
   
        //If no operator key has been pressed OR a function has been pressed
        if ((OperatorKey == false) || (FunctionKey = true))
        {
            switch (Operator)           //depending on the operation performed
            {
                /* if the user pressed the addition button, add the two numbers 
                 * and put them in double Result
                 */
                case OpPlus     : Result = Operand + Result;
                                  break;
                                  
                /* if the user pressed the subtraction button, subtract the two
                 * numbers and put them in double Result
                 */
                case OpMinus    : Result = Operand - Result;
                                  break;
                                  
                /* if the user pressed the multiplication button, multiply
                 * the two numbers and put them in double Result
                 */
                case OpMultiply : Result = Result * Operand;
                                  break;
                                  
                /* if the user pressed the division button, check to see if 
                 * the second number entered is zero to avoid a divide-by-zero
                 * exception
                 */
                case OpDivide   : if (Result == 0)
                                  {
                                      //set the Status string to indicate an
                                      //an error
                                      Status = "ERROR";
                                      
                                      //display the word "ERROR" on the 
                                      //lcdDisplay label
                                      displayField.setText("ERROR");
                                      
                                      /* call the DisplayError method and 
                                       * send it a string indicating an error 
                                       * has occured and of what type
                                       */
                                      DisplayError("ERROR: Division by Zero");
                                  }
                                  else
                                  {
                                      //divide the two numbers and put the 
                                      //answer in double Result
                                      Result = Operand / Result;
                                  }
            }
            
            //if after breaking from the switch the Status string is not set
            //to "ERROR"
            if (Status != "ERROR")
            {
                Status = "FIRST";   /* set the Status string to "FIRST" to 
                                     * indicate that a simple operation was 
                                     * not performed
                                     */
                
                Operand = Result;   //Operand holds the value of Result
                
                Operator = i;       /* the integer value representing the 
                                     * operation being performed is stored 
                                     * in the integer Operator
                                     */
                
                //The lcdDisplay label has the value of double Result 
                //displayed
                displayField.setText(String.valueOf(Result));
                
                //The boolean decimal flag is set false, indicating that the 
                //decimal button has not been pressed
                DecimalFlag = false;
                
                //The boolean sign flag is set false, indicating that the sign
                //button has not been pressed
                SignFlag = false;
                
                //The boolean OperatorKey is set true, indicating that a simple
                //operation has been performed
                OperatorKey = true;
                
                //The boolean FunctionKey is set false, indicating that a 
                //function key has not been pressed
                FunctionKey = false;
                
                DisplayError(" ");      //Clears the error message field
            }

        }
   
    }           //end of OperatorButton method
    
    /* This is a method that is called whenever the decimal button is 
     * pressed.
     */
    public void DecimalButton()
    {   
        DisplayError(" ");      //Clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the displayField of the calculator
         */
        String Display = displayField.getText();
        
        //if a simple operation was performed successfully
        if (Status == "FIRST")
        {
            Display = "0";      //set Display string to character 0
        }

        //If the decimal button has not already been pressed
        if (!DecimalFlag)
        {
            //appends a decimal to the string Display
            Display = Display + "."; 
        }
        else
        {
            /* calls the DisplayError method, sending a string 
             * indicating that the number already has a decimal 
             */
            DisplayError("Number already has a Decimal Point");
        }
        
        /* calls the setText method of the Label lcdDisplay and 
         * sends it the string Display
         */
        displayField.setText(Display);
        DecimalFlag = true;     //the decimal key has been pressed
        Status = "VALID";       /* Status string indicates a valid
                                 * operation has been performed
                                 */
        OperatorKey = false;    //no operator key has been pressed
        
    }   //end of the DecimalButton method

    /* This method is called whenever the percent button is pressed
     */
    void PercentButton()
    {
        DisplayError(" ");      //clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the lcdDisplay of the calculator
         */
        String Display = displayField.getText();

        /* if the Status string is not set to "FIRST" OR the Display string
         * does not currently hold the value "0"
         */
        if ((Status != "FIRST") || (Display != "0"))
        {
            /* Creates a new Double object with the specific purpose of retaining
             * the string currently on the lcdDisplay label, and then immediately
             * converts that string into a double-precision real number and then 
             * gives that number to the variable Result.
             */
            Result = (new Double(displayField.getText())).doubleValue();
            
            //divide the double Result by 100, getting the percentage
            Result = Result / 100;
            
            /* call the setText method of Label lcdDisplay and send it the string
             * that represents the value in Result
             */
            displayField.setText(String.valueOf(Result));
            Status = "FIRST";       //
            OperatorKey = true;     //an operator key has been pressed
            FunctionKey = true;     //a function key has been pressed
        }
    }   //end of the PercentButton method

    /* This method is called first when the calculator is initialized 
     * with the init() method, and is called every time the "C" button 
     * is pressed
     */
    

    /* This method is called whenever the sign button is pressed */
    void PlusMinusButton()
    {
        DisplayError(" ");          //clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the lcdDisplay of the calculator
         */
        String Display = displayField.getText();

        /* if Status is not set to FIRST and the Display string does not
         * hold the value "0"
         */
        if ((Status != "FIRST") || (Display != "0"))
        {
            /* Creates a new Double object with the specific purpose of retaining
             * the string currently on the lcdDisplay label, and then immediately
             * converts that string into a double-precision real number and then 
             * gives that number to the variable Result.
             */
            Result = (new Double(displayField.getText())).doubleValue();
                        
            //sets the double Result to it's negative value
            Result = -Result;
            
            /* call the setText method of Label lcdDisplay and send it the string
             * that represents the value in Result
             */
            displayField.setText(String.valueOf(Result));
            Status = "VALID";       //sets Status string to VALID
            SignFlag = true;        //the sign button has been pressed
            DecimalFlag = true;     //a decimal has appeared
        }
    }   //end of the PlusMinusButton method

    /* This method is called whenever the square button is pressed */
    void SqrButton()
    {
        DisplayError(" ");          //clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the lcdDisplay of the calculator
         */
        String Display = displayField.getText();

        /* if Status is not set to FIRST and the Display string does not
         * hold the value "0"
         */
        if ((Status != "FIRST") || (Display != "0"))
        {
            /* Creates a new Double object with the specific purpose of retaining
             * the string currently on the lcdDisplay label, and then immediately
             * converts that string into a double-precision real number and then 
             * gives that number to the variable Result.
             */
            Result = (new Double(displayField.getText())).doubleValue();
            
            /* multiply the double Result by itself, effectively squaring 
             * the number */
            Result = Result * Result;
            
            /* call the setText method of Label lcdDisplay and send it the string
             * that represents the value in Result
             */
            displayField.setText(String.valueOf(Result));
            
            Status = "FIRST";       //indicates this is the first time
                                    //this button has been pressed
            
            OperatorKey = true;     //an operator button has been pressed
            FunctionKey = true;     //a function button has been pressed
        }
    }   //end of the SqrButton method
    
    /* This method is called whenever the square button is pressed */
    void SqrRootButton()
    {
        DisplayError(" ");          //clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the lcdDisplay of the calculator
         */
        String Display = displayField.getText();

        /* if Status is not set to FIRST and the Display string does not
         * hold the value "0"
         */
        if ((Status != "FIRST") || (Display != "0"))
        {
            /* Creates a new Double object with the specific purpose of retaining
             * the string currently on the lcdDisplay label, and then immediately
             * converts that string into a double-precision real number and then 
             * gives that number to the variable Result.
             */
            Result = (new Double(displayField.getText())).doubleValue();
            
            /* Makes a call to the Math class method "sqrt", which produced the 
             * square root and stores the value in Result
             */
            Result = Math.sqrt(Result);
            
            /* call the setText method of Label lcdDisplay and send it the string
             * that represents the value in Result
             */
            displayField.setText(String.valueOf(Result));
            
            Status = "FIRST";       //indicates this is the first time
                                    //this button has been pressed
            
            OperatorKey = true;     //an operator button has been pressed
            FunctionKey = true;     //a function button has been pressed
        }
    }   //end of the SqrRootButton method

    /* This method is called whenever the OneOverXButton is pressed */
    void OneOverXButton()
    {
        DisplayError(" ");          //clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the displayField of the calculator
         */
        String Display = displayField.getText();

        /* if Status is not set to FIRST and the Display string does not
         * hold the value "0"
         */
        if ((Status != "FIRST") || (Display != "0"))
        {
            /* Creates a new Double object with the specific purpose of retaining
             * the string currently on the lcdDisplay label, and then immediately
             * converts that string into a double-precision real number and then 
             * gives that number to the variable Result.
             */
            Result = (new Double(displayField.getText())).doubleValue();
            
            //divides one by the double Result 
            Result = 1 / Result;
            
            /* call the setText method of Label lcdDisplay and send it the string
             * that represents the value in Result
             */
            displayField.setText(String.valueOf(Result));
            
            Status = "FIRST";       //indicates that this is the first time
                                    //this button has been pressed
            
            OperatorKey = true;     //an operator key has been pressed
            FunctionKey = true;     //a function key has been pressed
        }
    }   //end of the OneOverXButton method

    /* This method is called whenever one of the three memory buttons is pressed,
     * accepting an integer representing the button pressed as an argument
     */
    void MemoryButton(int i)
    {
        DisplayError(" ");          //clears the error message field
        
        /* Declares a String called Display that will initialize to whatever 
         * is currently displayed in the lcdDisplay of the calculator
         */
        String Display = displayField.getText();
        
        //depending on the value sent representing the buttons
        switch (i)
        {
            /* if the M+ button is pressed, check if the Display string has the value 
             * "0." OR just "0", but along with the second condition check if double 
             * Mem holds the value zero
             */
            case OpMPlus : if (((Display == "0.") || (Display == "0")) && (Mem == 0))
                           {
                               //clears the LabelMem label
                               LabelMem.setText(" ");
                           }
                           else
                           {
                               /* Creates a new double variable called temp, which accepts the 
                                * double value of a new Double object retaining the string 
                                * currently on the lcdDisplay label, and then immediately 
                                * converts that string into a double-precision real number 
                                */
                               double temp = (new Double(displayField.getText())).doubleValue();
                               
                               //set the Mem variable with that the sum of what's currently in
                               //Mem and the temp variable
                               Mem = Mem + temp;
                               
                               /* calls the setText method of the Label LabelMem, sending
                                * it the character "M"
                                */
                               LabelMem.setText("M");
                           }
                           break;
                        
            /* if the MR button is pressed, call the setText method of Label lcdDisplay, 
             * sending it the string that represents what the value of Mem is
             */
            case OpMR : displayField.setText(String.valueOf(Mem));
                        break;
                        
            /* if the MC button is pressed, set the Mem variable to zero */
            case OpMClear : Mem = 0;
                            
                            //clears the LableMem field
                            LabelMem.setText(" ");
                            break;
        }
        Status = "FIRST";           //indicates that this is first time 
                                    //button has been pressed
        
        OperatorKey = true;         //an operator button has been pressed
        
        //if the Mem variable has the value zero
        if (Mem == 0)
        {
            LabelMem.setText(" ");  //clear the LabelMem field
        }
    }   //end of the MemoryButton method
}

void Clicked_Clear()
    {
        Counter = 0;                //sets the counter to zero
        Status = "FIRST";           //sets Status to FIRST
        Operand = 0;                //sets Operand to zero
        Result = 0;                 //sets Result to zero
        Operator = 0;               //sets Operator integer to zero
        
        DecimalFlag = false;        //decimal button has not been 
                                    //pressed
        
        SignFlag = false;           //sign button has not been pressed
        
        OperatorKey = false;        //no operator button has been 
                                    //pressed
        
        FunctionKey = false;        //no function button has been 
                                    //pressed
        
        /* calls the setText method of Label lcdDisplay and sends 
         * it the character "0"
         */
        displayField.setText("0");  
        DisplayError(" ");          //clears the error message field
    }
    
     /* This method is called whenever anything needs to be displayed
     * in the error message field at the bottom of the calculator,
     * accepting a String as an argument
     */
    void DisplayError(String err_msg)
       {
           /* calls the setText method of the Label DisplError, sending
            * whatever string it received initially 
            */
           errorMsg.setText(err_msg);
       }    
}//end of actionPerformed method
Salem commented: too much code without tags error - try again -4

Recommended Answers

All 4 Replies

What's wrong is that you posted that wall of code without code tags, creating an awful mess that no one is going to wade through to find this vague "event" you refer to.

Not urgent.

commented: Actually, I couldn't find any events that worked, out of the 15ish I tried in my quick run ;) +8

I just took a quick peak, but it's not very readable, may I suggest you use code tags?
I do have a couple of suggestions though:

- don't use integers to check what button has been pushed, just add a different actionEvent to each button.
- if you want to keep your code the way it is, use a switch statement instead of all those if's. For instance, replace the next part of code

if (src == button1)
{
NumericButton(1);
}
if (src == button2)
{
NumericButton(2);
}
if (src == button3)
{
NumericButton(3);
}

by

switch(src){
  case button1:  NumericButton(1); 
                        break;
  case button2:  NumericButton(2);
                       break;
  case button3:  NumericButton(3);
                       break;
  default: //the code to run if none of the above tests returned true
}

this way to implement these tests has got 2 big advantages:
1. it's easier to read, not to mention to add code if there come new buttons
2. it's a more efficiƫnt way, since after the right option has been found, it jumps out of the switch statement (using the break statement)
in your current code, even if it is button1, he will still run all the other if's, even though as the programmer you already know those 'll never return true.
an other way to do this, is by using lots of ifs, like this:

if (src == button1)
{
NumericButton(1);
}
else{
  if (src == button2)
  {
    NumericButton(2);
  }
  else{
     if (src == button3)
     {
        NumericButton(3);
     }
   }
}

but this kind of coding will get you a lot more lines of code, becomes pretty hard to maintain and to read after a while

if you register your JButtons like this

buttonName.addActionListener(this);

then they will fire an event when they are clicked. Also, you will need for your class to implement ActionListener. In the method actionPerfmored, do something likr

Object source = e.getSource(); // where e is the ActionEvent object being passed in. And then have a bunch of if statements like if (buttonName == source){ //do something}


...

buttonName.addActionListener(this);

then they will fire an event when they are clicked. ...

And then have a bunch of if statements like if (buttonName == source){ //do something}


...

why the hell would he want to do that? giving the entire class as an argument while you really do not need that??
as I tried to suggest earlier, but not everybody seems to get, I would suggest something more like this:

jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

....

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

all you need to pass on is an ActionEvent
code it like this, and there is no need for any if's, since you specify for each button what method should be called
even if, as I pointed out before, you would like to keep the 'one-method-for-all-buttons' approach, it would be far better to use a switch statement then a bunch of if's

Be a part of the DaniWeb community

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