devxlogo

Push Button Increments

Push Button Increments

Question:
I have this declaration in init: pushButton1 = new Button(“0”). In the action class I try to increase the value from 0 through 9 every time I click the button trying pushButton1++, but it gives me an error.

    if(e.target == pushButton1){           showStatus(“You pressed: ” + o.toString());           pushButton1++;        }                                                   

Answer:
Since pushButton1 is an instance of Button, it doesn’t make sense to increment it using the ++ operator. Only integers can be incremented this way.

Try this instead:

   if(e.target==pushButton1) {      int num = Integer.getValue(pushButton1.getLabel()).intValue();      AppletContext context = getAppletContext();      context.showStatus(“You pushed ” + num);      pushButton1.setLabel(“” + ++num);      repaint();   } 

devx-admin

Share the Post: