Java AWT Unable to change a value of a variable in the actionPerformed() function

Viewed 19

I have two buttons on an applet and a variable "stat". The idea is that I want to change a value of the variable by either pressing button plus or minus that will add or subtract 1, respectively. But the variable "stat" does not change its value in any way. Can you please help me understand in what way can I overwrite a value of the variable?

import  java.awt.*;
import  java.applet.*;
import  java.awt.event.*;

public  class  test  extends  Applet implements ActionListener
{ 

   int stat=0;
   Button plus, minus;
   String str="";
   
   public void init()
   {
       
       plus = new Button("+");
       plus.setActionCommand("+");
       
       minus = new Button("-");
       minus.setActionCommand("-");
       

       minus.addActionListener(this);
       plus.addActionListener(this);
           
       add(plus);
       add(minus);
           
   }
   
   
 
   
   public void actionPerformed(ActionEvent ae)
   {
        str = ae.getActionCommand();

        if (str.equals("minus")) { stat-=1;  }
        else if (str.equals("plus")) { stat+=1; }
        
   }
   
   
}
0 Answers
Related