-1

Essentially, I need help writing an increment and decrement code in my lab. I've managed to do the stuff beforehand, but I want to wrap my mind around what I'm doing with this. My first thought is creating some kind of for loop? Any ideas of how to go about this?

class IncreaseCount implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        int counter=0;
        counter++; 
        label.setText(String.valueOf(counter));
    }   
}

class DecreaseCount implements ActionListener {
     public void actionPerformed(ActionEvent e) {
        int counter = 0;
        counter--;
        label.setText(String.valueOf(counter));
     }
}
4

1 回答 1

1

您正在增加/减少一个局部变量。所以每次执行actionPerformed,计数器都会初始化为0,然后递增或递减。计数器应该是包含这两个动作侦听器的类的字段(属性)。

于 2013-03-10T00:22:10.760 回答