为什么每次激活此计时器时此变量都会重置?

时间:2014-10-13 22:08:29

标签: java swing timer

我有两个计时器。按下按钮1时,它们都应该被激活,按下按钮3时都应该停止它们。如果激活,则每秒钟,定时器将值1添加到它们各自的计数器,即计数器和计数器1。

如果按下停止按钮,则只有第一个定时器的计数器应重置为零,而第二个定时器的计数器应保持不变。

所以如果我按下开始,10秒后我按下停止,那么第一个计数器的值应该再次为0,而第二个计数器的值应为10。

如果我,在此之后,再按一次开始,10秒后我再次按停止,第一个计数器的值应该再次为0,而第二个计数器的值现在应为20。

我遇到的问题是,无论我做什么,如果我按停止,第二个计数器的值会保持重置,所以每次按下停止它再次变为0而不是保留与停止按钮之前相同的值按下了(因此在示例中它应该是20)。

这是代码(我留下了一些不重要的东西,可以根据要求上传完整的代码):

public class ColoredWordsExperiment {
    Timer timer;
    Timer timer1;
    TimerAction timeraction;
    TimerAction1 timeraction1;
    int counter;
    int counter1;

    ColoredWordsExperiment() {
        button1 = new JButton("Matching");
        button3 = new JButton("Finished");

        buttonHandler = new ButtonHandler(this);
        button1.addActionListener(buttonHandler);
        button3.addActionListener(buttonHandler);

        counter = 0;
        timeraction = new TimerAction(this);
        timer = new Timer(1000, timeraction);
        timer.setInitialDelay(1000);

        counter1 = 0;
        timeraction1 = new TimerAction1(this);
        timer1 = new Timer(1000, timeraction1);
        timer1.setInitialDelay(1000);

        }

    public static void main(String[] arg) {
        new ColoredWordsExperiment();
    }
}

-

class ButtonHandler implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public ButtonHandler(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent e){
        //if button1 pressed
        if (e.getActionCommand().equals("Matching")) {
            coloredWords.timer.start();
            coloredWords.timer1.start();
        //if button3 pressed
    }   else if (e.getActionCommand().equals("Finished")) {
            coloredWords.timer.stop();
            coloredWords.counter = 0;
            coloredWords.timer1.stop();
    }        

-

class TimerAction implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public TimerAction(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        coloredWords.counter++;
    }

}

-

class TimerAction1 implements ActionListener {
    ColoredWordsExperiment coloredWords;
    public TimerAction1(ColoredWordsExperiment coloredWords) {
        this.coloredWords = coloredWords;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        coloredWords.counter1++;
    }

}

1 个答案:

答案 0 :(得分:2)

您的修改代码适用于我:

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

@SuppressWarnings("serial")
public class ColoredWordsExperiment extends JPanel {
   Timer timer;
   Timer timer1;
   TimerAction timeraction;
   TimerAction1 timeraction1;
   private int counter;
   private int counter1;
   private JButton button1;
   private JButton button3;
   private ButtonHandler buttonHandler;
   private JLabel counterLabel = new JLabel("   ");
   private JLabel counter1Label = new JLabel("   ");

   ColoredWordsExperiment() {
      button1 = new JButton("Matching");
      button3 = new JButton("Finished");

      buttonHandler = new ButtonHandler(this);
      button1.addActionListener(buttonHandler);
      button3.addActionListener(buttonHandler);

      counter = 0;
      timeraction = new TimerAction(this);
      timer = new Timer(1000, timeraction);
      timer.setInitialDelay(1000);

      counter1 = 0;
      timeraction1 = new TimerAction1(this);
      timer1 = new Timer(1000, timeraction1);
      timer1.setInitialDelay(1000);

      add(button1);
      add(button3);
      add(new JLabel("Counter:"));
      add(counterLabel);
      add(new JLabel("Counter1:"));
      add(counter1Label);

   }

   public int getCounter() {
      return counter;
   }


   public void setCounter(int counter) {
      this.counter = counter;
      counterLabel.setText(String.valueOf(counter));
   }


   public int getCounter1() {
      return counter1;
   }


   public void setCounter1(int counter1) {
      this.counter1 = counter1;
      counter1Label.setText(String.valueOf(counter1));
   }


   private static void createAndShowGui() {
      ColoredWordsExperiment mainPanel = new ColoredWordsExperiment();

      JFrame frame = new JFrame("Colored Words Experiment");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class ButtonHandler implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public ButtonHandler(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      // if button1 pressed
      if (e.getActionCommand().equals("Matching")) {
         coloredWords.timer.start();
         coloredWords.timer1.start();
         // if button3 pressed
      } else if (e.getActionCommand().equals("Finished")) {
         coloredWords.timer.stop();
         //!! coloredWords.counter = 0;
         coloredWords.setCounter(0); //!! 
         coloredWords.timer1.stop();
      }
   }
}

class TimerAction implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public TimerAction(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent event) {
      coloredWords.setCounter(coloredWords.getCounter() + 1);
      //!! coloredWords.counter++;
   }

}

class TimerAction1 implements ActionListener {
   ColoredWordsExperiment coloredWords;

   public TimerAction1(ColoredWordsExperiment coloredWords) {
      this.coloredWords = coloredWords;
   }

   @Override
   public void actionPerformed(ActionEvent event) {
      //!! coloredWords.counter1++;
      coloredWords.setCounter1(coloredWords.getCounter1() + 1);
   }

}

向我确认您的问题位于代码未显示的其他位置。

作为一个侧面问题,我确实担心你的代码的非OOP直接操作字段。这增加了增加复杂性和可能看不见的副作用的风险,这是优秀的OOP实践旨在减少的。